I am very new to using the NodeMCU ESP8266 12E module. I am using it on the arduino-IDE. I have a simple sketch that controls an LED over a local network. It works fine when I run the server on the device. However, after about 10 min the browser cannot communicate anymore with the device, it seems like the server closed. I tried flashing the module following this video because I read that i should try updating the firmware. I flashed to nodemcu_float_0.9.6-dev_20150704. Still the problem persists.
Here is the code.
#include <ESP8266WiFi.h> const char* ssid = "mynetwork"; const char* password = "mypassword"; int ledPin = 13; WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); server.begin(); Serial.println("Server started"); Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } void loop() { WiFiClient client = server.available(); if (!client) { return; } Serial.println("new client"); while(!client.available()){ delay(1); } String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); int value = LOW; if (request.indexOf("/LED=ON") != -1) { digitalWrite(ledPin, HIGH); value = HIGH; } if (request.indexOf("/LED=OFF") != -1) { digitalWrite(ledPin, LOW); value = LOW; } // Return the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.print("Led pin is now: "); if(value == HIGH) { client.print("On"); } else { client.print("Off"); } client.println("<br><br>"); client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>"); client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />"); client.println("</html>"); delay(1); Serial.println("Client disonnected"); Serial.println(""); } Why is it timing out or closing abruptly? I want the server running 24/7 and my plan is to build an android app to turn on/off fans,lights,pumps etc over the internet.
Please advise.