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;
}
-
NodeMCU can be an alternative. All in one package: WiFi, faster CPU, lots of RAM.user31481– user314812017年10月06日 08:24:01 +00:00Commented Oct 6, 2017 at 8:24
2 Answers 2
I might not have understood what you are trying to do, but if it is to use the ESP8266 to allow the Nano to connect to WIFI and act as a web server then I believe you are doing this the hard way.
The ESP8266 is an 80 or 160MHz microprocessor with 64KB Program Ram + 96KB data RAM. You are using a 16/32MHz microprocessor to generate pages and pass them to it. I would recommend that you investigate using the Arduino IDE to program the ESP8266 in "Arduino C" it comes with a set of examples that include webservers (which is what I think you are trying to do). Keep the Nano if the ESP8266s 10 digital and one analogue ports aren't enough to interface to you hardware.
-
1I will need more than one analog input... Could I still use the ESP module as "master", as "Server" and query the Arduino's analog ports over serial connection?mvermand– mvermand2016年05月18日 16:41:39 +00:00Commented May 18, 2016 at 16:41
-
1@mvermand if your case allows to read in the analog values one by one (not parallel), you could just use a multiplexer to achieve this - I just did that using an ESP and the HCF4051 - you need 3 control pins to address one of the 8 channels, and then open that to the ESPs ADC. Works just fine and stable.Bachi– Bachi2017年02月16日 09:20:36 +00:00Commented Feb 16, 2017 at 9:20
-
the esp8266 certainly doesn't have 4 MB of RAM, the highest amount of free RAM I've seen was around 40 kB.MightyPork– MightyPork2017年10月05日 21:03:55 +00:00Commented Oct 5, 2017 at 21:03
-
@MightyPork - Quoted the wrong number, corrected it now, but its still more than an Arduino Uno.Code Gorilla– Code Gorilla2017年10月06日 07:26:12 +00:00Commented Oct 6, 2017 at 7:26
I had the same experience. When you send data by an AT command and receive something from another connection at the same time, then the ESP chip starts to behave unexpectedly. Sometimes it sends the data but does not respond with OK. Sometimes it does not send. And sometimes it ignores received data (missing +IPD message).
The AT firmware works only well with single telnet-style connections. I mean half-duplex communication where it never happens that more than one sender is "talking" at the same time.
I hoped that this issue becomes solved over time, but the modules that I bought recently (with AT Firmware 1.4) are still not better. However, I did not notice any unexpected auto-reset or hangup anymore in my 2-Week test loop. At least that became better.
-
The issue has been solved since SDK 1.5.4, which is used by the Arduino ESP8266 core version 2.3.0.Stefan– Stefan2020年02月06日 16:27:28 +00:00Commented Feb 6, 2020 at 16:27