Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By lmerega
#55910 Hi all, I have a simple web server on my Nodemcu
The problem is that I have to reset the board every 3 minutes because the web server stops working.
I do not kno why... I can use it for 2 or 3 minutes... then it stops.
Any sugestion?

I attach my code:


#include <ESP8266WiFi.h>
#include <TimeLib.h>

const char* ssid = "***********************";
const char* password = "********************";

int ledPin = 14; // GPIO14
int myport = 80;
String actionToDo = "";

WiFiServer server(myport);

void setup() {

Serial.begin(115200);
delay(10);

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

// Connect to WiFi network
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");

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

// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.print(myport);
Serial.println("/");

}

void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (!client) {
if (now() >= 180) {
ESP.restart(); //why don't you work?!?
}
return;
}
Serial.println("new client");
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
if (request.indexOf("/WAKEUP") != -1) {
actionToDo = "WakeUp";
} else if (request.indexOf("/SHUTDOWN") != -1) {
actionToDo = "ShutDown";
}
Serial.println(actionToDo);

// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<br />");
client.println(actionToDo);
client.println(now());
client.println("<br />");
client.println("</html>");
// give the web browser time to receive the data
delay(1);
// close the connection:
Serial.println("client disconnected");

if (actionToDo == "WakeUp") {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
} else if (actionToDo == "ShutDown") {
digitalWrite(ledPin, HIGH);
delay(7000);
digitalWrite(ledPin, LOW);
}
User avatar
By mrburnette
#55915 1st: when yoh have code issues, go to a published example of same functionality; that is the webserver code example installed with ESP8266.

2nd: if you still have issues, the problem is hardware related. I would guess that 90% of all H/W issues is actually the power supply...

- Power supply needs to be both stable and lo-noise. With the NodeMCU, running from thd 5V USB may be an issue if the PC port is an older 1.1 spec port. Try a powered USB hub.

- IMO, NodeMCU USB port is for developing only. Switch to a quality 3.3V source after software is finalized.

A quality 3.3V source can be built from a good quality $1 buck DC-DC converter. Such as this example.

Do Not build out a power supply on breadboard... bad idea due to potential resistance and voltage sag under load.


Ray
User avatar
By lmerega
#55917 1 - Thanks for the info! Very, veru useful.
2 - I do not think it is code related... as I can read from your post, I agree it could be HW realated.
Infact my NodeMCU is powered by a USB HUB, but I have ground problems, in fact I get a little shock when I work on my board.
In my (very old) house theres isn't any ground, so there is a lot of dispersion all over.
So I agree with you... there is only one left strange fact:
I print on the serial "foo" every 60 seconds.
In this way it works without any problem.
If I leave the board in waiting mode... it hangs... this should exclude HW issues... or not?

TIA

.luca
User avatar
By mrburnette
#55938
I print on the serial "foo" every 60 seconds.
In this way it works without any problem.
If I leave the board in waiting mode... it hangs... this should exclude HW issues... or not?


If you are receiving 'electrical shocks', you have a very serious health issue.

The above being said, if you can loop code consistently but have issues with what are called "blocking instructions" then you need to use non-blocking instructions or design your code to use delay(0) or yield() at least every 50mS... delays beyond that can cause the RF section to malfunction and eventually could cause a WDT reset.

Ray