|
| 1 | +/* |
| 2 | +
|
| 3 | + Udp NTP Client |
| 4 | +
|
| 5 | + Get the time from a Network Time Protocol (NTP) time server |
| 6 | + Demonstrates use of UDP sendPacket and ReceivePacket |
| 7 | + For more on NTP time servers and the messages needed to communicate with them, |
| 8 | + see http://en.wikipedia.org/wiki/Network_Time_Protocol |
| 9 | +
|
| 10 | + created 4 Sep 2010 |
| 11 | + by Michael Margolis |
| 12 | + modified 9 Apr 2012 |
| 13 | + by Tom Igoe |
| 14 | +
|
| 15 | + This code is in the public domain. |
| 16 | +
|
| 17 | + Modified by David Henry to show where all the 'magic numbers' come from. |
| 18 | + You need to read the RFC-1305 spec to understand https://tools.ietf.org/html/rfc1305 |
| 19 | + |
| 20 | +
|
| 21 | +*/ |
| 22 | + |
| 23 | +#include <SPI.h> |
| 24 | +#include <Ethernet.h> |
| 25 | +#include <EthernetUdp.h> |
| 26 | +#include "RFC1305.h" |
| 27 | + |
| 28 | +// Enter a MAC address for your controller below. |
| 29 | +// Newer Ethernet shields have a MAC address printed on a sticker on the shield |
| 30 | +byte mac[] = { |
| 31 | + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED |
| 32 | +}; |
| 33 | + |
| 34 | +unsigned int localPort = 8888; // local port to listen for UDP packets |
| 35 | + |
| 36 | +char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server |
| 37 | + |
| 38 | +#define NTP_PACKET_SIZE sizeof(struct sRFC1305) |
| 39 | + |
| 40 | +struct sRFC1305 packetBuffer; //buffer to hold incoming and outgoing packets |
| 41 | + |
| 42 | +// A UDP instance to let us send and receive packets over UDP |
| 43 | +EthernetUDP Udp; |
| 44 | + |
| 45 | +void setup() |
| 46 | +{ |
| 47 | + // Open serial communications and wait for port to open: |
| 48 | + Serial.begin(9600); |
| 49 | + while (!Serial) { |
| 50 | + ; // wait for serial port to connect. Needed for Leonardo only |
| 51 | + } |
| 52 | + //Serial.println(NTP_PACKET_SIZE); // just for debugging |
| 53 | + //Serial.println(ENDIAN_SWAP_32(0x11223344),HEX); |
| 54 | + //Serial.println(ENDIAN_SWAP_16(0xAABB),HEX); |
| 55 | + // start Ethernet and UDP |
| 56 | + if (Ethernet.begin(mac) == 0) { |
| 57 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 58 | + // no point in carrying on, so do nothing forevermore: |
| 59 | + for (;;) |
| 60 | + ; |
| 61 | + } |
| 62 | + Udp.begin(localPort); |
| 63 | +} |
| 64 | + |
| 65 | +void loop() |
| 66 | +{ |
| 67 | + sendNTPpacket(timeServer); // send an NTP packet to a time server |
| 68 | + |
| 69 | + // wait to see if a reply is available |
| 70 | + delay(1000); |
| 71 | + if ( Udp.parsePacket() ) { |
| 72 | + // We've received a packet, read the data from it |
| 73 | + Udp.read((byte *)&packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer |
| 74 | +#if 0 // just for debugging |
| 75 | + Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_main),HEX); |
| 76 | + Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_fraction),HEX); |
| 77 | + Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdispersion_main),HEX); |
| 78 | + Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdispersion_fraction),HEX); |
| 79 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.referencetimestamp_main),HEX); |
| 80 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.referencetimestamp_fraction),HEX); |
| 81 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.origintimestamp_main),HEX); |
| 82 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.origintimestamp_fraction),HEX); |
| 83 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.receivetimestamp_main),HEX); |
| 84 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.receivetimestamp_fraction),HEX); |
| 85 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_main),HEX); |
| 86 | + Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_fraction),HEX); |
| 87 | +#endif |
| 88 | + Serial.print("Delay "); |
| 89 | + Serial.print(ENDIAN_SWAP_16(packetBuffer.rootdelay_main));Serial.print(".");Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_fraction)); |
| 90 | + Serial.print("Seconds since Jan 1 1900 = " ); |
| 91 | + unsigned long secsSince1900 = ENDIAN_SWAP_32(packetBuffer.transmittimestamp_main); |
| 92 | + Serial.print(secsSince1900);Serial.print(".");Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_fraction)); |
| 93 | + |
| 94 | + // now convert NTP time into everyday time: |
| 95 | + Serial.print("Unix time = "); |
| 96 | + // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: |
| 97 | + const unsigned long seventyYears = 2208988800UL; |
| 98 | + // subtract seventy years: |
| 99 | + unsigned long epoch = secsSince1900 - seventyYears; |
| 100 | + // print Unix time: |
| 101 | + Serial.println(epoch); |
| 102 | + |
| 103 | +#define SECS_PER_MINUTE 60 |
| 104 | +#define SECS_PER_HOUR 3600 |
| 105 | +#define SECS_PER_DAY 86400L |
| 106 | + |
| 107 | + // print the hour, minute and second: |
| 108 | + Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) |
| 109 | + Serial.print((epoch % SECS_PER_DAY) / SECS_PER_HOUR); |
| 110 | + Serial.print(':'); |
| 111 | + if ( ((epoch % SECS_PER_HOUR) / SECS_PER_MINUTE) < 10 ) { |
| 112 | + // In the first 10 minutes of each hour, we'll want a leading '0' |
| 113 | + Serial.print('0'); |
| 114 | + } |
| 115 | + Serial.print((epoch % SECS_PER_HOUR) / SECS_PER_MINUTE); |
| 116 | + Serial.print(':'); |
| 117 | + if ( (epoch % SECS_PER_MINUTE) < 10 ) { |
| 118 | + // In the first 10 seconds of each minute, we'll want a leading '0' |
| 119 | + Serial.print('0'); |
| 120 | + } |
| 121 | + Serial.println(epoch % SECS_PER_MINUTE); // print the second |
| 122 | +} |
| 123 | + // wait ten seconds before asking for the time again |
| 124 | + delay(10000); |
| 125 | +} |
| 126 | + |
| 127 | +// send an NTP request to the time server at the given address |
| 128 | +unsigned long sendNTPpacket(char* address) |
| 129 | +{ |
| 130 | + // set all bytes in the buffer to 0 |
| 131 | + memset((char *)&packetBuffer, 0, NTP_PACKET_SIZE); |
| 132 | + // Initialize values needed to form NTP request |
| 133 | + // (see URL above for details on the packets) |
| 134 | + packetBuffer.LI = LI_ALARM; |
| 135 | + packetBuffer.VN = VERN; |
| 136 | + packetBuffer.MODE = MODE_CLIENT; |
| 137 | + packetBuffer.stratum = 0; |
| 138 | + packetBuffer.poll = 6; |
| 139 | + packetBuffer.precision = -20; // ? copied from original UdnNtpClient code |
| 140 | + packetBuffer.identifier[0] = '1'; // I've no idea where this ID comes from |
| 141 | + packetBuffer.identifier[1] = 'N'; |
| 142 | + packetBuffer.identifier[2] = '1'; |
| 143 | + packetBuffer.identifier[3] = '4'; |
| 144 | +// Serial.println(*(uint8_t *)&packetBuffer,HEX); |
| 145 | + // all NTP fields have been given values, now |
| 146 | + // you can send a packet requesting a timestamp: |
| 147 | + Udp.beginPacket(address, 123); //NTP requests are to port 123 |
| 148 | + Udp.write((byte *)&packetBuffer, NTP_PACKET_SIZE); |
| 149 | + Udp.endPacket(); |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
0 commit comments