sntp: how to know if the time sync is done properly

asurti
Posts: 7
Joined: Tue Sep 07, 2021 9:37 am

sntp: how to know if the time sync is done properly

Postby asurti » Tue Sep 07, 2021 9:45 am

Hello,

I am performing sntp sync. However, I need to know how I can be aware of the time sync not done?
When the device is failing to update the ntp time, it fetches the device time and sets the status as SNTP_SYNC_STATUS_COMPLETED.

I can check the timestamp which is updated and can compare it with the older time. But this is valid once per boot. As when I perform the sntp sync once again, and internet is not available it gets the device time instead of the ntp pool server time and also the status is SNTP_SYNC_STATUS_COMPLETED.

So, how we can know if the sntp lib is getting the network time or it is getting the device time which is previously set?

I am using esp-idf v4.3 - esp32s2 module.

Thanks!

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: sntp: how to know if the time sync is done properly

Postby Craige Hales » Wed Sep 08, 2021 12:51 pm

I'm pretty sure the callback only happens when the time has been correctly parsed from the time server.

Code: Select all

sntp_set_time_sync_notification_cb(time_sync_notification_cb);
Until the callback happens, assume the time has not been set. The callback will be called periodically. By default it seems to be once an hour. I increased the interval to two hours using

Code: Select all

printf("sntp_get_sync_interval %u\n",sntp_get_sync_interval());// 3600000, 1 hour
sntp_set_sync_interval(2*3600*1000); // 2 hours
printf("sntp_get_sync_interval %u\n",sntp_get_sync_interval());// 7200000
Craige

asurti
Posts: 7
Joined: Tue Sep 07, 2021 9:37 am

Re: sntp: how to know if the time sync is done properly

Postby asurti » Thu Sep 09, 2021 6:46 am

Thanks for the reply,
However, the callback is called regardless of the time sync. Check the below logs for the reference.

Code: Select all

I (7348) sntp.c: Waiting for system time to be set... (1/10)
I (9348) sntp.c: Waiting for system time to be set... (2/10)
I (11348) sntp.c: Waiting for system time to be set... (3/10)
I (13348) sntp.c: Waiting for system time to be set... (4/10)
I (15348) sntp.c: Waiting for system time to be set... (5/10)
I (17348) sntp.c: Waiting for system time to be set... (6/10)
I (19348) sntp.c: Waiting for system time to be set... (7/10)
I (21348) sntp.c: Waiting for system time to be set... (8/10)
I (23348) sntp.c: Waiting for system time to be set... (9/10)
I (25348) sntp.c: Waiting for system time to be set... (10/10)
I (27858) sntp.c: Notification of a time synchronization event
The device is not connected to the internet and after 10 retries, it expires and gets default time (1st Jan 1970) and also executes the callback.


asurti
Posts: 7
Joined: Tue Sep 07, 2021 9:37 am

Re: sntp: how to know if the time sync is done properly

Postby asurti » Mon Sep 13, 2021 8:07 am

Sure, thanks for the insight.

Got the point regarding the time sync first time.
The issue I am facing seems to be with the re-sync. What will be the steps when we do want to resync to the ntp server?
I am using "sntp_sync_time" API with default implementation, I have not overridden the API. But by using this, it is calling the callback function regardless of sntp sync status.
Below log is printed every time:

Code: Select all

I (26438) sntp.c: Notification of a time synchronization event
Was wondering if that is the correct way of re-sync the time on a single boot. We have a custom requirement to do time sync once again when the device is registered on the IoT cloud. So we are just checking if sntp is initialized or not, if it is then we are using the sntp_sync_time API to resync.
Is there a better way other than this one to achieve the re-sync?

Thanks!

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: sntp: how to know if the time sync is done properly

Postby Craige Hales » Tue Sep 14, 2021 1:57 pm

I don't call sntp_sync_time directly. And I don't have my own function by that name.

All I do is this:

Code: Select all

   
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, sClockState.cfgSntpUrl[0] ); // "pool.ntp.org"
    sntp_set_time_sync_notification_cb(time_sync_notification_cb);
    sntp_init();
I get the callback via my time_sync_notification_cb function, every 2 hours in my case. If I unplug the wifi, notifications stop until I plug it back in. I do have code that continually tries to reconnect to the wifi when the connection drops.
Craige

asurti
Posts: 7
Joined: Tue Sep 07, 2021 9:37 am

Re: sntp: how to know if the time sync is done properly

Postby asurti » Tue Sep 14, 2021 4:18 pm

Thanks for the reply!
Yes, that would be the fixed interval of time when the time gets synced and callback is triggered.
But how can I sync time at custom intervals i.e. suppose there is a event after which I need to re-sync the time regardless of the current drift. How can I achieve it? Which API would be helpful in that case?
I see sntp_request API, but that is static. Which I can not use in my code directly.

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: sntp: how to know if the time sync is done properly

Postby ESP_YJM » Wed Sep 15, 2021 6:50 am

If I understand correctly, you want to re-send the NTP synchronization request through the API sntp_request instead of internally using a fixed time interval for time synchronization. I think you can try the sntp_restart API.

asurti
Posts: 7
Joined: Tue Sep 07, 2021 9:37 am

Re: sntp: how to know if the time sync is done properly

Postby asurti » Wed Sep 15, 2021 7:21 am

Thanks for the reply,
But if we do sntp_restart does it deinit the sntp and initialize again?
Do we need to again set the operating mode and NTP servers? Or it just uses the previous configurations?

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: sntp: how to know if the time sync is done properly

Postby ESP_YJM » Wed Sep 15, 2021 7:44 am

Yes, it will only deinit sntp pcb and initialize sntp pcb again, but will not change configuration you set before, such as operating mode and NTP servers.

Who is online

Users browsing this forum: No registered users and 51 guests