NTPClient_Generic Library to get time from NTP servers

Working libraries, libraries being ported and related hardware
Post Reply
khoih-prog
Posts: 102
Joined: Thu Feb 27, 2020 7:54 am
Location: Toronto

NTPClient_Generic Library to get time from NTP servers

Post by khoih-prog »

NTPClient_Generic Library

How To Install Using Arduino Library Manager


Why do we need this NTPClient_Generic Library

The NTPClient_Generic Library is designed to work in conjunction with the Arduino Time Library, which must also be installed on your system. This documentation assumes some familiarity with the Time library.

The primary goal of the NTPClient_Generic Library is to enable the creation of an NTPClient to connect to NTP time-servers to get time and keep local time in sync. For ESP8266/ESP32, nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, etc. besides AVR, using ESP WiFi, WiFiNINA, Ethernet W5x00, ENC28J60, LAN8742A, ESP8266/ESP32 AT-command WiFi. But whether hardware RTC or another time-keeping source is present is not mandatory, since the Time Library can function as a software RTC without additional hardware (although its accuracy is dependent on the accuracy of the microcontroller's system clock.)

The examples will demonstrate how to get the UTC time from NTP server, then update the DS3231, STM32 built-in RTC or local clock to make sure the time is perfectly correct.

In the examples, the interval to get perfect time from the NTP server to update RTC and/or local clock is configurable to ensure the RTC and/or local clock accuracy.

This NTPClient_Generic Library is based on and modified from Fabrice Weinberg's NTPClient Library to add functions, features and as well as support to many boards and shields.

Releases v3.2.2

1. Add examples to use STM32 Built-In RTC.

Releases v3.2.1

1. Initial porting to many Generic boards using WiFi/Ethernet modules/shields.
2. Add support to SAMD21/SAMD51, nRF52, STM32F/L/H/G/WB/MP1 with WiFiNINA, W5x00, ENC28J60, LAN8742A Ethernet modules/shields.
3. Add functions.
4. Completely new examples using NTP time to update DS3231 RTC and/or local clock.

Currently Supported Boards

 - ESP8266.
 - ESP32.
 - SAM DUE
 - AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox etc..
 - Arduino SAMD21 (ZERO, MKR, NANO_33_IOT, etc.).
 - Adafruit SAMD21 (Itsy-Bitsy M0, Metro M0, Feather M0, Gemma M0, etc.).
 - Adafruit SAMD51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).
 - Seeeduino SAMD21/SAMD51 boards (SEEED_WIO_TERMINAL, SEEED_FEMTO_M0, SEEED_XIAO_M0, Wio_Lite_MG126, WIO_GPS_BOARD, SEEEDUINO_ZERO, SEEEDUINO_LORAWAN, SEEED_GROVE_UI_WIRELESS, etc.)
 - STM32 (Nucleo-144, Nucleo-64, Nucleo-32, Discovery, STM32F1, STM32F3, STM32F4, STM32H7, STM32L0, etc.).
 - STM32F/L/H/G/WB/MP1 (Nucleo-64 L053R8,Nucleo-144, Nucleo-64, Nucleo-32, Discovery, STM32Fx, STM32H7, STM32Lx, STM32Gx, STM32WB, STM32MP1, etc.) having 32K+ Flash program memory.
 - AVR Mega, UNO, Nano
 
Currently Supported WiFi Modules/Shields

 - ESP8266 built-in WiFi.
 - ESP32 built-in WiFi.
 - WiFiNINA using WiFiNINA or WiFiNINA_Generic library.
 - ESP8266-AT, ESP32-AT WiFi shields using WiFiEspAT or ESP8266_AT_WebServer library. To be done soon.
 
Currently Supported Ethernet Modules/Shields

 - W5x00's using Ethernet, EthernetLarge, Ethernet2 or Ethernet3 Library.
 - ENC28J60 using EthernetENC or UIPEthernet library.
 - LAN8742A using STM32Ethernet / STM32 LwIP libraries.

Examples

ESP32/ESP8266 Boards

1. ESP_NTPClient_Advanced
2. ESP_NTPClient_Basic

Generic Boards with Ethernet

3. Ethernet_NTPClient_Advanced
4. Ethernet_NTPClient_Basic
5. RTC_Ethernet_NTPClient
6. TZ_NTP_Clock_Ethernet
7. TZ_NTP_WorldClock_Ethernet

STM32F/L/H/G/WB/MP1 Boards with Ethernet

8. BI_RTC_Alarm_Ethernet_NTPClient_STM32
9. BI_RTC_Ethernet_NTPClient_STM32
10. Ethernet_NTPClient_Advanced_STM32
11. Ethernet_NTPClient_Basic_STM32
12. RTC_Ethernet_NTPClient_STM32
13. TZ_NTP_Clock_STM32_Ethernet
14. TZ_NTP_WorldClock_STM32_Ethernet

Generic Boards with WiFiNINA

15. NINA_NTPClient_Advanced
16. NINA_NTPClient_Basic
17. NINA_TZ_NTP_Clock
18. NINA_TZ_NTP_WorldClock


Debug Termimal Output Samples

1. This is terminal debug output when running Ethernet_NTPClient_Advanced example on Adafruit nRF52 NRF52840_FEATHER_EXPRESS with ENC28J60 & EthernetENC Library.

Code: Select all

Starting Ethernet_NTPClient_Advanced on NRF52840_FEATHER with ENC28J60 using EthernetENC Library

[ETHERNET_WEBSERVER] =========================
[ETHERNET_WEBSERVER] Default SPI pinout:
[ETHERNET_WEBSERVER] MOSI: 25
[ETHERNET_WEBSERVER] MISO: 24
[ETHERNET_WEBSERVER] SCK: 26
[ETHERNET_WEBSERVER] SS: 5
[ETHERNET_WEBSERVER] =========================
[ETHERNET_WEBSERVER] Board : NRF52840_FEATHER , setCsPin: 10
=========================
Currently Used SPI pinout:
MOSI:25
MISO:24
SCK:26
SS:5
=========================
Using mac index = 3
You're connected to the network, IP = 192.168.2.79
Using NTP Server 0.ca.pool.ntp.org
******NOT UPDATED******
UTC : 00:00:05
UTC : 00:00:05 Thu 01 Jan 1970
LOC : 02:28:21
LOC : 02:28:21 Sun 07 Feb 2106
UTC EPOCH : 5
LOC EPOCH : 4294952901
UTC : 0:0:5 Thu 1/1/1970 or 1 Jan 1970
UTC : 00:00:05 Thu 1/1/1970 or 1 Jan 1970
LOC : 2:28:21 Sun 7/2/2106 or 7 Feb 2106
LOC : 02:28:21 Sun 7/2/2106 or 7 Feb 2106
********UPDATED********
UTC : 18:55:26
UTC : 18:55:26 Mon 26 Oct 2020
LOC : 14:55:26
LOC : 14:55:26 Mon 26 Oct 2020
UTC EPOCH : 1603738526
LOC EPOCH : 1603724126
UTC : 18:55:26 Mon 26/10/2020 or 26 Oct 2020
UTC : 18:55:26 Mon 26/10/2020 or 26 Oct 2020
LOC : 14:55:26 Mon 26/10/2020 or 26 Oct 2020
LOC : 14:55:26 Mon 26/10/2020 or 26 Oct 2020

2. The following is debug terminal output when running example BI_RTC_Alarm_Ethernet_NTPClient_STM32 example on STM32F7 Nucleo-144 NUCLEO_F767ZI with LAN8742A using STM32Ethernet Library to demonstrate the usage of STM32 built-in RTC Alarm function

Code: Select all

Start BI_RTC_Alarm_Ethernet_NTPClient_STM32 on NUCLEO_F767ZI, using LAN8742A Ethernet & STM32Ethernet Library
[ETHERNET_WEBSERVER] Board : NUCLEO_F767ZI , setCsPin: 10
[ETHERNET_WEBSERVER] Default SPI pinout:
[ETHERNET_WEBSERVER] MOSI: 11
[ETHERNET_WEBSERVER] MISO: 12
[ETHERNET_WEBSERVER] SCK: 13
[ETHERNET_WEBSERVER] SS: 10
[ETHERNET_WEBSERVER] =========================
You're connected to the network, IP = 192.168.2.111
Retry RTC update in seconds : 3

Updating Time for STM32 RTC
********UPDATED********
=======RTC ALARM SET========
20:47:55 Wed 28 Oct 2020 UTC
16:47:55 Wed 28 Oct 2020 EDT
============================
RTC updated. Next update in seconds : 3600
============================
20:47:31 Wed 28 Oct 2020 UTC
16:47:31 Wed 28 Oct 2020 EDT
============================
20:47:40 Wed 28 Oct 2020 UTC
16:47:40 Wed 28 Oct 2020 EDT
============================
20:47:50 Wed 28 Oct 2020 UTC
16:47:50 Wed 28 Oct 2020 EDT
*****RTC ALARM ACTIVATED*****
*****RTC ALARM ACTIVATED*****
============================
20:47:59 Wed 28 Oct 2020 UTC
16:47:59 Wed 28 Oct 2020 EDT
============================
20:48:08 Wed 28 Oct 2020 UTC
16:48:08 Wed 28 Oct 2020 EDT
3. The following is debug terminal output when running example BI_RTC_Ethernet_NTPClient_STM32 example on STM32F7 Nucleo-144 NUCLEO_F767ZI with LAN8742A using STM32Ethernet Library to demonstrate the usage of STM32 built-in RTC

Code: Select all

Start BI_RTC_Ethernet_NTPClient_STM32 on NUCLEO_F767ZI, using ENC28J60 & EthernetENC Library
[ETHERNET_WEBSERVER] Board : NUCLEO_F767ZI , setCsPin: 10
[ETHERNET_WEBSERVER] Default SPI pinout:
[ETHERNET_WEBSERVER] MOSI: 11
[ETHERNET_WEBSERVER] MISO: 12
[ETHERNET_WEBSERVER] SCK: 13
[ETHERNET_WEBSERVER] SS: 10
[ETHERNET_WEBSERVER] =========================
BYou're connected to the network, IP = 192.168.2.97

Updating Time for STM32 RTC
********UPDATED********
RTC updated. Next update in seconds : 3600
============================
20:40:14 Wed 28 Oct 2020 UTC
16:40:14 Wed 28 Oct 2020 EDT
============================
20:40:24 Wed 28 Oct 2020 UTC
16:40:24 Wed 28 Oct 2020 EDT
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: NTPClient_Generic Library to get time from NTP servers

Post by mrburnette »

@khoih-prog

+1

Thank you for the effort that goes into such a lib and for putting it all into the open source environment.
Same for Timezone_Generic Library to convert UTC to local time


Ray
khoih-prog
Posts: 102
Joined: Thu Feb 27, 2020 7:54 am
Location: Toronto

Re: NTPClient_Generic Library to get time from NTP servers

Post by khoih-prog »

Thanks so much for your encouraging words, @mrburnette

As you know, ESP32/ESP8266 have been so popular and powerful thanks to the well-supported infrastructure (core, libraries, etc). When I moved to other boards (nRF52, STM32, SAMD, etc.), I was amazed that I had to do many things from scratch as the support level is nowhere compared to that of ESP platform.

That's why I've been trying to port / write, as many as possible, libraries to save some us time, so that we can spend on more important work.

Regards,
zoomx
Posts: 28
Joined: Fri Dec 20, 2019 10:12 am
Location: Near Mt.Etna

Re: NTPClient_Generic Library to get time from NTP servers

Post by zoomx »

+1!!!

Though ESP8266 and ESP have NTP syncronization in their core with timezone and daylight saving time too.
khoih-prog
Posts: 102
Joined: Thu Feb 27, 2020 7:54 am
Location: Toronto

Re: NTPClient_Generic Library to get time from NTP servers

Post by khoih-prog »

Thanks.

For ESP32 and ESP8266, it's better to use built-in functions to get UTC Time from NTP servers and update their internal RTCs.
Post Reply

Return to “Libraries & Hardware”