Once you have a real time clock working on the ESP8266, you might actually want to set it. As they have a backup battery you may just set it before you connect it to the ESP8266 and forget about it, but that’s not ideal. These cheap RTCs probably aren’t perfectly accurate and if it stops for any reason (e.g. dead backup battery) you’ll need to reset it. The DS3231 has a flag to indicate it’s been stopped – ideally this should be checked on startup and the clock set via NTP if there has been any interruption.
I did find one other simple NTP implementation but it’s incomplete, there is no timeout and it doesn’t clean up the connection when it’s finished (so it’ll leak memory). I think my version should work a little better, but I can’t guarantee it’s bug free so please let me know if you find any. As well as getting the time, the code is a nice simple example of a UDP client.
To use simply call ntp_get_time( ). The NTP request is asynchronous so you get the time in the ntp_udp_recv callback function, have a look there for two simple examples of what you could do with your newly received NTP time (print it out or set an RTC).
Code now on GitHub: https://github.com/raburton/esp8266
5 thoughts on “A simple NTP client for ESP8266”
I was wondering if this library was compatible with an Arduino? I have the ESP8266 connected to an Arduino Unto and I wanted to find a way to give it NTP capability. Any help would be great!
Don’t know. It’s written for the SDK API, but if you can call these APIs from Arduino then it should work. Otherwise you’d need to convert it to use the Arduino API.
Thank you !
could you please provide basic example code for your ntp Library ???
This will print out the time. If you want to do something else edit the ntp_udp_recv method.