diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index ad30eb7f7..a08bea0fc 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -7,6 +7,7 @@ * Remove command SetOption62 as it's functionality is replaced by user changing the device template (#5255) * Add property LinkCount to state and status 11 message representing number of Wifi Link re-connections * Add property MqttCount to status 6 message representing number of Mqtt re-connections + * Add property Downtime to state and status 11 message representing the duration of wifi connection loss * * 6.4.1.16 20190211 * Initial support for online template change using command Template or GUI Configure Other (#5177) diff --git a/sonoff/i18n.h b/sonoff/i18n.h index d2e15e475..7340a3484 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -54,6 +54,7 @@ #define D_JSON_DISTANCE "Distance" #define D_JSON_DNSSERVER "DNSServer" #define D_JSON_DONE "Done" +#define D_JSON_DOWNTIME "Downtime" #define D_JSON_ECO2 "eCO2" #define D_JSON_EMPTY "Empty" #define D_JSON_ENDDST "EndDST" // End Daylight Savings Time diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 26198507b..e54c23260 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -1775,8 +1775,8 @@ void MqttShowState(void) MqttShowPWMState(); } - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_WIFI "\":{\"" D_JSON_AP "\":%d,\"" D_JSON_SSID "\":\"%s\",\"" D_JSON_BSSID "\":\"%s\",\"" D_JSON_CHANNEL "\":%d,\"" D_JSON_RSSI "\":%d,\"" D_JSON_LINK_COUNT "\":%d}}"), - mqtt_data, Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active], WiFi.BSSIDstr().c_str(), WiFi.channel(), WifiGetRssiAsQuality(WiFi.RSSI()), WifiLinkCount()); + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_WIFI "\":{\"" D_JSON_AP "\":%d,\"" D_JSON_SSID "\":\"%s\",\"" D_JSON_BSSID "\":\"%s\",\"" D_JSON_CHANNEL "\":%d,\"" D_JSON_RSSI "\":%d,\"" D_JSON_LINK_COUNT "\":%d,\"" D_JSON_DOWNTIME "\":\"%s\"}}"), + mqtt_data, Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active], WiFi.BSSIDstr().c_str(), WiFi.channel(), WifiGetRssiAsQuality(WiFi.RSSI()), WifiLinkCount(), WifiDowntime().c_str()); } bool MqttShowSensor(void) diff --git a/sonoff/support_rtc.ino b/sonoff/support_rtc.ino index 12a4d89ec..5c9f9e2b4 100644 --- a/sonoff/support_rtc.ino +++ b/sonoff/support_rtc.ino @@ -87,6 +87,23 @@ String GetTimeZone(void) return String(tz); // -03:45 } +String GetDuration(uint32_t time) +{ + char dt[16]; + + TIME_T ut; + BreakTime(time, ut); + + // "P128DT14H35M44S" - ISO8601:2004 - https://en.wikipedia.org/wiki/ISO_8601 Durations +// snprintf_P(dt, sizeof(dt), PSTR("P%dDT%02dH%02dM%02dS"), ut.days, ut.hour, ut.minute, ut.second); + + // "128 14:35:44" - OpenVMS + // "128T14:35:44" - Tasmota + snprintf_P(dt, sizeof(dt), PSTR("%dT%02d:%02d:%02d"), ut.days, ut.hour, ut.minute, ut.second); + + return String(dt); // 128T14:35:44 +} + String GetDT(uint32_t time) { // "2017-03-07T11:08:02" - ISO8601:2004 @@ -155,37 +172,24 @@ String GetTime(int type) return String(stime); // Thu Nov 01 11:41:02 2018 } +uint32_t UpTime(void) +{ + if (restart_time) { + return utc_time - restart_time; + } else { + return uptime; + } +} + String GetUptime(void) { - char dt[16]; - - TIME_T ut; - - if (restart_time) { - BreakTime(utc_time - restart_time, ut); - } else { - BreakTime(uptime, ut); - } - - // "P128DT14H35M44S" - ISO8601:2004 - https://en.wikipedia.org/wiki/ISO_8601 Durations -// snprintf_P(dt, sizeof(dt), PSTR("P%dDT%02dH%02dM%02dS"), ut.days, ut.hour, ut.minute, ut.second); - - // "128 14:35:44" - OpenVMS - // "128T14:35:44" - Tasmota - snprintf_P(dt, sizeof(dt), PSTR("%dT%02d:%02d:%02d"), ut.days, ut.hour, ut.minute, ut.second); - - return String(dt); // 128T14:35:44 + return GetDuration(UpTime()); } uint32_t GetMinutesUptime(void) { TIME_T ut; - - if (restart_time) { - BreakTime(utc_time - restart_time, ut); - } else { - BreakTime(uptime, ut); - } + BreakTime(UpTime(), ut); return (ut.days *1440) + (ut.hour *60) + ut.minute; } diff --git a/sonoff/support_wifi.ino b/sonoff/support_wifi.ino index ba3f81ac8..c394005a1 100644 --- a/sonoff/support_wifi.ino +++ b/sonoff/support_wifi.ino @@ -49,7 +49,9 @@ using namespace axTLS; */ #include // Wifi, MQTT, Ota, WifiManager -uint16_t wifi_link_count = 0; +uint32_t wifi_last_event = 0; // Last wifi connection event +uint32_t wifi_downtime = 0; // Wifi down duration +uint16_t wifi_link_count = 0; // Number of wifi re-connect uint8_t wifi_counter; uint8_t wifi_retry_init; uint8_t wifi_retry; @@ -346,14 +348,21 @@ uint16_t WifiLinkCount() return wifi_link_count; } +String WifiDowntime() +{ + return GetDuration(wifi_downtime); +} + void WifiSetState(uint8_t state) { if (state == global_state.wifi_down) { if (state) { rules_flag.wifi_connected = 1; wifi_link_count++; + wifi_downtime += UpTime() - wifi_last_event; } else { rules_flag.wifi_disconnected = 1; + wifi_last_event = UpTime(); } } global_state.wifi_down = state ^1;