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.

43 lines
1.1 KiB

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(WiFiEventStationModeGotIP ipInfo) {
  12. NTP.begin(NTP_SERVER, NTP_TIME_OFFSET, NTP_DAY_LIGHT);
  13. NTP.setInterval(NTP_UPDATE_INTERVAL);
  14. }
  15. void ntpSetup() {
  16. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  17. if (error) {
  18. if (error == noResponse) {
  19. DEBUG_MSG("[NTP] Error: NTP server not reachable\n");
  20. } else if (error == invalidAddress) {
  21. DEBUG_MSG("[NTP] Error: Invalid NTP server address\n");
  22. }
  23. } else {
  24. DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString(NTP.getLastNTPSync()).c_str());
  25. }
  26. });
  27. static WiFiEventHandler e;
  28. e = WiFi.onStationModeGotIP(ntpConnect);
  29. }
  30. void ntpLoop() {
  31. now();
  32. }