Arduino library to read time from Network Time Protocol (NTP) servers.
- Handles all heavy lifting involved with managing connections to and parsing time from an NTP server.
- As easy as providing a UDP object to the constructor during initialization.
- Works on Arduino, ESP8266, and ESP32.
- Configurable NTP server pool, time offset, and update interval.
- Stale time preservation: returns the last known time when a resync fails.
-
NodeMCU
Using EasyNTPClient on a NodeMCU (ESP8266). -
ArduinoEspWifiShield
Using EasyNTPClient on an Arduino UNO with an ESP-01 (ESP8266) WiFi module. -
TestNTPClient
Full test suite covering all features. Runs on ESP8266, ESP32, and Uno+ESP-01.
-
No frills
EasyNTPClient(UDP &udp) Parameters: udp: Reference to a UDP object. Returns: EasyNTPClient object.
-
Custom server pool
EasyNTPClient(UDP& udp, const char* serverPool) Parameters: udp: Reference to a UDP object. serverPool: NTP server pool domain name. Default = "pool.ntp.org" Returns: EasyNTPClient object.
-
Time offset
EasyNTPClient(UDP& udp, const char* serverPool, int offset) Parameters: udp: Reference to a UDP object. serverPool: NTP server pool domain name. Default = "pool.ntp.org" offset: Difference from UTC in seconds. Default = 0 Returns: EasyNTPClient object.
-
Time offset and update interval
EasyNTPClient(UDP& udp, const char* serverPool, int offset, uint32_t updateIntervalSecs) Parameters: udp: Reference to a UDP object. serverPool: NTP server pool domain name. Default = "pool.ntp.org" offset: Difference from UTC in seconds. Default = 0 updateIntervalSecs: How often to resync with the NTP server, in seconds. Default = 60 Returns: EasyNTPClient object.
-
Get Unix time
unsigned long getUnixTime() Returns: Current time as a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC), adjusted by the configured time offset. Resyncs with the NTP server when the update interval has elapsed. Returns the last known time if resync fails.
-
Get time offset
int getTimeOffset() Returns: Current time offset from UTC in seconds.
-
Set time offset
void setTimeOffset(int offset) Parameters: offset: Difference from UTC in seconds.
-
Get NTP server
const char* getNTPServer() Returns: The current NTP server pool domain name.
-
Set NTP server
void setNTPServer(const char* serverPool) Parameters: serverPool: NTP server pool domain name.
-
Get update interval
uint32_t getUpdateInterval() Returns: How often the client resyncs with the NTP server, in seconds.
-
Set update interval
void setUpdateInterval(uint32_t seconds) Parameters: seconds: Resync interval in seconds.
-
Check if last sync succeeded
bool wasUpdated() Returns: true if the most recent NTP resync attempt succeeded, false if it failed or no resync has been attempted yet.