Webserver - how to make it failsafe ?

Hi,

I built an ESP8266 webserver that replys his WLAN-Signalstrength to each HTTP-Requests it gets.

For the webserver I used the example-code:

  Serial.print("New client connected   ");
  while(!client.available()){
    delay(1);
  }
client.flush();

Seldom but regularily it happens that the webserver gets stuck in this infinite loop of delay(1) and then I have to dis- and reconnect the whole chip.

Is there another, better way to make it failsafe ? Make a timeout after lets say 5 seconds of something ?

Can someone tell me what client.flush() and client.stop() does ?

BR
Gawan

Can someone tell me what client.flush() and client.stop() does ?

Why not look at the source code, and find out for yourself?

Gawan:
Is there another, better way to make it failsafe ?

You can easily add a timeout to your WHILE loop using millis() - something like this

start = millis();
while(!client.available()){
    delay(1);
    if (millis() - start >= timeoutMillis) {
        break;
    }
}

...R

ok, thanks a lot

is it sufficient to jump out with "break", or is it better to combine client.stop() and break ?

BR
Gawan

Gawan:
is it sufficient to jump out with "break", or is it better to combine client.stop() and break ?

That depends on what you want to achieve.

Before break, you could set some flag variable to record the fact that the connection attempt failed and use that value elsewhere in your program.

...R

If the connection attempt failed (if I run into a timeout) I would like to leave the loop and start everything from the beginning.
Does "break" implicitly close all client connections ?

Does "break" implicitly close all client connections ?

No. All it does is jump out of the while loop.

I see ... what do I have to do to quit the current loop() and start the next one ?
To be on the safe side I'd prefer to do a full "restart" .

I have code that is as failsafe as it can be, but it is designed for the w5100. I don't know if the ESP8266 has the same weaknesses. You might need to test it to see. The w5100 can fail if clients connect without sending anything, then not send a disconnect message. It uses all the sockets. Post scanning apps sometimes do this.

My server code on the playground has a checkSockStatus function that checks and releases the sockets if they are frozen by a rogue app. Maybe some of the features I have incorporated into my code can be adapted for use with the ESP8266.
http://playground.arduino.cc/Code/WebServerST

what esp8266 library are you using?

last I checked (which is a while back), I did not find any esp8266 library that implements the Client and Server classes.

its name is #include <ESP8266WiFi.h>

Gawan:
If the connection attempt failed (if I run into a timeout) I would like to leave the loop and start everything from the beginning.

The reason I said "That depends on what you want to achieve" in Reply #4 is that it does not seem to make any sense to start over if the connection failed - won't it just fail again?

And if you just want to keep trying why not make the timeout period longer?

...R

My HTTP-Response is refreshing the page on a regular bases
--> meta http-equiv='refresh' content='60'

For any reason it works for minutes, but suddenly it fails ... it simply gets stuck here:

while(!client.available()){
delay(1);

I do not see a reason so far.
As soon as I reset the whole thing it works perfectly fine again ... for minutes.
I'd like to do the manual resetting in my code and I expect this to work if I simply re-run the loop from the beginning

and I expect this to work if I simply re-run the loop from the beginning

Breaking out of the while loop will cause loop() to be called again. Expecting that to clear the problem is unreasonable.

My assumption is that at the end of loop() all connections/objects that were created inside loop() (e.g. WiFi client;) are stopped/killed/resetted.

Right ?

... or is it better to use a watchdog here ?

Right ?

No.

Gawan:
its name is #include <ESP8266WiFi.h>

ok, I think you are not using any arduino hardware, but only esp8266 module that uses arduino ide.
you will get better help if you contact the author of that library directly.

I am using the NodeMCU hardware

Gawan:
I am using the NodeMCU hardware

Post a link to its datasheet.

It's a pity you waited until Reply #17 to tell us you are not using a regular Arduino with an ESP8266 attached.

...R