So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By timj_1
#68346 I wonder if anyone would be kind enough to help a newbie

My esp8266 is battery powered and sends data to Thingspeak every 15 mins and then goes to deep sleep. It works great but the battery life is way short of my calcs. The problem seems to be that every now and then the router/internet is down and then the battery is drained by repeated connection attempts.

What I would like is to limit the connection attempts to say 3 and then skip to deep sleep as normal as if everything had worked.

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

At the mo have bog standard connection routine as above.

Can anyone advise of a simple and elegant solution?

TIA

Tim
User avatar
By schufti
#68360 Hi,
its probly not elegant but functional
Code: Select all 
wifi_on();
// wait for connect
int cc=0;
  while ((WiFi.status() != WL_CONNECTED) && (cc < 20)) {
#if DEBUG
    Serial.print(WiFi.status());
#endif
    delay(500);
    ++cc;
  }
  if (cc > 19) {
#if DEBUG
    Serial.println(" WiFi_timeout!");
#endif
  wifi_off();
 }
User avatar
By timj_1
#68376 Hi Schufti

Many thanks for your kind reply - I am unsure what the #debug is doing but a modified version of your code seems to work fine. Had to put a bigger delay in after wifi connect to allow it to establish connection otherwise keep getting returned a 6 from wifistatus - tried it with router off and it seems to skip to sleep as i wanted - again many thanks

Tim