Any Full NTP Client For esp8266/esp32? - Page 1
Forum section: electronics / general

author: The Doktor
author_rank: Regular Contributor
timestamp: September 01, 2018, 02:00:05 am
content: I'm aware that the esp32 includes a sntp client, at least in the incarnation in the arduino IDE. And google give me several "NTP" libraries for both the esp8266 and esp32. However, everything I've tested seems to only sync to the nearest second.I would like to find something that syncs more accurately, and perhaps disciplines the clock as well (if that's even possible with these chips). Do you know of any NTP libraries like this? I've considered trying to write my own, but it's probably above my (lack of) programming ability.I'm planning to build a few 6 digit clocks using nixies and numitrons, and don't want to have the seconds occasionally sit still for almost 2 seconds or maybe go back a second when time is synched.Ed

author: usagi
author_rank: Frequent Contributor
timestamp: September 01, 2018, 11:28:02 am
content: you're probably better off using a gps module.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: September 03, 2018, 08:46:33 pm
content: I started doing this but got side tracked...It should not be too hard... just send an NTP request(48byte UDP buffer required), wait for replies and slowly discipline the local clock.NTP request/response has fractional time so accuracy better than 1s is easily achieveable.

author: Jr460
author_rank: Regular Contributor
timestamp: September 03, 2018, 09:08:16 pm
content: Quote from: usagi on September 01, 2018, 11:28:02 amSeems overkill when NTP will sync very well, and all you are talking about is software, rather than getting more hardware for a GPS module, then re-inevting NTP to software to sync to the GPS module. And before you say, just use the time from the GPS, then what happens when you don't have a GPS fix for some reason. You have to free run, and then when the GPS comes back you have the problem that NTP was designed to fix. Bringing the local clock back to the right time without jumps and without moving backwards.I have machines that talk to stratum 3 servers and I get time synced to hundreds of microseconds very often. YMMV.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: September 05, 2018, 10:48:23 am
content: I had another look at this and it seems really quite simple now.It appears that SNTP has been built into the esp8266 arduino core libraries so you can get a corrected local time very easily. There is even an example that is very close.https://github.com/esp8266/Arduino/tree/master/libraries/esp8266/examples/NTP-TZ-DSTGenerating the NMEA message shouldn't be too hard (although you need to determine if you are handling DST on the clock or want to bodge it)Someone seems to have already done alot of the hard work...https://www.tindie.com/products/mazevedo/nwts-ntp-sync-over-wi-fi-for-nixie-clocks-kit/#product-description

author: usagi
author_rank: Frequent Contributor
timestamp: September 06, 2018, 12:19:50 am
content: SNTP != NTP, not by a long shot.he's wanting to discipline the uC clock, which istime-nutslevel metrology. and really, probably not worth the effort.free running clock in most gps modules is likely far more accurate than any esp8266/esp32 internal clock. and they should be, considering what they have to do.the easy way to get full blown ntp would be running something like a raspberry pi zero.

author: hamster_nz
author_rank: Super Contributor
timestamp: September 06, 2018, 12:49:51 am
content: From what I have read on NTP, it wouldn't be too hard to and rudimentary NTP support.You send a UDP packet requesting the NTP server to reply, the NTP server adds timestamps and sends a reply.You can assume that time the NTP server processed it was half way between when it left and when it arrived, allowing you to estimate the offset of the local clock.You then either need to skew the local clock, or more likely maintain a couple of calibration factors (absolute offset and a linear trend).

author: NivagSwerdna
author_rank: Super Contributor
timestamp: September 06, 2018, 11:19:55 am
content: Quote from: usagi on September 06, 2018, 12:19:50 amGPS doesn't fit my basement use case; not without lots of additional wiring.Quote from: usagi on September 06, 2018, 12:19:50 amI'm a bit confused now at the implementation on the ESP8266.... Sending a NTP request and using the four timestamps to calculate local time, adjust, rinse and repeat should not be hard.The aforementioned product says "with approximately 1s precision" which seems like at least an order of magnitude worse than could be achieved.

author: usagi
author_rank: Frequent Contributor
timestamp: September 06, 2018, 11:56:24 am
content: the arduino sntp implementations don't do clock disciplining. all they do is stomp the clock with a decoded ntp time.it would be an interesting project to do a full implementation,but will be a bunch of work and stepping into time-nuts territory.it is not clear that the esp8266/esp32 clock can be disciplined in the required way. someone will need to dig through the hardware to see.

author: jceev
author_rank: Newbie
timestamp: September 06, 2018, 07:47:24 pm
content: I have the Arduino code for this (Nixie "GPS" clock sync) from a few years ago if I can find it. It's written for the ESP8266, but should be easy to port for the 32. It's been running well over two years on my clock and I think it's running a sync every 60 minutes. I'm very much a cut and paste coder, so I'm sure it could be cleaned up a lot, but it does work. I'll post it later this afternoon if I can dig it up.Edit:Attached code that I modified a little to try to get automatic DST to work, but I don't remember if I tested it. Hope someone finds it useful.

author: hamster_nz
author_rank: Super Contributor
timestamp: September 06, 2018, 09:55:50 pm
content: Quote from: jceev on September 06, 2018, 07:47:24 pmI might have a play with this on the weekend - I've got an ESP32 monitoring my boy's bedroom temperature, and I might put on a 'time to get up" light.Any complaints if I push the code up to GitHub if I get it working? I'll put you in the comments as the author...

author: jceev
author_rank: Newbie
timestamp: September 06, 2018, 10:15:57 pm
content: Quote from: hamster_nz on September 06, 2018, 09:55:50 pmMy only concern is that lot of this code was cut and pasted from examples or other sources on the Internet , so I don't want to take undue credit for anything... But otherwise, have at it!

author: NivagSwerdna
author_rank: Super Contributor
timestamp: September 09, 2018, 06:15:43 pm
content: Quote from: jceev on September 06, 2018, 07:47:24 pmYou really need to consider fractional time and to delay sufficiently that the NMEA message is sent on the second boundary. Shouldn't be hard.