-->
Page 1 of 1

ESP8266 not responding after random intervals of time

PostPosted: Thu May 07, 2020 7:24 pm
by DavidKlempfner
I have this code which works on my ESP8266:

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WiFiGratuitous.h>

const size_t input_buffer_length = 256;

const char* ssid = "myWifi";
const char* wifiPassword = "password123";
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.

Board: AI-Thinker ESP8266MOD
IDE: Arduino 1.8.12
ESP8266 Boards version = 2.6.3
IDE Settings:
Image

I've tried resetting the connection at regular intervals using:

Code: Select allserver.stop();
delay(1000);
server.begin();


Then this:

Code: Select allESP.reset();


Then after those 2 didn't work, this:

Code: Select allESP.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.