From d39d62c2f82c6d5bab7c29ebf4436affa62347ff Mon Sep 17 00:00:00 2001 From: Proddy Date: Mon, 30 Jan 2023 17:04:33 +0100 Subject: [PATCH] show mqtt disconnect error in log, on first connect only --- src/mqtt.cpp | 6 ++++-- src/mqtt.h | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mqtt.cpp b/src/mqtt.cpp index e414daa1b..4cf6cb60e 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -461,10 +461,12 @@ void Mqtt::start() { mqttClient_->onConnect([this](bool sessionPresent) { on_connect(); }); mqttClient_->onDisconnect([this](AsyncMqttClientDisconnectReason reason) { - if (!connecting_) { + // only show the error once, not every 2 seconds + if (!connecting_ && first_connect_attempted_) { return; } - connecting_ = false; + first_connect_attempted_ = true; + connecting_ = false; if (reason == AsyncMqttClientDisconnectReason::TCP_DISCONNECTED) { LOG_WARNING("MQTT disconnected: TCP"); } else if (reason == AsyncMqttClientDisconnectReason::MQTT_IDENTIFIER_REJECTED) { diff --git a/src/mqtt.h b/src/mqtt.h index 16f607145..8de219c11 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -297,6 +297,8 @@ class Mqtt { uint32_t last_publish_heartbeat_ = 0; uint32_t last_publish_queue_ = 0; + bool first_connect_attempted_ = false; + static bool connecting_; static bool initialized_; static uint32_t mqtt_publish_fails_;