-->
Page 1 of 2

Limiting Wifi connection attempts

PostPosted: Wed Jul 19, 2017 5:03 am
by timj_1
Hi there

I wonder if anyone would be kind enough to help me with a small problem. I have built and designed a small circuit that uses the esp8266 to send data to Thingspeak.com. It works brilliantly except for one small or big flaw. It is battery operated and uses deep sleep to save power, however it drains the battery when the internet connection is down or fails to connect by constant repeated attempts to connect.
I wish to limit the connection attempts to say 3 goes and then simply miss the current reading and go to deep sleep as would happen normally.

My code is the standard connection routine

WiFi.disconnect();
WiFi.begin("VM172234-2G","Pasword");
while ((!(WiFi.status() == WL_CONNECTED))){
delay(300); }

I am sure a simple and elegant solution is easy

Thanks in advance

Tim

Re: Limiting Wifi connection attempts

PostPosted: Thu Jul 20, 2017 8:54 am
by btidey
initialise a variable attempts = 0 before the while loop, increment attempts inside the loop and change the condition to terminate if either status is OK or attempts > 3

Re: Limiting Wifi connection attempts

PostPosted: Thu Jul 20, 2017 9:03 am
by timj_1
Many thanks - a version of this seems to work fine :)

Re: Limiting Wifi connection attempts

PostPosted: Thu Jul 20, 2017 1:06 pm
by schufti
I'd think one thread should be enough for that minimalistic problem ...
viewtopic.php?f=160&t=15511