Tick tock, what’s the time? This simple class is a simple library to provide a clock for an Arduino equipped with an Ethernet shield. Saves adding a separate real-time clock chip to maintain time for a program connected to the Internet.
To use, declare a global instance of the clock class, and call maintain in each iteration of the loop. Every 5 hours, the clock will contact a network time protocol (NTP) server to get the latest time. In between it will extrapolate time from the Arduino’s millisecond timer. We’ve tried to keep the RAM footprint for this class as small as possible so, for example, it uses program memory to store the NPT message. This library builds on work by Michael Margolis and the Arduino Ethernet library.
Example
In this test program, the time is written out the serial port every second. An instance variable, initialization and a call to maintain (which contacts the NTP server at the right time) are all that are needed to drive this simple clock.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include #include #include "Clock.h" Clock g_Clock; unsigned long g_ulLastPrint; void setup() { byte ArduinoMAC[] = {0xAE, 0xAD, 0xBF, 0xEA, 0xDE, 0xED}; IPAddress ArduinoIP(192, 168, 15, 55); Serial.begin(9600); // Initialize Ethernet. Ethernet.begin(ArduinoMAC, ArduinoIP); // Initialize time for last print. g_ulLastPrint = millis(); // Setup the clock. In New Zealand we are 12 hours ahead of GMT g_Clock.Setup(); g_Clock.SetTimezoneOffset(12,0); } void loop() { g_Clock.Maintain(); if (millis() - g_ulLastPrint > 1000) { g_ulLastPrint = millis(); g_Clock.WriteDateTime(&Serial); Serial.write('\n'); } } |
Public Interface
The clock class includes 7 public functions:
1 2 3 4 5 6 7 8 |
void Setup(); void SetTimezoneOffset(int nHours, int nMinutes); void Maintain(); unsigned long GetTimestamp(); void WriteTime(HardwareSerial *pPort); void WriteDateTime(HardwareSerial *pPort); void DecodeTo(DateTime &dt); |
Infrastructure
The class infrastructure is handled through the Setup and Maintain functions. Neither take any parameters and are intended to be called from the main Arduino setup and loop functions respectively. The work to update the local time is handled by the Maintain function. Most of the time it does nothing. Every five hours it contacts a network time server to update the internal time reference.
The clock class stores time in UTC format as an offset, in seconds, from 1 Jan 1970; the same format used for Unix time. The function SetTimezoneOffset saves an offset, applied to correct for the local time zone.
Time Functions
Four functions provide access to the time stored:
- GetTimestamp – Returns the number of seconds since 1 Jan 1970, in the local time-zone.
- WriteTime – Writes the current local time to a serial port in the form HH:MM:SS
- WriteDateTime – Writes the current local date and time to a serial port in the form YYYY/MM/DD HH:MM:SS
- DecodeTo – Decodes the local date and time into a DateTime structure. The DateTime structure includes fields for year, month, day, hours, minutes and seconds.
Download Here
The Arduino clock class can be downloaded and used in your own projects. The zip file includes the Clock class and the example above. Place it in your Arduino libraries folder and you’ll find the example on the File menu.