ESP8266 NodeMCU NTP (Network Time Protocol) Clock

This tutorial demonstrate how to get NTP Network Time using ESP8266 NodeMCU? Getting network time is much simpler than adding external RTC Chip to ESP8266. Use of NTP with ESP8266 makes getting time simpler and accurate. Before we start we must know what is NTP and How to get NTP Time in ESP8266.

What is NTP?

The Network Time Protocol (NTP) is widely used to synchronize computer clocks in the Internet. NTP is intended to synchronize all participating computers to within a few milliseconds of Coordinated Universal Time (UTC).

What is NTP Server?

NTP uses the concepts of server and client. A server is a source of time information, and a client is a system that is attempting to synchronize its clock to a server.

What is NTP Port?

OpenNTPD also uses high-numbered source ports so if it is able to synchronize but ntpd is not, it is very probable that the incoming UDP port 123 is blocked. If you’re going to run ntpd , you need to fix your network/firewall/NAT so that ntpd can have full unrestricted access to UDP port 123 in both directions.

How NTP UDP Protocol Works?

for more details read RFC958

The NTP packets sent by the client to the server and the responses from the server to the client use a common format, as shown in Figure.

ntp udp packet format for ESP8266

The header fields of the NTP message are as follows:

LI Leap Indicator (2 bits)
This field indicates whether the last minute of the current day is to have a leap second applied. The field values follow:
0: No leap second adjustment
1: Last minute of the day has 61 seconds
2: Last minute of the day has 59 seconds
3: Clock is unsynchronized
VN NTP Version Number (3 bits) (current version is 4).
Mode NTP packet mode (3 bits)
The values of the Mode field follow:
0: Reserved
1: Symmetric active
2: Symmetric passive
3: Client
4: Server
5: Broadcast
6: NTP control message
7: Reserved for private use
Stratum Stratum level of the time source (8 bits)
The values of the Stratum field follow:
0: Unspecified or invalid
1: Primary server
2–15: Secondary server
16: Unsynchronized
17–255: Reserved
Poll Poll interval (8-bit signed integer)
The log2 value of the maximum interval between successive NTP messages, in seconds.
Precision Clock precision (8-bit signed integer)
The precision of the system clock, in log2 seconds.
Root Delay The total round-trip delay from the server to the primary reference sourced. The value is a 32-bit signed fixed-point number in units of seconds, with the fraction point between bits 15 and 16. This field is significant only in server messages.
Root Dispersion    The maximum error due to clock frequency tolerance. The value is a 32-bit signed fixed-point number in units of seconds, with the fraction point between bits 15 and 16. This field is significant only in server messages.
Reference Identifier For stratum 1 servers this value is a four-character ASCII code that describes the external reference source (refer to Figure 2). For secondary servers this value is the 32-bit IPv4 address of the synchronization source, or the first 32 bits of the Message Digest Algorithm 5 (MD5) hash of the IPv6 address of the synchronization source.

ESP8266 NodeMCU Program to get Network Time

Before uploading program make changes in WiFi Network settings and UTC Time Zone for your area.

Results

after uploading program open serial monitor.

ESP8266 NTP Time Clock
ESP8266 NTP Clock in Serial Monitor

This way we can get NTP Time in ESP8266.

 

One thought on “ESP8266 NodeMCU NTP (Network Time Protocol) Clock

Leave a Reply