SNTP sync when connected to wifi

CodingArco
Posts: 2
Joined: Thu Oct 26, 2023 5:23 pm

SNTP sync when connected to wifi

Postby CodingArco » Wed Jul 10, 2024 12:10 pm

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!

Who is online

Users browsing this forum: No registered users and 95 guests