From 4884a94b6d643a40ca60bdd4a6638b82bb185b18 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 18 Feb 2019 17:05:25 +0100 Subject: [PATCH] Add LinkCount and MqttCount * 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 --- sonoff/_changelog.ino | 2 ++ sonoff/i18n.h | 3 ++- sonoff/sonoff.ino | 8 ++++---- sonoff/support_wifi.ino | 8 ++++---- sonoff/xdrv_02_mqtt.ino | 7 +++++++ 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 8872f0e4a..ad30eb7f7 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -5,6 +5,8 @@ * Add rule expression enabled by define USE_EXPRESSION in my_user_config.h (#5210) * Add Configure Template menu option to GUI (#5222) * 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 * * 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 b5a7f7697..d2e15e475 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -89,13 +89,14 @@ #define D_JSON_INFRARED "Infrared" #define D_JSON_UNKNOWN "Unknown" #define D_JSON_LIGHT "Light" -#define D_JSON_LINK_DOWN "LinkCount" +#define D_JSON_LINK_COUNT "LinkCount" #define D_JSON_LOCAL_TIME "Local" #define D_JSON_LOW "Low" #define D_JSON_MAC "Mac" #define D_JSON_MASK "Mask" #define D_JSON_MINIMAL "minimal" #define D_JSON_MODEL "Model" +#define D_JSON_MQTT_COUNT "MqttCount" #define D_JSON_NO "No" #define D_JSON_NOISE "Noise" #define D_JSON_NONE "None" diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 9e0f4530e..26198507b 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -1682,8 +1682,8 @@ void PublishStatus(uint8_t payload) } if (((0 == payload) || (6 == payload)) && Settings.flag.mqtt_enabled) { - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS6_MQTT "\":{\"" D_CMND_MQTTHOST "\":\"%s\",\"" D_CMND_MQTTPORT "\":%d,\"" D_CMND_MQTTCLIENT D_JSON_MASK "\":\"%s\",\"" D_CMND_MQTTCLIENT "\":\"%s\",\"" D_CMND_MQTTUSER "\":\"%s\",\"MqttType\":%d,\"MAX_PACKET_SIZE\":%d,\"KEEPALIVE\":%d}}"), - Settings.mqtt_host, Settings.mqtt_port, Settings.mqtt_client, mqtt_client, Settings.mqtt_user, MqttLibraryType(), MQTT_MAX_PACKET_SIZE, MQTT_KEEPALIVE); + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATUS D_STATUS6_MQTT "\":{\"" D_CMND_MQTTHOST "\":\"%s\",\"" D_CMND_MQTTPORT "\":%d,\"" D_CMND_MQTTCLIENT D_JSON_MASK "\":\"%s\",\"" D_CMND_MQTTCLIENT "\":\"%s\",\"" D_CMND_MQTTUSER "\":\"%s\",\"MqttType\":%d,\"" D_JSON_MQTT_COUNT "\":%d,\"MAX_PACKET_SIZE\":%d,\"KEEPALIVE\":%d}}"), + Settings.mqtt_host, Settings.mqtt_port, Settings.mqtt_client, mqtt_client, Settings.mqtt_user, MqttLibraryType(), MqttConnectCount(), MQTT_MAX_PACKET_SIZE, MQTT_KEEPALIVE); MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "6")); } @@ -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_DOWN "\":%d}}"), - mqtt_data, Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active], WiFi.BSSIDstr().c_str(), WiFi.channel(), WifiGetRssiAsQuality(WiFi.RSSI()), WifiLinkDown()); + 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()); } bool MqttShowSensor(void) diff --git a/sonoff/support_wifi.ino b/sonoff/support_wifi.ino index 138768138..ba3f81ac8 100644 --- a/sonoff/support_wifi.ino +++ b/sonoff/support_wifi.ino @@ -49,7 +49,7 @@ using namespace axTLS; */ #include // Wifi, MQTT, Ota, WifiManager -uint32_t wifi_link_down = 0; +uint16_t wifi_link_count = 0; uint8_t wifi_counter; uint8_t wifi_retry_init; uint8_t wifi_retry; @@ -341,9 +341,9 @@ void WifiBeginAfterScan() } } -uint32_t WifiLinkDown() +uint16_t WifiLinkCount() { - return wifi_link_down; + return wifi_link_count; } void WifiSetState(uint8_t state) @@ -351,9 +351,9 @@ void WifiSetState(uint8_t state) if (state == global_state.wifi_down) { if (state) { rules_flag.wifi_connected = 1; + wifi_link_count++; } else { rules_flag.wifi_disconnected = 1; - wifi_link_down++; } } global_state.wifi_down = state ^1; diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 64afb0ebd..fa681e309 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -73,6 +73,7 @@ const char kMqttCommands[] PROGMEM = D_CMND_MQTTUSER "|" D_CMND_MQTTPASSWORD "|" D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|" D_CMND_BUTTONTOPIC "|" D_CMND_SWITCHTOPIC "|" D_CMND_BUTTONRETAIN "|" D_CMND_SWITCHRETAIN "|" D_CMND_POWERRETAIN "|" D_CMND_SENSORRETAIN ; +uint16_t mqtt_connect_count = 0; // MQTT re-connect count uint16_t mqtt_retry_counter = 1; // MQTT connection retry counter uint8_t mqtt_initial_connection_state = 2; // MQTT connection messages state bool mqtt_connected = false; // MQTT virtual connection status @@ -379,6 +380,11 @@ void MqttPublishPowerBlinkState(uint8_t device) /*********************************************************************************************/ +uint16_t MqttConnectCount() +{ + return mqtt_connect_count; +} + void MqttDisconnected(int state) { mqtt_connected = false; @@ -398,6 +404,7 @@ void MqttConnected(void) AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_CONNECTED)); mqtt_connected = true; mqtt_retry_counter = 0; + mqtt_connect_count++; GetTopic_P(stopic, TELE, mqtt_topic, S_LWT); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR(D_ONLINE));