ESP8266 SHT21 sensor
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.

ntp.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * ntp.cpp
  3. *
  4. * Simple NTP implementation to aquire the current time matched to the systick.
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file. As long as
  9. * you retain this notice you can do whatever you want with this stuff. If we
  10. * meet some day, and you think this stuff is worth it, you can buy us a beer
  11. * in return. Thomas Buck & Christian Högerle
  12. * ----------------------------------------------------------------------------
  13. */
  14. #include <Arduino.h>
  15. #include <ESP8266WiFi.h>
  16. #include <WiFiUdp.h>
  17. #include "config.h"
  18. #include "ntp.h"
  19. //#define DEBUG
  20. IPAddress timeServerIP;
  21. byte ntpPacketBuffer[NTP_PACKET_SIZE];
  22. WiFiUDP ntp;
  23. unsigned long lastNTP = 0; // timer for retries
  24. unsigned long timestamp = 0; // received epoch time
  25. unsigned long timeReceived = 0; // systick matching received epoch
  26. static void sendNTPpacket(IPAddress& address) {
  27. #ifdef DEBUG
  28. Serial.println("Sending NTP packet...");
  29. #endif
  30. memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
  31. ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
  32. ntpPacketBuffer[1] = 0;
  33. ntpPacketBuffer[2] = 6;
  34. ntpPacketBuffer[3] = 0xEC;
  35. ntpPacketBuffer[12] = 49;
  36. ntpPacketBuffer[13] = 0x4E;
  37. ntpPacketBuffer[14] = 49;
  38. ntpPacketBuffer[15] = 52;
  39. ntp.beginPacket(address, NTP_PORT_TO);
  40. ntp.write(ntpPacketBuffer, NTP_PACKET_SIZE);
  41. ntp.endPacket();
  42. }
  43. void ntpInit(void) {
  44. ntp.begin(NTP_PORT_FROM);
  45. WiFi.hostByName(NTP_SERVER_NAME, timeServerIP);
  46. lastNTP = millis();
  47. sendNTPpacket(timeServerIP);
  48. }
  49. void ntpRun(void) {
  50. // Retry if we haven't received an answer soon enough
  51. if ((timestamp == 0) && ((millis() - lastNTP) > NTP_RETRY_TIMEOUT)) {
  52. #ifdef DEBUG
  53. Serial.println("NTP packet retry...");
  54. #endif // DEBUG
  55. WiFi.hostByName(NTP_SERVER_NAME, timeServerIP);
  56. lastNTP = millis();
  57. sendNTPpacket(timeServerIP);
  58. }
  59. // Process received response
  60. if (ntp.parsePacket() >= NTP_PACKET_SIZE) {
  61. ntp.read(ntpPacketBuffer, NTP_PACKET_SIZE);
  62. unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
  63. unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
  64. unsigned long secsSince1900 = highWord << 16 | lowWord;
  65. const unsigned long seventyYears = 2208988800UL;
  66. unsigned long epoch = secsSince1900 - seventyYears;
  67. timestamp = epoch;
  68. timeReceived = millis();
  69. #ifdef DEBUG
  70. Serial.print("Got NTP time: ");
  71. Serial.println(epoch);
  72. #endif // DEBUG
  73. }
  74. }