Page 1 of 1

SNTP sync when connected to wifi

Posted: Wed Jul 10, 2024 12:10 pm
by CodingArco
To sync the time I'm using the SNTP time synchronization (https://docs.espressif.com/projects/esp ... ronization).

This syncs the time every CONFIG_LWIP_SNTP_UPDATE_DELAY ms. However, if there is no network (wifi) connection, it doesn't sync the time, which is understandable. However, it also doesn't sync the time when the wifi connects again (see example below for explanation).

Example:
Wifi is turned on on bootup and time is synced. (time 0 minutes)
At time = 5 minutes the wifi is turned off.
At time = 60 minutes the SNTP tries to sync the time, but the wifi is off, so no sync happens.
At time = 65 minutes, the wifi is turned on. At this moment, I'd like the time to be synced, because it missed the previous sync (at the 60 minute mark).
At time = 70 minutes, the wifi is turned off.
At time = 120, the SNTP tries to sync, but can't sync again because the wifi is off.
etc...

Is it possible to sync the time when the wifi is turned off and longer than CONFIG_LWIP_SNTP_UPDATE_DELAY passed since the last sync? Or do I have to manually check that, e.g. on the WIFI_EVENT_STA_CONNECTED event and then manually sync the time. And if so, how can I force an SNTP sync to happen?

Thanks in advance!