ESP32 Webserver disconnects spontaneously

I modified an example from here so it works with Serial.print instead of leds. It shows the date you just have put in on screen.

The problem is, this works fine for a short period of time. After a few entries the connection is closed and it stops working.

I get this behavior also with another (brand new) ESP32 board and also when I connect with my phone instead of my laptop.

I don’t get any error messages but the serial monitor outputs a few lines of random characters, like this:

Client Disconnected.
New Client.
GET /favicon.ico HTTP/1.1
Host: www.msftconnecttest.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://www.msftconnecttest.com/redirect?BDform=2019-01-16&custId=birthdayBIRTHDAY RECEIVED2019-01-16
BIRTHDAY RECEIVED2019-01-16
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,nl;q=0.8,de;q=0.7

Client Disconnected.
New Client.
}RP⸮⸮⸮⸮⸮խ4⸮⸮⸮⸮⸮gReR"P⸮3砓0s⸮⸮⸮< etc. etc.

This is my code:

#include <WiFi.h>
#include <DNSServer.h>
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  Serial.print("I'm alive"); 
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP("DNSServer CaptivePortal example");
  dnsServer.start(DNS_PORT, "*", apIP);
  server.begin();
}

void loop() {
  String datum;
  dnsServer.processNextRequest();
  WiFiClient client = server.available();   // listen for incoming clients
  if (client) {    // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.print(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("<form>  Birthday:  <input type=\"date\" name=\"BDform\">  <input type=\"submit\"> <input type=\"hidden\"  name=\"custId\" value=\"birthday\"> </form>");
            client.print("Click <a href=\"/L\">here</a> to turn the QED on pin 5 off.
");
            client.println(datum);
            // The HTTP response ends with another blank line:
            client.println();
          
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          Serial.print("GET H");            // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
           Serial.print("GET L");                 // GET /L turns the LED off
        }

if (currentLine.endsWith("birthday")) {
          Serial.print("BIRTHDAY RECEIVED");  
          int place = currentLine.indexOf('BDform=');
          datum= currentLine.substring(place+1, place+11);
          Serial.print(datum);
         
      }     
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

I’m puzzled because it works and then it somehow stops working. (I’m more used to code that doesn’t work at all :slight_smile: )

It was probably Windows. Restarting helped. :) :angry: