Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By wjburl
#81838 I am using the ESP8266 in a digital clock project. The problem I have is that my ISP occasionally loses internet connection. The program sees it is still connected to the WiFi, but when it tries to retrieve the time from the internet, it loops until the ISP reestablishes internet connection. That could be several minutes. What code should I use to see if I have an internet connection? This is my current code"

if (WiFi.status() == WL_CONNECTED){
do {
// U.S. Central Daylight Time. Adjust for DST
dateTime = NTPch.getNTPtime(TimeZone, 2); // Query NTP
} while (!dateTime.valid);

hours = dateTime.hour;
minutes = dateTime.minute;
seconds = dateTime.second;
}
User avatar
By btidey
#81849 Don't put code in a loop that potentially never terminates.

Normally NTP is used is used to sync the internal clock at intervals so that any drift is insignificant. Missing out on a sync is no big deal it just tries again either sometime later (without stopping the rest of the code or on the next sync interval.

Some of the NTP clients will do this all for you. You just set the sync interval and let it get on with it.
User avatar
By wjburl
#81909 My program checks NTP every 30 seconds. If the call is successful, it updates the program's time. If it's not successful, it tries again one second later. I have a 1000 msec delay in my loop. I realize that the overhead makes the check a little longer than a second and it will drift if the connection is down for a while. I haven't tried the time the loop. I'm not sure how to do that. Also if the program unsuccessfully tries to connect to NTP, that will add to the loop time. The counter I use to see if has looped 30 seconds is a long int that only gets reset on a successful call to NTP. I did reboot my router while the program was running and it hadn't drifted during the 3 minutes the connection was lost.