PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 satır
4.1KB

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