Fork of the espurna firmware for `mhsw` switches
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.

44 lines
1.1 KiB

8 years ago
8 years ago
8 years ago
  1. /*
  2. ESPURNA
  3. NTP MODULE
  4. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #include <TimeLib.h>
  7. #include <NtpClientLib.h>
  8. #include <WiFiClient.h>
  9. // -----------------------------------------------------------------------------
  10. // NTP
  11. // -----------------------------------------------------------------------------
  12. void ntpConnect(WiFiEventStationModeGotIP ipInfo) {
  13. NTP.begin(NTP_SERVER, NTP_TIME_OFFSET, NTP_DAY_LIGHT);
  14. NTP.setInterval(NTP_UPDATE_INTERVAL);
  15. }
  16. void ntpSetup() {
  17. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  18. if (error) {
  19. if (error == noResponse) {
  20. DEBUG_MSG("[NTP] Error: NTP server not reachable\n");
  21. } else if (error == invalidAddress) {
  22. DEBUG_MSG("[NTP] Error: Invalid NTP server address\n");
  23. }
  24. } else {
  25. DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString(NTP.getLastNTPSync()).c_str());
  26. }
  27. });
  28. static WiFiEventHandler e;
  29. e = WiFi.onStationModeGotIP(ntpConnect);
  30. }
  31. void ntpLoop() {
  32. now();
  33. }