I've used the simpletime example to get time from a ntp server. It all works very well except that it is not possible to get accuracy better than a second. I can make repeated requests until the second changes, but that seems a waste. Should I make my own udp request to get at the full data packet returned, or is there a simpler way?
Would it be better to use the expression idf?
Thanks
Chris John
Time from ntp with milliseconds
-
lbernstone
- Posts: 1132
- Joined: Mon Jul 22, 2019 3:20 pm
Re: Time from ntp with milliseconds
Technically, you should remove half the travel time to get to the highest accuracy, but this gets you a lot closer than 1 sec.
https://wokwi.com/projects/440695449336485889
https://wokwi.com/projects/440695449336485889
Re: Time from ntp with milliseconds
Thanks very much. I only need the time for a clock that seems to be the same as a gps time from nmea decode, so better than 200 to 100 ms is OK. I don't know how much variation there is between the outgoing and incoming delays, but I would like to know. I have gps pps which I think is better than 1ms. I also have msf time which seems within about 30ms of gps pps time. It is difficult to know without access to a proper time source.
-
lbernstone
- Posts: 1132
- Joined: Mon Jul 22, 2019 3:20 pm
Re: Time from ntp with milliseconds
Don't lose track of accuracy vs precision. The accuracy here is very much going to depend on how quickly you can get the info from the source. NTP can be used to get pretty accurate, but there is some estimation in travel times. Read the RFC for the nitty gritty details on how you insert a source time into your outgoing packet to do the calculations to get as close to the time source as possible. GPS is going to be much more accurate- it is usually within 100ns if you have a few satellites tracking; the latency there is based on your baud rate, not some travel time out on the internet.
Nobody has really bothered to make a more precise library because these are $1 chips that are connected over WiFi. Any additional precision is swamped by the inherent inaccuracy of getting the time itself and the noisy onboard rtc.
Nobody has really bothered to make a more precise library because these are $1 chips that are connected over WiFi. Any additional precision is swamped by the inherent inaccuracy of getting the time itself and the noisy onboard rtc.
-
RandomInternetGuy
- Posts: 82
- Joined: Fri Aug 11, 2023 4:56 am
Re: Time from ntp with milliseconds
Why do Arduino users insist on recoding this or finding some (inevitably abandoned) library?
Configure the daemon (I think it's in LWIP, actually, but it's exposed via ESP-IDF) via esp_netif_sntp_init() and be done.
https://docs.espressif.com/projects/esp ... ronization
It's super simple, and the system lets the SNTP fetches run in the background while it's doing ARPs and other things, so it's low overhead. I don't think it even _really_ configures another daemon as much as it tells existing ones there's a hitchhiker.
When I tried it, it even did a sensible thing across low-power transitions, though I don't exercise that case on a regular basis.
Of course, https://en.cppreference.com/w/cpp/chron ... k/now.html and the rest of std::chrono works great and you never have to touch those 1970's-era ISO C time functions again.
Configure the daemon (I think it's in LWIP, actually, but it's exposed via ESP-IDF) via esp_netif_sntp_init() and be done.
https://docs.espressif.com/projects/esp ... ronization
It's super simple, and the system lets the SNTP fetches run in the background while it's doing ARPs and other things, so it's low overhead. I don't think it even _really_ configures another daemon as much as it tells existing ones there's a hitchhiker.
When I tried it, it even did a sensible thing across low-power transitions, though I don't exercise that case on a regular basis.
Of course, https://en.cppreference.com/w/cpp/chron ... k/now.html and the rest of std::chrono works great and you never have to touch those 1970's-era ISO C time functions again.
-
lbernstone
- Posts: 1132
- Joined: Mon Jul 22, 2019 3:20 pm
Who is online
Users browsing this forum: No registered users and 2 guests