3

I have an Arduino Uno and a server written in C++. I connected the ESP8266 to my router successfully using the following code:

#include <SoftwareSerial.h>

SoftwareSerial esp8266(3, 2);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Started");
  // set the data rate for the SoftwareSerial port
  esp8266.begin(115200);
  esp8266.write("AT\r\n");
}

void loop() {
  if (esp8266.available()) {
    Serial.write(esp8266.read());
  }
  if (Serial.available()) {
    esp8266.write(Serial.read());
  }
}

Now, I want the ESP8266 to connect to my server as a client in the same LAN (I have the server IP). How can I do it with SoftwareSerial? Is there another way to do it?

1 Answer 1

2

You have to send it AT commands to create a HTTP request. This would connect to a server at 192.168.88.35 on port 80

// Connect to the server
esp8266.write("AT+CIPSTART=\"TCP\",\"192.168.88.35\",80\r\n"); //make this command: AT+CPISTART="TCP","192.168.88.35",80

//wait a little while for 'Linked'
delay(300);

//This is our HTTP GET Request change to the page and server you want to load.
String cmd = "GET /status.html HTTP/1.0\r\n";
cmd += "Host: 192.168.88.35\r\n\r\n";

//The ESP8266 needs to know the size of the GET request
esp8266.write("AT+CIPSEND=");
esp8266.write(cmd.length());
esp8266.write("\r\n");

esp8266.write(cmd);
esp8266.write("AT+CIPCLOSE\r\n");

This link should help if you need more details: http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I saw the code in : blog.huntgang.com/2015/01/20/… , but it connects the server everytime (in the loop function), but I need it to connect my server only one time... How can I do it?
Put the connection logic in setup() instead of loop(). Code that is in setup() will only run 1 time as soon as the arduino is started. After that is runs the loop() in a loop.
Yes I know that, but 1 time is not enough for the esp8266 to connect... It needs a lot of tries... How can I detect whether the esp is connected?
You can check the return value of AT+CIPSTATUS. A return status of 4 means it is disconnected. Check out AT+CIPSTATUS for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.