ESP8266 WiFiServer Connection Issues

I've been having this problem in which I have an ESP WiFiServer which usually works perfectly once the ESP starts up, but after about an hour or two of letting it go, if I try to reconnect to make another request, the webserver seems to no longer exist and I can't get any connection. Also, the reason I say usually is because sometimes I have to restart the ESP multiple times to be able to connect in the first place. I will restart it, see in the console that it connected to my WiFi network and has its own IP, but then I can't connect to it.

Does anyone know what could be causing these connection issues? I have included the server-related parts of my code below.

IPAddress staticIP(192,168,0,85);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

WiFiServer server(80);

String header;

String valueString = String(5);
int pos1=0;
int pos2=0;
String curmode = "solid";

void setup() {
  ESP.eraseConfig();
  WiFi.disconnect();
  WiFi.setAutoConnect(false);
  WiFi.setAutoReconnect(false);
  WiFi.softAP(ssid);
  Serial.begin(115200);
  
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(blue,OUTPUT);

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(staticIP,gateway,subnet);
  WiFi.begin(ssid, pwd);
  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP Address: ");
  Serial.println(WiFi.localIP());
  
  server.begin();
}
void loop() {
  WiFiClient client = server.available();
  if(client){
    Serial.println("New Client.");
    String currentLine = "";
    while(client.connected()){
      if(client.available()){
        char c = client.read();
        Serial.write(c);
        header += c;
        if(c=='\n'){
          if(currentLine.length() == 0){
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            //The HTML/CSS code for the webpage is printed to the client here. After which point the header is interpreted by the program in some other unrelated code.

            client.println();
            break;
          } else{
            currentLine = "";
          }
        }else if(c!='\r'){
          currentLine += c;
        }
      }
    }
    header = "";
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

How are you powering the ESP8266 module?

It is powered from a DC wall adapter supplying 20v through a 6v buck converter to the VIN of the ESP. The reason for this is because the DC wall adapter is also powering a common-ground LED strip whose grounds must go through MOSFETS which are controlled by the ESP's pins, so everything has to be on the same ground.

Isn't ESP8266 a 3.3V IC?

I have NodeMCU which has a 3.3v Voltage Regulator which can handle up to 12v or so. I don't think power is the problem

aidanferry02:
I have NodeMCU which has a 3.3v Voltage Regulator which can handle up to 12v or so.

But not the power/heat associated with dropping 8.7volt@100mA.

6volt>3.3volt@0.1A= 0.27watt into the regulator.
It could be power-cycling when the regulator gets too hot (depending on ambient temp).

If you can't change that 6volt buck to 5volt, then try a 1N4004 diode in series with that supply.
Could also use a 5volt cellphone charger, connected to the USB socket.
Leo..

"Does anyone know what could be causing these connection issues?"

How does it work if you let the router assign it an IP address instead of you making the assignment? I've tested a number of example codes and noticed at least one that seemed to require a physical board reset after a period of time. Below is some code that was posted somewhere that has a static IP setup. It seems to run for days without a reset. You might try it to see if you get a longer run.

//use in browser
//http://192.168.0.9/ledOn
// http://192.168.0.9/ledOff

// Import required libraries
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <Servo.h>

//Static IP address configuration
IPAddress staticIP(192, 168, 0, 9); //ESP static ip
IPAddress gateway(192, 168, 0, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(8, 8, 8, 8);  //DNS

const char* deviceName = "deviceName.com";

//On board LED Connected to GPIO2
#define LED 2 

// WiFi parameters
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxx";

////Declare a global object variable from the ESP8266WebServer class.
ESP8266WebServer server(80); //Server on port 80
Servo servo;

void handleLEDon() {
 Serial.println("LED on page");
 digitalWrite(LED,LOW); //LED is connected in reverse
 delay(20); // If the issue is caused by the watchdog timer, adding small delays fix the issue
 servo.write(0);
 server.send(200, "text/html", "LED is ON"); //Send ADC value only to client ajax request
}

void handleLEDoff() {
 Serial.println("LED off page");
 digitalWrite(LED,HIGH); //LED off
 delay(20); // If the issue is caused by the watchdog timer, adding small delays fix the issue
 servo.write(120);
 server.send(200, "text/html", "LED is OFF"); //Send ADC value only to client ajax request
}

void setup(void)
{
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output

  // Start Serial
  Serial.begin(115200);
  WiFi.disconnect();  //Prevent connecting to wifi based on previous configuration
 
  WiFi.hostname(deviceName);      // DHCP Hostname (useful for finding device for static lease)
  //WiFi.config(staticIP, subnet, gateway);
  WiFi.begin(ssid, password);

  WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only
 

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.on("/ledOn", handleLEDon); //as Per  <a href="ledOn">, Subroutine to be called
  server.on("/ledOff", handleLEDoff);

  server.begin();                  //Start server
  Serial.println("HTTP server started");

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

  servo.attach(14); //GPIO15 = D5
}

void loop() {
  server.handleClient();          //Handle client requests
  delay(20); // If the issue is caused by the watchdog timer, adding small delays fix the issue
}

zoomkat:
Below is some code that was posted somewhere that has a static IP setup.

When doing static IP , be sure to set the allowed IP address range accordingly so that the router does not assign another device the static IP being used by the ESP-01. I do this for a shared network printer.

.

Thank you everyone for your advice. I added a diode between the output of the buck converter and the VIN of the ESP to drop the voltage. I tried getting rid of the static IP restriction and using the ESPWebServer library instead, but with no luck.

However, I found a different library called ASyncWebServer (GitHub - me-no-dev/ESPAsyncWebServer: Async Web Server for ESP8266 and ESP32), which after about an hour of setting up seems to be working perfectly. I don't know if the problem will come back with this new library but if not, I definitely recommend it to anyone who may have had the same problem as me.