From 64a2f5eb117ab1393a05193a229423928cd4a952 Mon Sep 17 00:00:00 2001 From: proddy Date: Tue, 14 Jan 2025 19:03:30 +0100 Subject: [PATCH] gracefully close mqtt on restart --- src/core/mqtt.h | 6 ++++++ src/core/system.cpp | 1 + 2 files changed, 7 insertions(+) diff --git a/src/core/mqtt.h b/src/core/mqtt.h index 4de1235e9..61c096cd1 100644 --- a/src/core/mqtt.h +++ b/src/core/mqtt.h @@ -114,6 +114,12 @@ class Mqtt { return mqttClient_ ? mqttClient_->connected() : false; } + static void disconnect() { + if (mqttClient_) { + mqttClient_->disconnect(); + }; + } + static MqttClient * client() { return mqttClient_; } diff --git a/src/core/system.cpp b/src/core/system.cpp index 93ba984b6..44e51453f 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -303,6 +303,7 @@ void System::system_restart(const char * partitionname) { store_nvs_values(); // save any NVS values Shell::loop_all(); // flush log to output + Mqtt::disconnect(); // gracefully disconnect MQTT, needed for QOS1 delay(1000); // wait 1 second ESP.restart(); #else