I want to make a server using an ESP8266 (12e) and an Arduino Nano. I have got everything hooked and I created a minimal "Hello World" example, based on a number of samples found on the web, which is working fine... as long as the ESP does not receive multiple requests simultaneously.
I am using SoftwareSerial on pins 8 and 9 of my Arduino Nano.
As long as I send one request at the time, things work fine.
If I use two browsers and send two requests with little interval, things go wrong, and that figures: the sendData()
method just reads from the serial connection. It will consume the new request while waiting for an "OK" from the initial request...
Is there any sample code or library (for ESP + Arduino (nano)) that can handle correctly multiple concurrent connections?
Thanks
My code:
/*
Software serial multple serial test
*/
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(8, 9); // RX, TX
void setup() {
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different : 115200
Serial.println("Setup...");
/*
sendData("AT+RST\r\n",2000,DEBUG); // reset module
sendData("AT+GMR\r\n",1000,DEBUG); // get version
sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
*/
sendData("AT+RST\r\n", 2000,DEBUG);
sendData("AT+GMR\r\n", 1000, DEBUG);
sendData("AT+CWMODE=1\r\n",1000,DEBUG); // configure as station
sendData("AT+CWJAP=\"******\",\"*******\"\r\n", 6000, DEBUG);
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
Serial.println("Setup ready.");
}
//WIRE RESET TO HIGH!!?
void loop() { // run over and over
if(esp8266.available()) // check if the esp is sending a message
{
if(esp8266.find("+IPD,"))
{
delay(1000);
int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
String webpage = "<html><h1>Hello World!</h1></html>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand+= connectionId; // append connection id
closeCommand+="\r\n";
sendData(closeCommand,250,DEBUG);
}
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "?";
int pos = -5;
Serial.println("--------------");
Serial.println(" sending "+command);
esp8266.print(command); // send the read character to the esp8266
long int _timeout = timeout + millis();
while( (_timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
pos+=1;
if(pos>=0 && response.substring(pos) == "\r\nOK\r\n"){
//WE GOT OK -> stop waiting
_timeout=0;
}
}
}
if(debug)
{
Serial.println(" response: "+response);
}
return response;
}