UdpNtpClient.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Udp NTP Client
  3. Get the time from a Network Time Protocol (NTP) time server
  4. Demonstrates use of UDP sendPacket and ReceivePacket
  5. For more on NTP time servers and the messages needed to communicate with them,
  6. see http://en.wikipedia.org/wiki/Network_Time_Protocol
  7. created 4 Sep 2010
  8. by Michael Margolis
  9. modified 9 Apr 2012
  10. by Tom Igoe
  11. This code is in the public domain.
  12. */
  13. #include <SPI.h>
  14. #include <Ethernet2.h>
  15. #include <EthernetUdp2.h>
  16. // Enter a MAC address for your controller below.
  17. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  18. byte mac[] = {
  19. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  20. };
  21. unsigned int localPort = 8888; // local port to listen for UDP packets
  22. char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
  23. const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
  24. byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
  25. // A UDP instance to let us send and receive packets over UDP
  26. EthernetUDP Udp;
  27. void setup()
  28. {
  29. // Open serial communications and wait for port to open:
  30. Serial.begin(9600);
  31. while (!Serial) {
  32. ; // wait for serial port to connect. Needed for Leonardo only
  33. }
  34. // start Ethernet and UDP
  35. if (Ethernet.begin(mac) == 0) {
  36. Serial.println("Failed to configure Ethernet using DHCP");
  37. // no point in carrying on, so do nothing forevermore:
  38. for (;;)
  39. ;
  40. }
  41. Udp.begin(localPort);
  42. }
  43. void loop()
  44. {
  45. sendNTPpacket(timeServer); // send an NTP packet to a time server
  46. // wait to see if a reply is available
  47. delay(1000);
  48. if ( Udp.parsePacket() ) {
  49. // We've received a packet, read the data from it
  50. Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
  51. //the timestamp starts at byte 40 of the received packet and is four bytes,
  52. // or two words, long. First, esxtract the two words:
  53. unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  54. unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  55. // combine the four bytes (two words) into a long integer
  56. // this is NTP time (seconds since Jan 1 1900):
  57. unsigned long secsSince1900 = highWord << 16 | lowWord;
  58. Serial.print("Seconds since Jan 1 1900 = " );
  59. Serial.println(secsSince1900);
  60. // now convert NTP time into everyday time:
  61. Serial.print("Unix time = ");
  62. // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  63. const unsigned long seventyYears = 2208988800UL;
  64. // subtract seventy years:
  65. unsigned long epoch = secsSince1900 - seventyYears;
  66. // print Unix time:
  67. Serial.println(epoch);
  68. // print the hour, minute and second:
  69. Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
  70. Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
  71. Serial.print(':');
  72. if ( ((epoch % 3600) / 60) < 10 ) {
  73. // In the first 10 minutes of each hour, we'll want a leading '0'
  74. Serial.print('0');
  75. }
  76. Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
  77. Serial.print(':');
  78. if ( (epoch % 60) < 10 ) {
  79. // In the first 10 seconds of each minute, we'll want a leading '0'
  80. Serial.print('0');
  81. }
  82. Serial.println(epoch % 60); // print the second
  83. }
  84. // wait ten seconds before asking for the time again
  85. delay(10000);
  86. }
  87. // send an NTP request to the time server at the given address
  88. unsigned long sendNTPpacket(char* address)
  89. {
  90. // set all bytes in the buffer to 0
  91. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  92. // Initialize values needed to form NTP request
  93. // (see URL above for details on the packets)
  94. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  95. packetBuffer[1] = 0; // Stratum, or type of clock
  96. packetBuffer[2] = 6; // Polling Interval
  97. packetBuffer[3] = 0xEC; // Peer Clock Precision
  98. // 8 bytes of zero for Root Delay & Root Dispersion
  99. packetBuffer[12] = 49;
  100. packetBuffer[13] = 0x4E;
  101. packetBuffer[14] = 49;
  102. packetBuffer[15] = 52;
  103. // all NTP fields have been given values, now
  104. // you can send a packet requesting a timestamp:
  105. Udp.beginPacket(address, 123); //NTP requests are to port 123
  106. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  107. Udp.endPacket();
  108. }