1

I'm new to NodeMCU and ESP. I've created a basic web server on my NodeMCU.

The server works fine for the first few minutes after resetting the board or re-uploading code. But after about half an hour, the server just stops responding and becomes unreachable. Funny thing is, after sending a few failed requests, server starts to respond normally again.

The code i'm using:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
 
String wifiName = "blablabla";
String password = "blablabla";

ESP8266WebServer server(9090);

void setup() {
  Serial.begin(115200);
  pinMode(5, OUTPUT);
  digitalWrite(5,LOW);
  
  connectToWifi();
  server.on("/", handleHomepage);
  server.on("/action", handleAction);
  server.begin();
}

void loop() {
  server.handleClient();
}

void handleHomepage() {
  server.send(200, "text/plain", "Welcome to esp8266 homepage!");
}

void handleAction() {
  String parameter = server.arg("value");

  if (parameter == "") {
    server.send(200, "text/plain", "No parameter supplied.");
    return;
  } else {
      if (parameter == "on") {
        digitalWrite(5,HIGH);
      } else if (parameter == "off") {
        digitalWrite(5,LOW);
      }
    server.send(200, "text/plain", "Action received successfully.");
  }
}

void connectToWifi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(wifiName, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

What should I do to keep the server always active? Thanks in advance.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.