3 * Example showing time sync to NTP time source
5 * This sketch uses the Ethernet library
10 #include <EthernetUdp.h>
13 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
15 IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
16 // IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
17 // IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
20 const int timeZone = 1; // Central European Time
21 //const int timeZone = -5; // Eastern Standard Time (USA)
22 //const int timeZone = -4; // Eastern Daylight Time (USA)
23 //const int timeZone = -8; // Pacific Standard Time (USA)
24 //const int timeZone = -7; // Pacific Daylight Time (USA)
28 unsigned int localPort = 8888; // local port to listen for UDP packets
33 while (!Serial) ; // Needed for Leonardo only
35 Serial.println("TimeNTP Example");
36 if (Ethernet.begin(mac) == 0) {
37 // no point in carrying on, so do nothing forevermore:
39 Serial.println("Failed to configure Ethernet using DHCP");
43 Serial.print("IP number assigned by DHCP is ");
44 Serial.println(Ethernet.localIP());
46 Serial.println("waiting for sync");
47 setSyncProvider(getNtpTime);
50 time_t prevDisplay = 0; // when the digital clock was displayed
54 if (timeStatus() != timeNotSet) {
55 if (now() != prevDisplay) { //update the display only if time has changed
57 digitalClockDisplay();
62 void digitalClockDisplay(){
63 // digital clock display of the time
65 printDigits(minute());
66 printDigits(second());
70 Serial.print(month());
76 void printDigits(int digits){
77 // utility for digital clock display: prints preceding colon and leading 0
84 /*-------- NTP code ----------*/
86 const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
87 byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
91 while (Udp.parsePacket() > 0) ; // discard any previously received packets
92 Serial.println("Transmit NTP Request");
93 sendNTPpacket(timeServer);
94 uint32_t beginWait = millis();
95 while (millis() - beginWait < 1500) {
96 int size = Udp.parsePacket();
97 if (size >= NTP_PACKET_SIZE) {
98 Serial.println("Receive NTP Response");
99 Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
100 unsigned long secsSince1900;
101 // convert four bytes starting at location 40 to a long integer
102 secsSince1900 = (unsigned long)packetBuffer[40] << 24;
103 secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
104 secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
105 secsSince1900 |= (unsigned long)packetBuffer[43];
106 return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
109 Serial.println("No NTP Response :-(");
110 return 0; // return 0 if unable to get the time
113 // send an NTP request to the time server at the given address
114 void sendNTPpacket(IPAddress &address)
116 // set all bytes in the buffer to 0
117 memset(packetBuffer, 0, NTP_PACKET_SIZE);
118 // Initialize values needed to form NTP request
119 // (see URL above for details on the packets)
120 packetBuffer[0] = 0b11100011; // LI, Version, Mode
121 packetBuffer[1] = 0; // Stratum, or type of clock
122 packetBuffer[2] = 6; // Polling Interval
123 packetBuffer[3] = 0xEC; // Peer Clock Precision
124 // 8 bytes of zero for Root Delay & Root Dispersion
125 packetBuffer[12] = 49;
126 packetBuffer[13] = 0x4E;
127 packetBuffer[14] = 49;
128 packetBuffer[15] = 52;
129 // all NTP fields have been given values, now
130 // you can send a packet requesting a timestamp:
131 Udp.beginPacket(address, 123); //NTP requests are to port 123
132 Udp.write(packetBuffer, NTP_PACKET_SIZE);