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.

104 lines
3.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
6 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. #if NTP_SUPPORT
  6. #include <TimeLib.h>
  7. #include <NtpClientLib.h>
  8. #include <WiFiClient.h>
  9. #include <Ticker.h>
  10. WiFiEventHandler _ntp_wifi_onSTA;
  11. Ticker _ntp_delay;
  12. // -----------------------------------------------------------------------------
  13. // NTP
  14. // -----------------------------------------------------------------------------
  15. void _ntpWebSocketOnSend(JsonObject& root) {
  16. root["time"] = ntpDateTime();
  17. root["ntpVisible"] = 1;
  18. root["ntpStatus"] = ntpConnected();
  19. root["ntpServer1"] = getSetting("ntpServer1", NTP_SERVER);
  20. root["ntpServer2"] = getSetting("ntpServer2");
  21. root["ntpServer3"] = getSetting("ntpServer3");
  22. root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
  23. root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
  24. }
  25. void _ntpUpdate() {
  26. #if WEB_SUPPORT
  27. wsSend(_ntpWebSocketOnSend);
  28. #endif
  29. DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
  30. }
  31. void _ntpConfigure() {
  32. NTP.begin(
  33. getSetting("ntpServer1", NTP_SERVER),
  34. getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(),
  35. getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1
  36. );
  37. if (getSetting("ntpServer2")) NTP.setNtpServerName(getSetting("ntpServer2"), 1);
  38. if (getSetting("ntpServer3")) NTP.setNtpServerName(getSetting("ntpServer3"), 2);
  39. NTP.setInterval(NTP_UPDATE_INTERVAL);
  40. }
  41. // -----------------------------------------------------------------------------
  42. bool ntpConnected() {
  43. return (timeStatus() == timeSet);
  44. }
  45. String ntpDateTime() {
  46. if (!ntpConnected()) return String("Not set");
  47. String value = NTP.getTimeDateString();
  48. int hour = value.substring(0, 2).toInt();
  49. int minute = value.substring(3, 5).toInt();
  50. int second = value.substring(6, 8).toInt();
  51. int day = value.substring(9, 11).toInt();
  52. int month = value.substring(12, 14).toInt();
  53. int year = value.substring(15, 19).toInt();
  54. char buffer[20];
  55. snprintf_P(buffer, sizeof(buffer), PSTR("%04d-%02d-%02d %02d:%02d:%02d"), year, month, day, hour, minute, second);
  56. return String(buffer);
  57. }
  58. void ntpSetup() {
  59. NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
  60. if (error) {
  61. #if WEB_SUPPORT
  62. wsSend_P(PSTR("{\"ntpStatus\": false}"));
  63. #endif
  64. if (error == noResponse) {
  65. DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
  66. } else if (error == invalidAddress) {
  67. DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
  68. }
  69. } else {
  70. _ntp_delay.once_ms(100, _ntpUpdate);
  71. }
  72. });
  73. _ntp_wifi_onSTA = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
  74. _ntpConfigure();
  75. });
  76. #if WEB_SUPPORT
  77. wsOnSendRegister(_ntpWebSocketOnSend);
  78. wsOnAfterParseRegister(_ntpConfigure);
  79. #endif
  80. }
  81. void ntpLoop() {
  82. now();
  83. }
  84. #endif // NTP_SUPPORT