ESP8266 - Freezing/Not responding?
Posted: Tue Nov 27, 2018 11:31 am
Hello All,
I'm trying to set up an ESP8266-ESP01 to be able to network my Uno. I have the Uno already set up and it is reading/validating several RC522 RFID readers. Its putting all the information into an array and is running fine.
I'm now trying to set up the ESP8266 to communicate with the Uno via Serial (SPI is out of the question due to the RC522s). However, before I'm able to get too far into it, Im having an issue with the ESP01 freezing.
Specifically, it looks like its able to send the string with the HTML code once or twice and then the ESP01 accepts the connection from the web browser on my laptop, but it does not recieve any data from the browser. At that point, the webpage is blank because its waiting for a response from the ESP01, and the ESP01 seems to be waiting for data from the browser/client.
The sketch seems to have plenty of memory when I upload the code below ( only uses 53% of flash and 38% of RAM) and I have tried this code on another ESP01 and a ESP8266 LOLIN D1 Mini, with similar issues. I ping the ESP01 from my laptop and although there is 0% loss, it does take about 200ms for packets to be sent (which would be fine for me. Minor latency is a non-issue). The ESP01 module connects immediately to WiFi as well, and signal stregth should not be an issue as the ESP01 can run all the example sketches perfectly fine.
I have some debug messages below, but can't seem to make sense of what may be causing this. I do still have to add some string manipulation and software serial stuff, but I'm not able to continue testing my code because I'm snagged up on this.
Any help would be greatly appreciated. I'm at my wits end here.
Debug Messages From Serial Monitor:
Connecting to myssid
.
Connected, IP address: 10.0.1.200
Client has connected
Client has sent Data
Message retained: GET / HTTP/1.1
Client disconnected
Client has connected
Client has sent Data
Message retained: GET / HTTP/1.1
Client disconnected
Client has connected
I'm trying to set up an ESP8266-ESP01 to be able to network my Uno. I have the Uno already set up and it is reading/validating several RC522 RFID readers. Its putting all the information into an array and is running fine.
I'm now trying to set up the ESP8266 to communicate with the Uno via Serial (SPI is out of the question due to the RC522s). However, before I'm able to get too far into it, Im having an issue with the ESP01 freezing.
Specifically, it looks like its able to send the string with the HTML code once or twice and then the ESP01 accepts the connection from the web browser on my laptop, but it does not recieve any data from the browser. At that point, the webpage is blank because its waiting for a response from the ESP01, and the ESP01 seems to be waiting for data from the browser/client.
The sketch seems to have plenty of memory when I upload the code below ( only uses 53% of flash and 38% of RAM) and I have tried this code on another ESP01 and a ESP8266 LOLIN D1 Mini, with similar issues. I ping the ESP01 from my laptop and although there is 0% loss, it does take about 200ms for packets to be sent (which would be fine for me. Minor latency is a non-issue). The ESP01 module connects immediately to WiFi as well, and signal stregth should not be an issue as the ESP01 can run all the example sketches perfectly fine.
I have some debug messages below, but can't seem to make sense of what may be causing this. I do still have to add some string manipulation and software serial stuff, but I'm not able to continue testing my code because I'm snagged up on this.
Any help would be greatly appreciated. I'm at my wits end here.
Debug Messages From Serial Monitor:
Connecting to myssid
.
Connected, IP address: 10.0.1.200
Client has connected
Client has sent Data
Message retained: GET / HTTP/1.1
Client disconnected
Client has connected
Client has sent Data
Message retained: GET / HTTP/1.1
Client disconnected
Client has connected
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define DEBUG
const char* ssid = "myssid";
const char* password = "password";
IPAddress staticIP(10, 0, 1, 200);
IPAddress gateway(10, 0, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(80);
void setup() {
Serial.begin(9600);
Serial.println();
WiFi.mode(WIFI_STA);
#ifdef DEBUG
Serial.printf("Connecting to %s\n", ssid);
#endif
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
#ifdef DEBUG
Serial.print(".");
#endif
}
#ifdef DEBUG
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
#endif
server.begin();
}
void loop() {
//Create a new instance of wifi client every iteration.
WiFiClient client = server.available();
if (!client) {
return;
}
#ifdef DEBUG
Serial.println("Client has connected");
#endif
//Waiting on client to send information - It seems this is where its looping infinitely.
while (!client.available()) {
delay(1);
}
#ifdef DEBUG
Serial.println("Client has sent Data");
#endif
//Take in only the first line
String messageIn = client.readStringUntil('\r');
#ifdef DEBUG
Serial.print("Message retained: ");
Serial.println(messageIn);
#endif
//erase everything else in the buffer
client.flush();
//Make sure we are dealing with the right message
//by confirming the first few characters
if (messageIn.indexOf("GET /?") == 0) {
//Get rid of "GET /?". Starts at index 0 and
//removes the next 5 character. Leaves string in place
messageIn.remove(0, 6);
//Get rid of second question mark
messageIn.remove(messageIn.indexOf("?"));
//Make sure its not longer than what we are expecting.
if (messageIn.length() < 5) {
Serial.println(messageIn);
}
else {
#ifdef DEBUG
Serial.print("Message longer than expected. Something is wrong. Message is: ");
Serial.println(messageIn);
#endif
}
}
////////////////////////////////////////
// Prepare the response //
////////////////////////////////////////
String s = "HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"<meta http-equiv='refresh' content='2; url =/home'/>"
"<title>MorseCode Broadcaster Board</title>"
"</br>"
"<b>"
"<div align='center'>"
"<p><h1>RFID Readers</h1></p>"
"<p>Status: readerStatus</p>"
"<input type='button' onclick=\"location.href='/?FWALL?'\" value='Force Win' /> "
" <input type='button' onclick=\"location.href='/?RESET?'\" value='Reset' /></div>"
"<div align='center'>"
"<table border='1' style='border-collapse: collapse; width: 99%; height: 54px'>"
"<tbody>"
"<div align='center'>"
"<tr style='height: 20px;'>"
"<td style='width: 11.1%; '>Reader 1</td>"
"<td style='width: 11.1%;'>Reader 2</td>"
"<td style='width: 11.1%;'>Reader 3</td>"
"<td style='width: 11.1%; '>Reader 4</td>"
"<td style='width: 11.1%;'>Reader 5</td>"
"<td style='width: 11.1%; '>Reader 6</td>"
"<td style='width: 11.1%; '>Reader 7</td>"
"<td style='width: 11.1%;'>Reader 8</td>"
"<td style='width: 11.1%;'>Reader 9</td>"
"</tr>"
"</b>"
"<tr style='height: 18px;'>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR1?'\" value='Reader 1' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR2?'\" value='Reader 2' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR3?'\" value='Reader 3' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR4?'\" value='Reader 4' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR5?'\" value='Reader 5' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR6?'\" value='Reader 6' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR7?'\" value='Reader 7' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR8?'\" value='Reader 8' /></td>"
"<td style='width: 11.1%;'><input type='button' onclick=\"location.href='/?FWR9?'\" value='Reader 9' /></td>"
"</tr>"
"<tr style='height: 18px;'>"
"<td style='width: 11.1%; background-color: cph1;'>sR1</td>"
"<td style='width: 11.1%; background-color: cph2;'>sR2</td>"
"<td style='width: 11.1%; background-color: cph3;'>sR3</td>"
"<td style='width: 11.1%; background-color: cph4;'>sR4</td>"
"<td style='width: 11.1%; background-color: cph5;'>sR5</td>"
"<td style='width: 11.1%; background-color: cph6;'>sR6</td>"
"<td style='width: 11.1%; background-color: cph7;'>sR7</td>"
"<td style='width: 11.1%; background-color: cph8;'>sR8</td>"
"<td style='width: 11.1%; background-color: cph9;'>sR9</td>"
"</tr> </tbody> </div> </table> </div>";
//Sends string, gives it some time and disconnect.
client.print(s);
delay(1);
client.stop();
#ifdef DEBUG
Serial.println("Client disconnected");
#endif
}