From 079ba896d13d53889e6b8390bcb68e38526e8d16 Mon Sep 17 00:00:00 2001 From: tastendruecker123 <111116980+tastendruecker123@users.noreply.github.com> Date: Mon, 22 Aug 2022 19:59:45 +0200 Subject: [PATCH] MQTT reconnect delay, fixed ESP32 build --- tools/esp8266/defines.h | 1 + tools/esp8266/mqtt.h | 20 +++++++++++--------- tools/esp8266/web.cpp | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index 31b65b77..4d55a7fc 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -104,6 +104,7 @@ typedef enum { // ToDo: to be verified by field tests #define MQTT_PORT_LEN 2 // uint16_t #define MQTT_DISCOVERY_PREFIX "homeassistant" #define MQTT_MAX_PACKET_SIZE 384 +#define MQTT_RECONNECT_DELAY 5000 #define SER_ENABLE_LEN 1 // uint8_t #define SER_DEBUG_LEN 1 // uint8_t diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index 74193afa..28ca2c1c 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -85,7 +85,8 @@ class mqtt { #endif boolean resub = false; - if(!mClient->connected()) { + if(!mClient->connected() && (millis() - lastReconnect) > MQTT_RECONNECT_DELAY ) { + lastReconnect = millis(); if(strlen(mDevName) > 0) { // der Server und der Port müssen neu gesetzt werden, // da ein MQTT_CONNECTION_LOST -3 die Werte zerstört hat. @@ -95,14 +96,14 @@ class mqtt { resub = mClient->connect(mDevName, mCfg->user, mCfg->pwd); else resub = mClient->connect(mDevName); - } - // ein Subscribe ist nur nach einem connect notwendig - 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/#" + // ein Subscribe ist nur nach einem connect notwendig + 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/#" + } } } } @@ -113,6 +114,7 @@ class mqtt { bool mAddressSet; mqttConfig_t *mCfg; char mDevName[DEVNAME_LEN]; + unsigned long lastReconnect = 0; }; #endif /*__MQTT_H_*/ diff --git a/tools/esp8266/web.cpp b/tools/esp8266/web.cpp index 5ccdb002..72d680df 100644 --- a/tools/esp8266/web.cpp +++ b/tools/esp8266/web.cpp @@ -453,7 +453,7 @@ void web::showWebApi(void) if (cmd == AlarmData){ iv->alarmMesIndex = response["payload"]; } - DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(cmd) + F(" and payload ") + String(response["payload"])); + DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(cmd) + F(" and payload ") + String((uint16_t) response["payload"])); // process payload from web request corresponding to the cmd iv->enqueCommand(cmd); }