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.

52 lines
1.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. NTP MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <TimeLib.h>
  6. #include <NtpClientLib.h>
  7. #include <WiFiClient.h>
  8. // -----------------------------------------------------------------------------
  9. // NTP
  10. // -----------------------------------------------------------------------------
  11. void ntpConnect() {
  12. NTP.begin(
  13. getSetting("ntpServer1", NTP_SERVER),
  14. getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(),
  15. getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1
  16. );
  17. if (getSetting("ntpServer2")) NTP.setNtpServerName(getSetting("ntpServer2"), 1);
  18. if (getSetting("ntpServer3")) NTP.setNtpServerName(getSetting("ntpServer3"), 2);
  19. NTP.setInterval(NTP_UPDATE_INTERVAL);
  20. }
  21. bool ntpConnected() {
  22. return (timeStatus() == timeSet);
  23. }
  24. void ntpSetup() {
  25. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  26. if (error) {
  27. if (error == noResponse) {
  28. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  29. } else if (error == invalidAddress) {
  30. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  31. }
  32. wsSend("{\"ntpStatus\": false}");
  33. } else {
  34. DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) NTP.getTimeDateString(NTP.getLastNTPSync()).c_str());
  35. wsSend("{\"ntpStatus\": true}");
  36. }
  37. });
  38. }
  39. void ntpLoop() {
  40. now();
  41. }