Getting time over the internet

A project log for The Clock Awakens

Using LEDs and an ESP8266 to make a smart, futuristic Star Wars clock

Alex Cordonnier 09/23/2015 at 05:110 Comments

Over the weekend, I wrote the code that lets the clock get the time over the internet using the Network Time Protocol. As I discovered, NTP is a rather intricate system, but thankfully this project doesn't need the microsecond accuracy that requires a complicated setup. It didn't take terribly long to write a simple NTP implementation.

However, writing an NTP implementation was not enough. My goal is to make the clock "just work" after plugging it in, and that means displaying time in the local time zone. To detect the time zone requires knowing the user's location, which for this project can be reasonably approximated by the location of the clock's public IP address. So I wrote another helper class to roughly locate the clock and determine the time zone from that.

Next, I wrote some very simple initialization that would connect to WiFi, print some debugging info, and set up the geolocation and NTP.

Once I received the hardware today, I could finally test the code. I fixed several embarrassing bugs, but it seems to work just fine now. The clock drifts more than I hoped, so I may need to synchronize more frequently and/or write code to apply a correction factor based on the typical amount of drift.

The next step is to hook up the NeoPixels and start displaying the time visually. I would also like to provide a simple web interface for settings like the time zone and WiFi network, but that's low priority for now.