ESP8266 web server : no responding after few minutes

Hi,

I am trying to pilot relay and get temperature with ESP8266. I power it with voltage regulator. After few minutes/hours (no rules), no response by web interface.

My code :

#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);

#define RELAYPINA 4
#define RELAYPINB 5

// NETWORK: Static IP details...
IPAddress ip(192, 168, 1, 85);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
const char* ssid = "aaa";
const char* password = "aaa";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);


void setup() {
  Serial.begin(115200);
  delay(10);
  dht.begin();
  
  // prepare RELAYPINA
  pinMode(RELAYPINA, OUTPUT);
  digitalWrite(RELAYPINA, 0);
  pinMode(RELAYPINB, OUTPUT);
  digitalWrite(RELAYPINB, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());


}

int val_relay_a=0;
int val_relay_b=0;
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";

  
  // Match the request
  if (req.indexOf("/gpioA/0") != -1)    val_relay_a = 0;
  else if (req.indexOf("/gpioA/1") != -1)    val_relay_a = 1;
  else if (req.indexOf("/gpioB/0") != -1)    val_relay_b = 0;
  else if (req.indexOf("/gpioB/1") != -1)    val_relay_b = 1;  
  else {
    //s += req;
    Serial.println("invalid request");
  }

  // Set RELAYPINA according to the request
  digitalWrite(RELAYPINA, val_relay_a);
  digitalWrite(RELAYPINB, val_relay_b);


  // Prepare the response
  s += "<p>\r\nGPIO A is now ";
  s += (val_relay_a)?"high":"low";
  s += "</p><p><a href='/gpioA/0'>go low</a> - <a href='/gpioA/1'>go high</a></p>";
  s += "<p>\r\nGPIO B is now ";
  s += (val_relay_b)?"high":"low";
  s += "</p><p><a href='/gpioB/0'>go low</a> - <a href='/gpioB/1'>go high</a></p>";
  s += "<p>Temperature : ";
  s += t;
  s += "<p>Humidity : ";
  s += h;
  s += "</p></html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

In my webserver, I hold the reference to the client until it is in connected() state and if it is not connected, I call client.stop().

in esp8266 2.4.1 and older is a memory leak with WiFiClient object even if you call stop(), not all memory is returned. take the stop() implementation from source code on GitHub and patch your installation

Thanks for the answer. I will try it!

I found a complementary solution here : Memory leak ESP8266WebServer · Issue #230 · esp8266/Arduino · GitHub

Ju-de-tomate:
I found a complementary solution here : Memory leak ESP8266WebServer · Issue #230 · esp8266/Arduino · GitHub

what solution?

Hi,
I'm new to ESP8266.
trying to use the webserver.
the server stops while the ESP is still alive (verifying it by blinking external led).
Looks like I have the same problem, but couldn't find the solution in the link to the Github.
Please help.
Thx,
Schlumm