/* Read the following carefully... https://www.arduino.cc/reference/en/language/functions/communication/serial/ I have an Arduino Mega, so I'll use Serial3... ESP8266 TX → Arduino Mega pin 15 (RX) ESP8266 RX → voltage divider → Arduino Mega pin 14 (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. */ // set SSID and password char const cmd[] = "AT+CWJAP=\"SkynetGlobalDefenseNetwork\",\"password\"\r\n"; // set baud rate unsigned long const baud = 115200; // If you want to use Serial Monitor, // make sure to set baud rate of Serial Monitor to this. // for sendCommand() unsigned long const timeout=10000; // 10 seconds unsigned long timee; char response[1000]; int i; void sendCommand(char command[], char acknowledgement[]) { while(true){ // send command to device Serial3.print(command); // listen until timeout memset(response, 0, sizeof(response)); // clear array i=0; timee = millis(); while ( timee + timeout > millis() ) { if (Serial3.available()){ // put time-critical stuff in its own loop while(Serial3.available()){ response[i] = char(Serial3.read()); i+=1; } if (strstr(response,acknowledgement)!=NULL) { Serial.println(response); Serial.println("___________________________________________________________________"); return; } } } Serial.println("Timeout reached!"); } } void setup() { Serial.begin(baud); Serial3.begin(baud); sendCommand("AT+RST\r\n", "OK"); //reset module delay(1000); //don't try to read the garbage during reset while(Serial3.available()){Serial3.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 (Serial3.find("+IPD,")) { while(!Serial3.available()){} cipsend[11]=char(Serial3.read()); Serial3.find("?"); Serial3.readStringUntil('='); value1 = Serial3.readStringUntil('&').toInt(); Serial3.readStringUntil('='); value2 = Serial3.readStringUntil('&').toInt(); Serial3.readStringUntil('='); state = ( Serial3.readStringUntil(' ') == "true" ); Serial.println(value1); Serial.println(value2); Serial.println(state); Serial.println(""); // clear buffer delay(100); while(Serial3.available()){Serial3.read();} sendCommand(cipsend, "OK"); sendCommand(httpResponse,"OK"); } }