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.

106 lines
3.0 KiB

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