Page 1 of 1

How to get and sync time with sntp?

Posted: Mon Jan 06, 2020 5:53 am
by zliudr
I'm not sure where in azure or outside of it the sntp is called but here is a snippet of my boot message that includes the announcement that time was obtained:
I (4660) event: sta ip: 192.168.0.103, mask: 255.255.255.0, gw: 192.168.0.1
I (4660) azure: SYSTEM_EVENT_STA_GOT_IP
I (4660) azure: Got IP: '192.168.0.103'
I (4670) azure: Connected to AP success!
Initializing SNTP
ESP platform sntp inited!
I (4680) platform: The current date/time is: Mon Jan 6 05:16:06 2020
So apparently only after wifi is connected the sntp was initialized. I check the code where wifi is started, there is no sntp call. It calls to start azure client, which calls a function to start connection to azure server. I bet that is where sntp got started. I did some digging but the azure stuff is jut too deep to find anything useful.

So essentially the sntp server has been possibly started by azure client. I don't know which code is using the "platform" tag.

I just wish to obtain epoch time. Is there a function I can call to get that?

A second question: once sntp server started, is there any need to call some function once in a while to synchronize or is done in the background?

I found this example but it is quite confusing. Not a hello world type of simplicity.

https://github.com/espressif/esp-idf/bl ... ple_main.c

Re: How to get and sync time with sntp?

Posted: Mon Jan 06, 2020 6:00 am
by zliudr
By the way, I heard that ESP32's RTC accuracy is horrible without external crystals. Is there any way to use DS3231 to substitute?

Re: How to get and sync time with sntp?

Posted: Mon Jan 06, 2020 7:52 am
by axellin
zliudr wrote:
Mon Jan 06, 2020 5:53 am

So apparently only after wifi is connected the sntp was initialized. I check the code where wifi is started, there is no sntp call. It calls to start azure client, which calls a function to start connection to azure server. I bet that is where sntp got started. I did some digging but the azure stuff is jut too deep to find anything useful.

So essentially the sntp server has been possibly started by azure client. I don't know which code is using the "platform" tag.

I just wish to obtain epoch time. Is there a function I can call to get that?

A second question: once sntp server started, is there any need to call some function once in a while to synchronize or is done in the background?
FYI:
https://github.com/espressif/esp-idf/issues/4437

Currently you cannot change NTP sync interval at runtime, it is determinated by
CONFIG_LWIP_SNTP_UPDATE_DELAY at compile time.
And there is no api to allow you to send NTP sync request immediately.

But you can use sntp_set_time_sync_notification_cb() to get notify about
the time synchronization process.

Re: How to get and sync time with sntp?

Posted: Mon Jan 06, 2020 7:15 pm
by zliudr
Thanks! From my observation of 12 hours, time ran faster about 4.5 minutes since sntp was called. This was the sntp_example_main.c from esp-idf examples. The sntp was initialized but not updated. So the CONFIG_LWIP_SNTP_UPDATE_DELAY you were referring to, is it in menuconfig? If the example never re-synced with sntp, or at least not within reasonable time, then how is the CONFIG_LWIP_SNTP_UPDATE_DELAY going to improve it?

I have a ds3231 integrated on my prototype board. Is there any way to have esp32 use it instead of its rtc with internal rc oscillator reference?