diff --git a/code/espurna/data/index.html.gz b/code/espurna/data/index.html.gz index b6c2ec8b..43123798 100644 Binary files a/code/espurna/data/index.html.gz and b/code/espurna/data/index.html.gz differ diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 321d88d9..f9eafced 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -13,7 +13,8 @@ Copyright (C) 2016-2018 by Xose PĂ©rez #include #include -Ticker _ntp_delay; +bool _ntp_update = false; +bool _ntp_configure = false; // ----------------------------------------------------------------------------- // NTP @@ -31,21 +32,63 @@ void _ntpWebSocketOnSend(JsonObject& root) { } void _ntpUpdate() { + + _ntp_update = false; + #if WEB_SUPPORT wsSend(_ntpWebSocketOnSend); #endif + DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str()); + } void _ntpConfigure() { + + _ntp_configure = false; + + int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(); + int sign = offset > 0 ? 1 : -1; + offset = abs(offset); + NTP.begin( - getSetting("ntpServer1", NTP_SERVER), - getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(), - getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1 + getSetting("ntpServer", 1, NTP_SERVER).c_str(), + sign * (offset / 60), + getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1, + sign * (offset % 60) ); - if (getSetting("ntpServer2")) NTP.setNtpServerName(getSetting("ntpServer2"), 1); - if (getSetting("ntpServer3")) NTP.setNtpServerName(getSetting("ntpServer3"), 2); + + if (hasSetting("ntpServer", 2)) NTP.setNtpServerName(getSetting("ntpServer", 2).c_str(), 1); + if (hasSetting("ntpServer", 3)) NTP.setNtpServerName(getSetting("ntpServer", 3).c_str(), 2); NTP.setInterval(NTP_UPDATE_INTERVAL); + + _ntp_update = true; + +} + +void _ntpLoop() { + + if (_ntp_configure) _ntpConfigure(); + if (_ntp_update) _ntpUpdate(); + + now(); + + #if BROKER_SUPPORT + static unsigned char last_minute = 60; + if (ntpSynced() && (minute() != last_minute)) { + last_minute = minute(); + brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str()); + } + #endif + +} + +void _ntpBackwards() { + int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(); + if (-30 < offset && offset < 30) { + offset *= 60; + setSetting("ntpOffset", offset); + } } // ----------------------------------------------------------------------------- @@ -65,8 +108,12 @@ String ntpDateTime() { return String(buffer); } +// ----------------------------------------------------------------------------- + void ntpSetup() { + _ntpBackwards(); + NTP.onNTPSyncEvent([](NTPSyncEvent_t error) { if (error) { #if WEB_SUPPORT @@ -78,33 +125,21 @@ void ntpSetup() { DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n")); } } else { - _ntp_delay.once_ms(100, _ntpUpdate); + _ntp_update = true; } }); - _ntpConfigure(); + wifiRegister([](justwifi_messages_t code, char * parameter) { + if (code == MESSAGE_CONNECTED) _ntp_configure = true; + }); #if WEB_SUPPORT wsOnSendRegister(_ntpWebSocketOnSend); - wsOnAfterParseRegister(_ntpConfigure); + wsOnAfterParseRegister([]() { _ntp_configure = true; }); #endif // Register loop - espurnaRegisterLoop(ntpLoop); - -} - -void ntpLoop() { - - now(); - - #if BROKER_SUPPORT - static unsigned char last_minute = 60; - if (ntpSynced() && (minute() != last_minute)) { - last_minute = minute(); - brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str()); - } - #endif + espurnaRegisterLoop(_ntpLoop); } diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 8c2b30e5..c950d564 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -390,6 +390,7 @@ void _relayConfigure() { #if WEB_SUPPORT void _relayWebSocketUpdate(JsonObject& root) { + Serial.println("_relayWebSocketUpdate"); JsonArray& relay = root.createNestedArray("relayStatus"); for (unsigned char i=0; i= 0 ? value : -value; + var text = "GMT" + (value >= 0 ? "+" : "-") + + zeroPad(parseInt(offset / 60, 10), 2) + ":" + + zeroPad(offset % 60, 2); + $("select[name='ntpOffset']").append( + $(""). + attr("value",value). + text(text) + ); + } + +} + function validateForm(form) { // password @@ -1169,6 +1197,7 @@ function connect(host) { $(function() { initMessages(); + loadTimeZones(); $("#menuLink").on("click", toggleMenu); $(".pure-menu-link").on("click", showPanel); diff --git a/code/html/index.html b/code/html/index.html index bdd87b43..eb03dc88 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -724,6 +724,11 @@
+
+ + +
+
@@ -741,10 +746,7 @@
-
-
-
-
Set to 0 for UTC time
+
diff --git a/code/platformio.ini b/code/platformio.ini index ffe91680..4835845b 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -11,14 +11,13 @@ debug_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_SSL -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP build_flags_512k = ${common.build_flags} -Wl,-Tesp8266.flash.512k0.ld build_flags_1m = ${common.build_flags} -Wl,-Tesp8266.flash.1m0.ld lib_deps = - https://github.com/xoseperez/Time ArduinoJson https://github.com/me-no-dev/ESPAsyncTCP#a57560d https://github.com/me-no-dev/ESPAsyncWebServer#313f337 https://github.com/marvinroger/async-mqtt-client#v0.8.1 PubSubClient Embedis - NtpClientLib + https://github.com/gmag11/NtpClient.git#2.5.0 OneWire Brzo I2C https://github.com/krosk93/espsoftwareserial#a770677