From 4507063ff40a28022c9b3fda448ba4eb81fc6d6c Mon Sep 17 00:00:00 2001 From: Erik Borowski Date: Thu, 5 Dec 2024 17:06:24 +0100 Subject: [PATCH] fix: reboot when MQTT connection fails too often --- mqtt.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/mqtt.cpp b/mqtt.cpp index c2fe54b..a27d785 100644 --- a/mqtt.cpp +++ b/mqtt.cpp @@ -101,22 +101,15 @@ void MQTT::setup() this->client->setServer(this->mqttServer, this->mqttPort); this->client->setCallback(std::bind(&MQTT::onMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + uint retries = 0; + Serial.println("[MQTT] Connecting to MQTT broker..."); while (!this->client->connected()) { - Serial.println("[MQTT] Connecting to MQTT broker..."); - uint retries = 0; - if (this->client->connect(this->clientId.c_str(), this->mqttUser, this->mqttPassword, String(this->getCombinedRootTopic() + "/availability").c_str(), 1, true, "offline")) - { - Serial.println("[MQTT] connected!"); - this->client->publish(String(this->getCombinedRootTopic() + "/availability").c_str(), "online", true); - this->client->subscribe(String(this->getCombinedRootTopic() + "/+/command").c_str()); - this->client->subscribe(String(this->getCombinedRootTopic() + "/+/pair").c_str()); - } - else + if (!this->client->connect(this->clientId.c_str(), this->mqttUser, this->mqttPassword, String(this->getCombinedRootTopic() + "/availability").c_str(), 1, true, "offline")) { Serial.print("[MQTT] Connection failed! rc="); Serial.print(this->client->state()); - Serial.println(" try again in 1 second"); + Serial.println(" trying again in 1 second."); delay(1000); retries++; if (retries > 60) @@ -124,6 +117,11 @@ void MQTT::setup() } } + Serial.println("[MQTT] connected!"); + this->client->publish(String(this->getCombinedRootTopic() + "/availability").c_str(), "online", true); + this->client->subscribe(String(this->getCombinedRootTopic() + "/+/command").c_str()); + this->client->subscribe(String(this->getCombinedRootTopic() + "/+/pair").c_str()); + this->sendAllHomeAssistantDiscoveryMessages(); }