From b48256b2e8e35e63190c551207d0fe5ffb533545 Mon Sep 17 00:00:00 2001 From: tastendruecker123 <111116980+tastendruecker123@users.noreply.github.com> Date: Fri, 19 Aug 2022 12:42:14 +0200 Subject: [PATCH 1/2] MQTT resub only when reconnected successful --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index da27a66d6..f94323539 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ tools/esp8266/binaries tools/esp8266/.vscode/extensions.json .DS_Store .vscode +tools/esp8266/platformio-device-monitor-*.log From e59ca5f3885da0828a883e0299ac79af71a4bf3d Mon Sep 17 00:00:00 2001 From: tastendruecker123 <111116980+tastendruecker123@users.noreply.github.com> Date: Fri, 19 Aug 2022 12:43:47 +0200 Subject: [PATCH 2/2] MQTT subscribe only when reconnect successful --- tools/esp8266/mqtt.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index 789cec0cd..38999dc4e 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -54,7 +54,7 @@ class mqtt { bool isConnected(bool doRecon = false) { //DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected")); - if(doRecon) + if(doRecon && !mClient->connected()) reconnect(); return mClient->connected(); } @@ -71,6 +71,7 @@ class mqtt { DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect")); DPRINTLN(DBG_DEBUG, F("MQTT mClient->_state ") + String(mClient->state()) ); DPRINTLN(DBG_DEBUG, F("WIFI mEspClient.status ") + String(mEspClient.status()) ); + boolean resub = false; if(!mClient->connected()) { if(strlen(mDevName) > 0) { // der Server und der Port müssen neu gesetzt werden, @@ -78,16 +79,18 @@ class mqtt { mClient->setServer(mCfg->broker, mCfg->port); mClient->setBufferSize(MQTT_MAX_PACKET_SIZE); if((strlen(mCfg->user) > 0) && (strlen(mCfg->pwd) > 0)) - mClient->connect(mDevName, mCfg->user, mCfg->pwd); + resub = mClient->connect(mDevName, mCfg->user, mCfg->pwd); else - mClient->connect(mDevName); + resub = mClient->connect(mDevName); } // ein Subscribe ist nur nach einem connect notwendig - char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte - // ToDo: "/devcontrol/#" is hardcoded - snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mCfg->topic); - DPRINTLN(DBG_INFO, F("subscribe to ") + String(topic)); - mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#" + if(resub) { + char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte + // ToDo: "/devcontrol/#" is hardcoded + snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mCfg->topic); + DPRINTLN(DBG_INFO, F("subscribe to ") + String(topic)); + mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#" + } } }