https://github.com/erriez/erriezntpclient
NTP client library for Arduino to retrieve UNIX Epoch UTC time
https://github.com/erriez/erriezntpclient
arduino avr client documentation epoch erriez esp32 esp8266 ethershield examples library network ntp sync time unix
Last synced: 2 months ago
JSON representation
NTP client library for Arduino to retrieve UNIX Epoch UTC time
- Host: GitHub
- URL: https://github.com/erriez/erriezntpclient
- Owner: Erriez
- License: mit
- Created: 2020-09-13T19:30:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-13T20:08:25.000Z (almost 6 years ago)
- Last Synced: 2025-04-06T21:33:52.688Z (about 1 year ago)
- Topics: arduino, avr, client, documentation, epoch, erriez, esp32, esp8266, ethershield, examples, library, network, ntp, sync, time, unix
- Language: C++
- Homepage: https://github.com/Erriez/ErriezArduinoLibraries
- Size: 364 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Erriez NTP Client library for Arduino
[](https://travis-ci.org/Erriez/ErriezNTPClient)
This is a minimized NTP Client library for Arduino to retrieve UNIX Epoch UTC from NTP time servers.
## Library features
* Retrieve UNIX Epoch UTC time from network time servers
* Compatible with `time.h`
* Timeout handling
## Supported hardware
* Arduino UNO with EtherShield (Wiznet W5100 Ethernet controller)
* ESP8266 WiFi
* ESP32 WiFi
## Documentation
- [Online HTML](https://erriez.github.io/ErriezNTPClient)
- [Doxygen PDF](https://github.com/Erriez/ErriezNTPClient/raw/master/ErriezNTPClient.pdf)
## Example output
```c++
Erriez ESP8266 NTP example
Connecting to 'wifi'...OK
Epoch: 1600025290
UTC: Sun Sep 13 19:28:10 2020
```
## Example ESP8266 / ESP32
```c++
#if defined(ARDUINO_ARCH_ESP8266)
#include
#elif defined(ARDUINO_ARCH_ESP32)
#include
#endif
#include
// WiFi SSID and Password
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
// "pool.ntp.org", "time.nist.gov" or NTP server IP address
#define NTP_SERVER "pool.ntp.org"
ErriezNTPClient ntp(NTP_SERVER);
void setup()
{
// Initialize serial
delay(500);
Serial.begin(115200);
Serial.println(F("\nErriez NTP client ESP8266 / ESP32 example"));
// Initialize WiFi
Serial.print(F("Connecting to '"));
Serial.print(WIFI_SSID);
Serial.print(F("'"));
// Connect to your WiFi router
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("OK");
}
void loop()
{
time_t t;
// Get epoch
t = ntp.getEpoch();
// Print result
if (t > 0) {
Serial.print(F("Epoch: "));
Serial.println((uint32_t)t);
Serial.print(F("UTC: "));
Serial.println(ctime(&t));
} else {
Serial.println(F("Timeout"));
}
delay(10000);
}
```
## Example AVR (ATMega328 / ATMega2560)
```c++
#include
#include
// "pool.ntp.org", "time.nist.gov" or NTP server IP address
#define NTP_SERVER "pool.ntp.org"
ErriezNTPClient ntp(NTP_SERVER);
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
uint8_t mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
void setup()
{
// Initialize serial
delay(500);
Serial.begin(115200);
Serial.println(F("\nErriez NTP client AVR example"));
// Start Ethernet and UDP
if (!Ethernet.begin(mac)) {
Serial.println(F("Failed to configure Ethernet using DHCP"));
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println(F("Ethernet shield was not found."));
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println(F("Ethernet cable is not connected."));
}
}
}
void loop()
{
time_t t;
// Get epoch
t = ntp.getEpoch();
// Print result
if (t > 0) {
Serial.print(F("Epoch: "));
Serial.println((uint32_t)t);
// A UNIX offset is needed for AVR target
t -= UNIX_OFFSET;
Serial.print(F("UTC: "));
Serial.println(ctime(&t));
} else {
Serial.println(F("Timeout"));
}
delay(10000);
}
```
## Library installation
Please refer to the [Wiki](https://github.com/Erriez/ErriezArduinoLibraries/wiki) page.
## Other Arduino Libraries and examples from Erriez
[Erriez Libraries](https://github.com/Erriez/ErriezArduinoLibraries)