|
| 1 | +/* |
| 2 | + Udp NTP Client |
| 3 | +
|
| 4 | + Get the time from a Network Time Protocol (NTP) time server |
| 5 | + Demonstrates use of UDP sendPacket and ReceivePacket |
| 6 | + For more on NTP time servers and the messages needed to communicate with them, |
| 7 | + see https://en.wikipedia.org/wiki/Network_Time_Protocol |
| 8 | +
|
| 9 | + Example freely inspired from the Arduino Ethernet library |
| 10 | + https://github.com/arduino-libraries/Ethernet/blob/master/examples/UdpNtpClient/UdpNtpClient.ino |
| 11 | + |
| 12 | + This code is in the public domain. |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <Arduino_RouterBridge.h> |
| 16 | + |
| 17 | +unsigned int localPort = 8888; // local port to listen for UDP packets |
| 18 | +const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server |
| 19 | +int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message |
| 20 | + |
| 21 | +// A UDP instance to let us send and receive packets over UDP |
| 22 | +BridgeUDP<4096> Udp(Bridge); |
| 23 | + |
| 24 | +void setup() { |
| 25 | + Monitor.begin(); |
| 26 | + Udp.begin(localPort); |
| 27 | +} |
| 28 | + |
| 29 | +void loop() { |
| 30 | + sendNTPpacket(timeServer); // send an NTP packet to a time server |
| 31 | + |
| 32 | + // wait one second to see if a reply is available |
| 33 | + Udp.setTimeout(1000); |
| 34 | + if (Udp.parsePacket()) { |
| 35 | + // We've received a packet, read the data from it |
| 36 | + byte packetBuffer[NTP_PACKET_SIZE]; |
| 37 | + Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer |
| 38 | + |
| 39 | + // the timestamp starts at byte 40 of the received packet and is four bytes, |
| 40 | + // or two words, long. |
| 41 | + |
| 42 | + // First, extract the two words: |
| 43 | + unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); |
| 44 | + unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); |
| 45 | + |
| 46 | + // combine the four bytes (two words) into a long integer |
| 47 | + // this is NTP time (seconds since Jan 1 1900): |
| 48 | + unsigned long secsSince1900 = highWord << 16 | lowWord; |
| 49 | + Monitor.print("Seconds since Jan 1 1900 = "); |
| 50 | + Monitor.println(secsSince1900); |
| 51 | + |
| 52 | + // now convert NTP time into everyday time: |
| 53 | + // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: |
| 54 | + const unsigned long seventyYears = 2208988800UL; |
| 55 | + // subtract seventy years: |
| 56 | + unsigned long epoch = secsSince1900 - seventyYears; |
| 57 | + Monitor.print("Unix time = "); |
| 58 | + Monitor.println(epoch); |
| 59 | + |
| 60 | + // print the hour, minute and second: |
| 61 | + Monitor.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) |
| 62 | + Monitor.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) |
| 63 | + Monitor.print(':'); |
| 64 | + if (((epoch % 3600) / 60) < 10) { |
| 65 | + // In the first 10 minutes of each hour, we'll want a leading '0' |
| 66 | + Monitor.print('0'); |
| 67 | + } |
| 68 | + Monitor.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) |
| 69 | + Monitor.print(':'); |
| 70 | + if ((epoch % 60) < 10) { |
| 71 | + // In the first 10 seconds of each minute, we'll want a leading '0' |
| 72 | + Monitor.print('0'); |
| 73 | + } |
| 74 | + Monitor.println(epoch % 60); // print the second |
| 75 | + } |
| 76 | + |
| 77 | + // wait ten seconds before asking for the time again |
| 78 | + delay(10000); |
| 79 | +} |
| 80 | + |
| 81 | +// send an NTP request to the time server at the given address |
| 82 | +void sendNTPpacket(const char * address) { |
| 83 | + byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets |
| 84 | + |
| 85 | + // set all bytes in the buffer to 0 |
| 86 | + memset(packetBuffer, 0, NTP_PACKET_SIZE); |
| 87 | + |
| 88 | + // Initialize values needed to form NTP request |
| 89 | + // (see URL above for details on the packets) |
| 90 | + packetBuffer[0] = 0b11100011; // LI, Version, Mode |
| 91 | + packetBuffer[1] = 0; // Stratum, or type of clock |
| 92 | + packetBuffer[2] = 6; // Polling Interval |
| 93 | + packetBuffer[3] = 0xEC; // Peer Clock Precision |
| 94 | + // 8 bytes of zero for Root Delay & Root Dispersion |
| 95 | + packetBuffer[12] = 49; |
| 96 | + packetBuffer[13] = 0x4E; |
| 97 | + packetBuffer[14] = 49; |
| 98 | + packetBuffer[15] = 52; |
| 99 | + |
| 100 | + // all NTP fields have been given values, now |
| 101 | + // you can send a packet requesting a timestamp: |
| 102 | + Udp.beginPacket(address, 123); // NTP requests are to port 123 |
| 103 | + Udp.write(packetBuffer, NTP_PACKET_SIZE); |
| 104 | + Udp.endPacket(); |
| 105 | +} |
| 106 | + |
0 commit comments