PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

157 行
5.0KB

  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. modified 02 Sept 2015
  12. by Arturo Guadalupi
  13. This code is in the public domain.
  14. */
  15. #include <SPI.h>
  16. #include <Ethernet.h>
  17. #include <EthernetUdp.h>
  18. // Enter a MAC address for your controller below.
  19. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  20. byte mac[] = {
  21. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  22. };
  23. unsigned int localPort = 8888; // local port to listen for UDP packets
  24. const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
  25. const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
  26. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
  27. // A UDP instance to let us send and receive packets over UDP
  28. EthernetUDP Udp;
  29. void setup() {
  30. // You can use Ethernet.init(pin) to configure the CS pin
  31. //Ethernet.init(10); // Most Arduino shields
  32. //Ethernet.init(5); // MKR ETH shield
  33. //Ethernet.init(0); // Teensy 2.0
  34. //Ethernet.init(20); // Teensy++ 2.0
  35. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  36. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  37. // Open serial communications and wait for port to open:
  38. Serial.begin(9600);
  39. while (!Serial) {
  40. ; // wait for serial port to connect. Needed for native USB port only
  41. }
  42. // start Ethernet and UDP
  43. if (Ethernet.begin(mac) == 0) {
  44. Serial.println("Failed to configure Ethernet using DHCP");
  45. // Check for Ethernet hardware present
  46. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  47. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  48. } else if (Ethernet.linkStatus() == LinkOFF) {
  49. Serial.println("Ethernet cable is not connected.");
  50. }
  51. // no point in carrying on, so do nothing forevermore:
  52. while (true) {
  53. delay(1);
  54. }
  55. }
  56. Udp.begin(localPort);
  57. }
  58. void loop() {
  59. sendNTPpacket(timeServer); // send an NTP packet to a time server
  60. // wait to see if a reply is available
  61. delay(1000);
  62. if (Udp.parsePacket()) {
  63. // We've received a packet, read the data from it
  64. Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
  65. // the timestamp starts at byte 40 of the received packet and is four bytes,
  66. // or two words, long. First, extract the two words:
  67. unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  68. unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  69. // combine the four bytes (two words) into a long integer
  70. // this is NTP time (seconds since Jan 1 1900):
  71. unsigned long secsSince1900 = highWord << 16 | lowWord;
  72. Serial.print("Seconds since Jan 1 1900 = ");
  73. Serial.println(secsSince1900);
  74. // now convert NTP time into everyday time:
  75. Serial.print("Unix time = ");
  76. // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  77. const unsigned long seventyYears = 2208988800UL;
  78. // subtract seventy years:
  79. unsigned long epoch = secsSince1900 - seventyYears;
  80. // print Unix time:
  81. Serial.println(epoch);
  82. // print the hour, minute and second:
  83. Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
  84. Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
  85. Serial.print(':');
  86. if (((epoch % 3600) / 60) < 10) {
  87. // In the first 10 minutes of each hour, we'll want a leading '0'
  88. Serial.print('0');
  89. }
  90. Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
  91. Serial.print(':');
  92. if ((epoch % 60) < 10) {
  93. // In the first 10 seconds of each minute, we'll want a leading '0'
  94. Serial.print('0');
  95. }
  96. Serial.println(epoch % 60); // print the second
  97. }
  98. // wait ten seconds before asking for the time again
  99. delay(10000);
  100. Ethernet.maintain();
  101. }
  102. // send an NTP request to the time server at the given address
  103. void sendNTPpacket(const char * address) {
  104. // set all bytes in the buffer to 0
  105. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  106. // Initialize values needed to form NTP request
  107. // (see URL above for details on the packets)
  108. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  109. packetBuffer[1] = 0; // Stratum, or type of clock
  110. packetBuffer[2] = 6; // Polling Interval
  111. packetBuffer[3] = 0xEC; // Peer Clock Precision
  112. // 8 bytes of zero for Root Delay & Root Dispersion
  113. packetBuffer[12] = 49;
  114. packetBuffer[13] = 0x4E;
  115. packetBuffer[14] = 49;
  116. packetBuffer[15] = 52;
  117. // all NTP fields have been given values, now
  118. // you can send a packet requesting a timestamp:
  119. Udp.beginPacket(address, 123); // NTP requests are to port 123
  120. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  121. Udp.endPacket();
  122. }