NTP (Network Time Protocol) is a clock protocol that synchronize between servers and computers system, attach to the data networks. NTP was designed by David Millis at the University of Delaware. NTP is envisioned to synchronize all participating computers of coordinated universal time (UTC) a modified version of Marzullos Algorithm to select an accurate time servers. NTP can usually maintain the time within tens of milliseconds over the internet and can accomplish better that one millisecond accuracy in the local area networks. NTP is usually describe in terms of a client server model but it can easily be used in peer to peer connection where both peers consider the other to be a possible time source. NTP uses User Datagram Protocol (UDP) at port 123, it can also use broadcasting or multicasting where clients possibly attend to time and updates after an initial round trip calibrating exchange. NTP also provide an alert of any about to happen leap second adjustment but there is no information about local time zone or daylight saving time is transmitted.
The NTP uses hierarchical semi layered system of time on each level named STRATUM and is assigned a number starting with 0 at the top the server synchronized to the stratum server and running at stratum n+1 the number represents the distance from reference clock used to prevent cyclical dependencies in the hierarchy. STRATUM is not always a sign of quality or reliability. It is most common find STRATUM 3 TIME source that are higher quality that STRATUM 2 source.
STRATUM 0 – is a precise timekeeping devices such as Atomic, GPS, and Radio Clocks. They generate a very accurate pulse per second signal that its triggers an interrupt and timestamp on a connected computer.
STRATUM 1 – Stratum 1 are computers are system clock are synchronized within microseconds of their attached stratum 0 devices. The stratum 1 servers may peer with other stratus 1 servers for rational checking backup this is also referred as primary time servers.
STRATUM 2 – Stratum 2 are computers that are synchronized over the network with STRATUS 1 servers. Stratum 2 computers will query stratus 1 servers. Stratus 2 devices may also peer with other STRATUM 2 devices to provide more stable, robust time for all device in group.
STRATUM 3 – Stratum 3 are computers that are synchronized to stratum 2 servers. This devices employ the same algorithm for peering data sampling as STRATUS 2 and can be act as servers for STRATUS 4 devices.
Network time protocol can be found in many devices and applications, especially in network appliances, operating systems, weather stations, market exchange etc. when the data receive runs at UDP at Port 123 it contains multiple information like UNIX timestamp, accuracy, delay or time-zone.
For more details about NTP > https://en.wikipedia.org/wiki/Network_Time_Protocol
Required Components
- ESP8266 12, 12E, ESP8266 NodeMCU, ESPDuino, WeMos.
- 3.3v Regulator (AMS1117) / Voltage Step Down Module
- Capacitor
- Resistor
- Solder Less Breadboard
- Male Pin
- Jumper Wires / DuPont Wires
- Tactile Push Button
Pooling NTP Server on ESP8266
Driving ESP8266 Module to get data from the Network Time Protocol NTP server you need to set the code to communicate using UDP running at port 123. To achieve this u need to use 2 libraries (ESP8266WIFI.H) & (WIFIUDP.H) library can be found at the Arduino ESP8266, here is the step how to integrate ESP8266 to your Arduino IDE. Both libraries are available after installing the library. However as you can see the code below you just need to place your AP SSID and Password make it sure you are connected to the internet to communicate with the NTP server just replay the NTPSERVER with any STRATUM service you want.
Wiring Diagram
Source Code
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#include <ESP8266WiFi.h> //Include ESP8266WiFi.h Arduino Core Code Library #include <WiFiUdp.h> //Include WiFiUdp.h Arduino Core Code Library char ssid[] = "14CORE"; // Set your AP network SSID char pass[] = "1234567890"; //Set your AP WiFi AP password unsigned int localPort = 123; //Set local port listen to UDP IPAddress timeSRV; const char* srvName = "3.pool.ntp.org"; //You can change this by providing srvName(127.121.25.20); const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets WiFiUDP udp; // Set to send and receive packets via UDP void setup() { Serial.begin(115200); Serial.println("14CORE | Timer Service Test Code"); Serial.println("Starting...."); delay(200); Serial.println("Initializing..."); delay(200); Serial.println("Connecting >:") Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) {delay(600); Serial.print("..."); } Serial.println("Connected: "); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Initializing UDP..."); udp.begin(localPort); Serial.print("PORT: "); Serial.println(udp.localPort()); } void loop() { WiFi.hostByName(srvName, timeSRV); sendNTPpacket(timeSRV); // Sending NTP packets to NTP server delay(1000); //Set to wait if relay is available int npkts = udp.parsePacket(); if (!npkts) { Serial.println("No packets received.."); } else { Serial.print("Packets received length = "); Serial.println(npkts);udp.read(packetBuffer, NTP_PACKET_SIZE); // set to read packets from buffer /*Timestamp starting at byte 40 at the RX packet @ 4 bytes - 2 words long then extract the 2 words*/ unsigned long hWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lwWord = word(packetBuffer[42], packetBuffer[43]); /* Merging 4bytes - 2words into long integer */ unsigned long seconds1990 = hWord << 16 | lwWord; Serial.print("Seconds since Jan 1 1900 = " ); Serial.println(seconds1990); /* now convert NTP time into everyday time:, UNIX time start on JAN 1 1970 in seconds will be 2208988800 */ Serial.print("UNIX TIME = "); const unsigned long 70Years = 2208988800UL; unsigned long timeOut = seconds1990 - 70Years; //Set to subtract 70 years Serial.println(timeOut); Serial.print("UTC TIME: ");//Set UTC is the time at GMT Serial.print((timeOut % 86400L) / 3600); //Set to print hour 86400 = sec per day Serial.print(':'); if ( ((timeOut % 3600) / 60) < 10 ) { Serial.print('0'); //Set to print first 10 min of each hr } Serial.print((timeOut % 3600) / 60); //Set to print min 3600 = sec per min Serial.print(':'); if ( (timeOut % 60) < 10 ) { Serial.print('0'); } Serial.println(timeOut % 60); //Set to print sec } delay(10000); //Set to wait 10 sec then ask for the time } unsigned long sendNTPpacket(IPAddress& address) //Sending NTP req to the time server { Serial.println("Syn NTP Pckets: "); memset(packetBuffer, 0, NTP_PACKET_SIZE); // Set bytes to buffer turn 0 // Initializing the required values form NTP request packetBuffer[0] = 0b11100011; //Set to LI version mode packetBuffer[1] = 0; // Set to Stratum type packetBuffer[2] = 6; // Set to pooling interval packetBuffer[3] = 0xEC; // Set to Peer Clock Precision packetBuffer[12] = 49; // 8bytes of 0 packetBuffer[13] = 0x4E; // 8bytes of 0 packetBuffer[14] = 49; // 8bytes of 0 packetBuffer[15] = 52; // 8bytes of 0 /*All NTP fields has been given values its time to send a packets that request a timestamp*/ //Set NTP requests to port 123 udp.beginPacket(address, 123);udp.write(packetBuffer, NTP_PACKET_SIZE);udp.endPacket(); } |