4th Update:
I bought an ESP32 (the successor to ESP8266) and am getting the same problem. I used the same code except used the Wifi.h
library instead, also removed the ESP8266WiFiGratuitous.h
code. I added an LED flash at the start of the loop and can confirm the LED stops flashing, so I can confirm the loop()
stops running.
This makes me think it must be something to do with my WIFI network.
3rd update: I've removed all the code and instead uploaded a simple onboard LED blink sketch. The LED has been blinking for over 24 hrs which leads me to think there is something in the wifi code that is making the software crash.
2nd update:
I've even tried resetting the board at the start of the loop()
body, every 10 mins, using the millisDelay.h library, calling this method to reset:
void(* resetFunc) (void) = 0;
but that doesn't help either. The loop()
method seems to completey stop running.
Update:
I've added experimental::ESP8266WiFiGratuitous::stationKeepAliveSetIntervalMs();
but am still failing to get a response after 1hr, with about 15 requests made in that hour.
I have this code which works on my ESP8266:
#include <ESP8266WiFi.h>
#include <ESP8266WiFiGratuitous.h>
const size_t input_buffer_length = 256;
const char* ssid = "iiNetC2DAD7";
const char* wifiPassword = "aM9PrxcbtkS";
const char* passwordToOpenDoor = "/87"; //password should begin with a slash
const int doorPin = 5;
const int buzzerPin = 4;
const int toneDuration = 700;
int numOfRequests = 0;
WiFiServer server(301); //Pick any port number you like
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(WiFi.localIP());
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1);
pinMode(doorPin, OUTPUT);
digitalWrite(doorPin, 0);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, 0);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
experimental::ESP8266WiFiGratuitous::stationKeepAliveSetIntervalMs();
server.begin();
Serial.println("Server started.");
Serial.println(WiFi.localIP());
}
void loop() {
client = server.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
char request[input_buffer_length];
client.readBytesUntil('\r', request, input_buffer_length);
Serial.println(request);
if(strstr(request, passwordToOpenDoor)) {//Is password correct?
GenerateResponse("Password is correct");
OpenDoor();
CorrectPasswordSound();
}
//Got a GET request and it wasn't the favicon.ico request, must have been a bad password:
else if (!strstr(request, "favicon.ico")) {
GenerateResponse("Password is incorrect.");
}
}
void OpenDoor() {
digitalWrite(LED_BUILTIN, 0); //flash the onboard LED to help during testing.
digitalWrite(doorPin, 1);
delay(500);
digitalWrite(LED_BUILTIN, 1);
digitalWrite(doorPin, 0);
}
void GenerateResponse(const char *text) {
Serial.println(text);
client.print(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<!DOCTYPE HTML>\r\n"
"<html>\r\n"
"<br><h1><b>"
);
client.print(text);
client.print("</b></h1><br><h1><b>Num of requests: ");
client.print(++numOfRequests);
client.print("</b></h1></html>\r\n");
client.flush();
}
void CorrectPasswordSound() {
//Play 1700, 1800, 1900Hz
for(int i = 17; i < 20; i++){
int frequency = i * 100;
tone(buzzerPin, frequency);
delay(toneDuration);
noTone(buzzerPin);
}
}
However, after a random amount of time, the software crashes and the device won't respond to HTTP requests anymore.
I've tested this by printing to the serial monitor every loop. It seems as though it crashes and the
loop()
function stops running since it stops printing to the monitor.I noticed that it'll still reply to pings.
It won't reconnect after restarting the router, I need to press the reset button on the ESP8266 to get it to start working again.
I've tested it by sending an HTTP Get request every 5 mins for many hours (using a C# console app). Sometimes it failed after 45 mins, sometimes 7 hours.
I initially thought it was because I was using String objects, but after getting an answer to this question and testing it out with only C-strings, I still have the same problem.
I've been sending HTTP Get requests using Chrome on my phone and laptop, as well as the .NET Framework class
System.Net.Http.HttpClient
.
Spec:
- Board: AI-Thinker ESP8266MOD
- IDE: Arduino 1.8.12
- ESP8266 Boards version = 2.6.3
- IDE Settings:
I've tried resetting the connection using:
server.stop();
delay(1000);
server.begin();
Then this:
ESP.reset();
Then after those 2 didn't work, this:
ESP.restart();
Resetting it frequently did nothing.
Is there anything in my code that stands out? If there's nothing wrong withe code, then maybe my only option is to hard reset the board every 30 mins some how.
void GenerateResponse(WiFiClient& client, const char *text) {