Author Topic: Any Full NTP Client For esp8266/esp32?  (Read 2909 times)

0 Members and 1 Guest are viewing this topic.

Offline The Doktor

  • Regular Contributor
  • *
  • Posts: 79
Any Full NTP Client For esp8266/esp32?
« on: September 01, 2018, 02:00:05 am »
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
 

Offline usagi

  • Frequent Contributor
  • **
  • Posts: 361
  • Country: us
Re: Any Full NTP Client For esp8266/esp32?
« Reply #1 on: September 01, 2018, 11:28:02 am »
you're probably better off using a gps module.

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2052
  • Country: gb
Re: Any Full NTP Client For esp8266/esp32?
« Reply #2 on: September 03, 2018, 08:46:33 pm »
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.

 

Offline Jr460

  • Regular Contributor
  • *
  • Posts: 117
Re: Any Full NTP Client For esp8266/esp32?
« Reply #3 on: September 03, 2018, 09:08:16 pm »
you're probably better off using a gps module.

Seems 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.
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2052
  • Country: gb
Re: Any Full NTP Client For esp8266/esp32?
« Reply #4 on: September 05, 2018, 10:48:23 am »
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-DST

Generating 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
 

Offline usagi

  • Frequent Contributor
  • **
  • Posts: 361
  • Country: us
Re: Any Full NTP Client For esp8266/esp32?
« Reply #5 on: September 06, 2018, 12:19:50 am »
SNTP != NTP, not by a long shot.

he's wanting to discipline the uC clock, which is time-nuts level 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.
« Last Edit: September 06, 2018, 12:22:18 am by usagi »
 

Online hamster_nz

  • Super Contributor
  • ***
  • Posts: 2285
  • Country: nz
Re: Any Full NTP Client For esp8266/esp32?
« Reply #6 on: September 06, 2018, 12:49:51 am »
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).
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 
The following users thanked this post: NivagSwerdna

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2052
  • Country: gb
Re: Any Full NTP Client For esp8266/esp32?
« Reply #7 on: September 06, 2018, 11:19:55 am »
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.
GPS doesn't fit my basement use case; not without lots of additional wiring.

the easy way to get full blown ntp would be running something like a raspberry pi zero.
I'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.
 

Offline usagi

  • Frequent Contributor
  • **
  • Posts: 361
  • Country: us
Re: Any Full NTP Client For esp8266/esp32?
« Reply #8 on: September 06, 2018, 11:56:24 am »
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.
« Last Edit: September 06, 2018, 11:59:19 am by usagi »
 

Offline jceev

  • Newbie
  • Posts: 3
  • Country: us
Re: Any Full NTP Client For esp8266/esp32?
« Reply #9 on: September 06, 2018, 07:47:24 pm »
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.
« Last Edit: September 06, 2018, 08:00:28 pm by jceev »
 
The following users thanked this post: NivagSwerdna

Online hamster_nz

  • Super Contributor
  • ***
  • Posts: 2285
  • Country: nz
Re: Any Full NTP Client For esp8266/esp32?
« Reply #10 on: September 06, 2018, 09:55:50 pm »
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.

I 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...
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline jceev

  • Newbie
  • Posts: 3
  • Country: us
Re: Any Full NTP Client For esp8266/esp32?
« Reply #11 on: September 06, 2018, 10:15:57 pm »
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...

My 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!  :-+

 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2052
  • Country: gb
Re: Any Full NTP Client For esp8266/esp32?
« Reply #12 on: September 09, 2018, 06:15:43 pm »
I have the Arduino code for this (Nixie "GPS" clock sync) from a few years ago if I can find it...
You really need to consider fractional time and to delay sufficiently that the NMEA message is sent on the second boundary.  Shouldn't be hard.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf