Every microcontroller project that logs data, schedules actions or shows a clock eventually needs to know the time. Getting it right is surprisingly hard: internal clocks drift, RTC modules need to be set, and NTP needs internet access, which you often don’t have in the field, in a workshop or on a battery-powered sensor.
Yet the answer is probably in your pocket. A smartphone knows the time very precisely, from the network and from its satellite receiver. The idea behind this project is simple: let the phone share its time with your boards.
Three projects that work together
Android Time Services is an app that turns an Android phone into a small time server. It collects time from internet time servers, from the phone’s satellite receiver and from nearby Bluetooth devices, combines these sources into one reliable clock, and keeps going even when some sources disappear. It then shares that time in two ways: over Wi-Fi, using the standard Network Time Protocol (NTP), and over Bluetooth Low Energy, using the standard Current Time Service (CTS).
TinyNTP is a small Arduino library for NTP. It lets a board get the time over Wi-Fi or Ethernet, from the phone or any other time server, and it can also act as a time server itself.
TinyCTS is the Bluetooth counterpart. It lets a board get the time from a phone, a watch or another board over Bluetooth, with no network at all, and it can also serve the time to others.
Together they give you a small, self-contained time network: the phone is the reference, and the boards pick up the time by whichever route suits them.
Wi-Fi or Bluetooth?
NTP over Wi-Fi is the natural choice when your board is on Wi-Fi anyway or needs the best precision. The phone can even provide its own hotspot, so this works without any internet connection as long as the phone can see the sky.
Bluetooth is the choice for battery-powered devices, places without a network, or simply “set the clock from my phone”. It uses little power, needs no setup, and the phone sends its time zone along, so the board shows the correct local time automatically.
It works in both directions
The app is not only a server. Boards running TinyNTP or TinyCTS as a server, for example with a GPS module or a precise real-time clock attached, can feed their time back into the app as additional sources. Boards can also pass time along: one board can fetch the time over Wi-Fi and then share it over Bluetooth with others that have no network.
Where things stand
The app is a working prototype, so far tested on one phone. The libraries run on popular boards such as the ESP32, the Raspberry Pi Pico W and Arduino’s Bluetooth-capable boards, as well as on Linux desktops. Feedback from other phones and boards is very welcome.
Learn more
- Android Time Services: https://github.com/pschatzmann/Android-Time-Services
- TinyNTP: https://github.com/pschatzmann/TinyNTP
- TinyCTS: https://github.com/pschatzmann/TinyCTS
All three projects are open source. If you find them useful, you can support the work through the sponsorship links on their GitHub pages.
0 Comments