/* Read the following carefully... https://www.arduino.cc/reference/en/language/functions/communication/serial/ I'll use SoftwareSerial... ESP8266 TX → Arduino pin 10 (RX) ESP8266 RX → voltage divider → Arduino pin 11 (TX) The serial buffer may only have a max of 64 bytes, so read from it quickly! New data sent to a full buffer is lost. */ #include // https://www.arduino.cc/en/Reference/SoftwareSerial // I *think* that you can also do any Serial function with this // in addition to the functions listed on this webpage. #define rxPin 10 #define txPin 11 SoftwareSerial mySerial(rxPin, txPin); // set SSID and password char const cmd[] = "AT+CWJAP=\"SkynetGlobalDefenseNetwork\",\"password\"\r\n"; // Set baud rate. unsigned long const baud = 57600; // You must choose from one of the allowed rates // https://www.arduino.cc/en/Reference/SoftwareSerialBegin // Note that 115200 is too high for my Arduino // (Serial Monitor displays garbage). // If you want to use Serial Monitor, // make sure to set baud rate of Serial Monitor to this. // Before uploading this code to Arduino, first configure // the baud rate of the ESP8266 using AT commands. // for sendCommand() unsigned long const timeout=10000; // 10 seconds unsigned long timee; char response[500]; int i; void sendCommand(char command[], char acknowledgement[]) { while(true){ // send command to device mySerial.print(command); // listen until timeout memset(response, 0, sizeof(response)); // clear array i=0; timee = millis(); while ( timee + timeout > millis() ) { if (mySerial.available()){ // put time-critical stuff in its own loop while(mySerial.available()){ response[i] = char(mySerial.read()); i+=1; } if (strstr(response,acknowledgement)!=NULL) { Serial.println(response); Serial.println("___________________________________________________________________"); return; } } } Serial.println("Timeout reached!"); } } void setup() { pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); Serial.begin(baud); mySerial.begin(baud); sendCommand("AT+RST\r\n", "OK"); //reset module delay(1000); //don't try to read the garbage during reset while(mySerial.available()){mySerial.read();} // clear buffer sendCommand("AT+CWMODE=1\r\n", "OK"); //set station mode to client sendCommand(cmd, "OK"); //connect wifi network sendCommand("AT+CIFSR\r\n", "OK"); // display IP address sendCommand("AT+CIPMUX=1\r\n", "OK"); // allow multiple connections sendCommand("AT+CIPSERVER=1,80\r\n", "OK"); // set to be in server mode } /* A response to a HTTP request is needed to stop certain applications from reopening the connection to request again and again. This is a problem because there are only 5 connections that the ESP8266 can make at any time. Using HTTP/1.0 prevents certain applications from requesting favicon.ico. The following response works. It is length 40. However, I'd like a longer one to be sure it will always work. char const httpResponse[] = "HTTP/1.0 200 OK\r\nContent-Length: 2\r\n\r\nhi"; The response I am using is length 99. */ char const httpResponse[] = "HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 2\r\nConnection: close\r\n\r\nhi"; char cipsend[] = "AT+CIPSEND=0,99\r\n"; int value1; int value2; bool state; // Note that the Arduino is receiving the HTTP GET request in a // format that is unique to the ESP8266. // The line of interest begins with "+IPD,". // Note that there is a default timeout of 1000 ms for // find() and readStringUntil() void loop() { if (mySerial.find("+IPD,")) { while(!mySerial.available()){} cipsend[11]=char(mySerial.read()); mySerial.find("?"); mySerial.readStringUntil('='); value1 = mySerial.readStringUntil('&').toInt(); mySerial.readStringUntil('='); value2 = mySerial.readStringUntil('&').toInt(); mySerial.readStringUntil('='); state = ( mySerial.readStringUntil(' ') == "true" ); Serial.println(value1); Serial.println(value2); Serial.println(state); Serial.println(""); // clear buffer delay(100); while(mySerial.available()){mySerial.read();} sendCommand(cipsend, "OK"); sendCommand(httpResponse,"OK"); } }