Add non-blocking delay to do... while loop

I’ve googled this and not found a workable solution - maybe it’s not possible??

I have a do… while loop which pings 2 IP’s to establish if I have a working internet connection. If so, the ESP goes to sleep for 15 mins, and then repeats.
If not, then after a number of further attempts, it will reboot my router. (I live in an area prone to internet outages, which usually can be restored by rebooting and picking up a different route to the exchange).

However, to ensure that my router is not rebooted after a temporary glitch, I would like to introduce a 2 minute delay between each iteration.
I considered adding delay(120000); after count++; but that may cause the wifi to drop out, and make the subsequent ping unreliable.

  do {
      if(Ping.ping(google)) {
      Serial.println("reply received OK from google");
      Serial.println("Going into deep sleep for 60 seconds"); //this will be 15 minutes after testing
      ESP.deepSleep(60e6); // 60e6 is 60 seconds
     } else if(Ping.ping(cloudflare)) {
        Serial.println("reply received OK from cloudflare");
        Serial.println("Going into deep sleep for 60 seconds");
        ESP.deepSleep(60e6); // 60e6 is 60 seconds
        //If no reply from either site, iterate the loop a further 4 times and then reboot router
         } else {
         count++;
         }
  } while (count < 5);

Hello, use millis(), as described in the pinned topic https://forum.arduino.cc/index.php?topic=503368.0

I use millis() regularly to get a non-blocking delay, and that was my first choice, but couldn't get it to work in the do... while loop. I tried several times but managed to either stop the iterations completely, or it was just ignored....

Any ideas how it could be structured please.

I'd rethink the entire approach and go with a State Machine implementation.

couldn't get it to work in the do... while loop

What is wrong with using the loop() function to test conditions during a period of time ?

millis() and loops are in general not the best of friends.

if((millis() - lastPingTime >= 120000) && (count < 5))
{
  lastPingTime = millis();
  
  if (Ping.ping(google))
  {
    Serial.println("reply received OK from google");
    Serial.println("Going into deep sleep for 60 seconds"); //this will be 15 minutes after testing
    ESP.deepSleep(60e6); // 60e6 is 60 seconds
  }
  else if (Ping.ping(cloudflare))
  {
    Serial.println("reply received OK from cloudflare");
    Serial.println("Going into deep sleep for 60 seconds");
    ESP.deepSleep(60e6); // 60e6 is 60 seconds
    //If no reply from either site, iterate the loop a further 4 times and then reboot router
  }
  else
  {
    count++;
  }
}

Call this repeatedly as usually done with millis() based approaches. If you need more help, you’ll need to post your original complete code.

//PS
not tested, not compiled

sterretje - that seems like a great solution! I'll redo my sketch along those lines.

Many thanks

Imagine asking this question for a "for" loop, or a "while" loop.

Same problem.