1
votes

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.

1
0.9.6 is old and buggy. Use the firmware from nodemcu-build.comgre_gor
Thanks for the link...its quite a task choosing the modules, particularly since I am not clear on all of them.Faiyet
Flashing the NodeMCU firmware won't do any good for you - You aren't using their firmware when you use the Arduino SDK. In fact, every time you flash your code above, it will erase any firmware on the chip.Dawn Minion

1 Answers

1
votes

Your code only keeps WiFiClient in scope for the duration it takes to run the final delay/Serial prints - After that, WiFiClient will be destroyed, and no data is guaranteed to make it to the client. Now, this seems to work fine for you for the first ten minutes, but, I wouldn't expect this to always be the case.

Additionally, can you enable debug logging and post the results here? The SDK has the function Serial.setDebugOutput(true/false) to set that. It will include additional information that may help.

Lastly, have you considered using the ESP8266WebServer classes included with the ESP Arduino SDK? It already implements a well tested and functional web server, which seems to be exactly what you want, and may save you frustration developing your own.