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?

dda
  • 6,030
  • 2
  • 25
  • 34
P.Kole
  • 33
  • 1
  • 6

1 Answers1

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/

L Bahr
  • 2,723
  • 3
  • 22
  • 25
  • Hi, I saw the code in : http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/ , 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? – P.Kole Apr 13 '16 at 15:26
  • 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. – L Bahr Apr 13 '16 at 16:53
  • 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? – P.Kole Apr 15 '16 at 09:24
  • You can check the return value of AT+CIPSTATUS. A return status of 4 means it is disconnected. Check out [AT+CIPSTATUS](https://github.com/espressif/ESP8266_AT/wiki/CIPSTATUS) for more information. – L Bahr Apr 15 '16 at 14:04