sntp: how to know if the time sync is done properly
sntp: how to know if the time sync is done properly
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!
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!
-
- Posts: 94
- Joined: Tue Sep 07, 2021 12:07 pm
Re: sntp: how to know if the time sync is done properly
I'm pretty sure the callback only happens when the time has been correctly parsed from the time server.
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
sntp_set_time_sync_notification_cb(time_sync_notification_cb);
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
Re: sntp: how to know if the time sync is done properly
Thanks for the reply,
However, the callback is called regardless of the time sync. Check the below logs for the reference.
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.
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
-
- Posts: 94
- Joined: Tue Sep 07, 2021 12:07 pm
Re: sntp: how to know if the time sync is done properly
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:
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!
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
Is there a better way other than this one to achieve the re-sync?
Thanks!
-
- Posts: 94
- Joined: Tue Sep 07, 2021 12:07 pm
Re: sntp: how to know if the time sync is done properly
I don't call sntp_sync_time directly. And I don't have my own function by that name.
All I do is this:
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.
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();
Craige
Re: sntp: how to know if the time sync is done properly
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.
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.
Re: sntp: how to know if the time sync is done properly
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.
Re: sntp: how to know if the time sync is done properly
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?
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?
Re: sntp: how to know if the time sync is done properly
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: Baidu [Spider], sirOwlBeak and 156 guests