From a7dc6b71ae952bc390ce420f6856c86e97613510 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 7 Jun 2019 18:03:34 +0200 Subject: [PATCH] Add some MQTT housekeeping which might solve issue (#5755) Add some MQTT housekeeping which might solve issue (#5755) --- sonoff/_changelog.ino | 1 + sonoff/sonoff_post.h | 3 +++ sonoff/xdrv_02_mqtt.ino | 7 ++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 6229fdaa1..4811e0073 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -2,6 +2,7 @@ * 6.5.0.15 20190606 * Change pubsubclient MQTT_KEEPALIVE from 10 to 30 seconds in preparation of AWS IoT support * Add support for AWS IoT with TLS 1.2 on core 2.5.2. Full doc here: https://github.com/arendst/Sonoff-Tasmota/wiki/AWS-IoT + * Add some MQTT housekeeping which might solve issue (#5755) * * 6.5.0.14 20190602 * Change webserver HTML input, button, textarea, and select name based on id diff --git a/sonoff/sonoff_post.h b/sonoff/sonoff_post.h index 99868a1ba..416f1a3d0 100644 --- a/sonoff/sonoff_post.h +++ b/sonoff/sonoff_post.h @@ -453,6 +453,9 @@ void KNX_CB_Action(message_t const &msg, void *arg); #ifndef MQTT_TIMEOUT #define MQTT_TIMEOUT 10000 // milli seconds #endif +#ifndef MQTT_CLEAN_SESSION +#define MQTT_CLEAN_SESSION 1 // 0 = No clean session, 1 = Clean session (default) +#endif #ifndef MESSZ //#define MESSZ 405 // Max number of characters in JSON message string (6 x DS18x20 sensors) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 1bab26de3..d1a0b8d26 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -345,6 +345,8 @@ void MqttDisconnected(int state) mqtt_connected = false; mqtt_retry_counter = Settings.mqtt_retry; + MqttClient.disconnect(); + #ifdef USE_MQTT_AWS_IOT AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT D_CONNECT_FAILED_TO " %s:%d, rc %d. " D_RETRY_IN " %d " D_UNIT_SECOND), AWS_endpoint, Settings.mqtt_port, state, mqtt_retry_counter); #else @@ -508,12 +510,15 @@ void MqttReconnect(void) GetTopic_P(stopic, TELE, mqtt_topic, S_LWT); Response_P(S_OFFLINE); + if (MqttClient.connected()) { MqttClient.disconnect(); } #ifdef USE_MQTT_TLS EspClient = WiFiClientSecure(); // Wifi Secure Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497) + MqttClient.setClient(EspClient); #elif defined(USE_MQTT_AWS_IOT) awsClient->stop(); #else EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497) + MqttClient.setClient(EspClient); #endif if (2 == mqtt_initial_connection_state) { // Executed once just after power on and wifi is connected @@ -552,7 +557,7 @@ void MqttReconnect(void) AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "AWS IoT endpoint: %s"), AWS_endpoint); if (MqttClient.connect(mqtt_client, mqtt_user, mqtt_pwd, nullptr, 0, false, nullptr)) { #else - if (MqttClient.connect(mqtt_client, mqtt_user, mqtt_pwd, stopic, 1, true, mqtt_data)) { + if (MqttClient.connect(mqtt_client, mqtt_user, mqtt_pwd, stopic, 1, true, mqtt_data, MQTT_CLEAN_SESSION)) { #endif #ifdef USE_MQTT_AWS_IOT AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "AWS IoT connected in %d ms"), millis() - time);