TimeNTP_ESP8266WiFi.ino 4.7 KB

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