P.Kole
P.Kole

Reputation: 33

Connect to my server from ESP8266 Arduino

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?

Upvotes: 3

Views: 1904

Answers (1)

L Bahr
L Bahr

Reputation: 2901

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/

Upvotes: 2

Related Questions