From 79a91578b7da810bf0a60167344af2749638ebef Mon Sep 17 00:00:00 2001 From: omgbebebe Date: Tue, 18 Nov 2025 02:54:02 +0400 Subject: [PATCH] mqtt: do not try to send packets when it disconnected (#8658) --- src/mqtt/MQTT.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index f9f114039..babbcfd7c 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -51,6 +51,7 @@ constexpr int reconnectMax = 5; static uint8_t bytes[meshtastic_MqttClientProxyMessage_size + 30]; // 12 for channel name and 16 for nodeid static bool isMqttServerAddressPrivate = false; +static bool isConnected = false; inline void onReceiveProto(char *topic, byte *payload, size_t length) { @@ -320,8 +321,10 @@ bool connectPubSub(const PubSubConfig &config, PubSubClient &pubSub, Client &cli std::string nodeId = nodeDB->getNodeId(); const bool connected = pubSub.connect(nodeId.c_str(), config.mqttUsername, config.mqttPassword); if (connected) { + isConnected = true; LOG_INFO("MQTT connected"); } else { + isConnected = false; LOG_WARN("Failed to connect to MQTT server"); } return connected; @@ -507,6 +510,7 @@ bool MQTT::publish(const char *topic, const uint8_t *payload, size_t length, boo void MQTT::reconnect() { + isConnected = false; if (wantsLink()) { if (moduleConfig.mqtt.proxy_to_client_enabled) { LOG_INFO("MQTT connect via client proxy instead"); @@ -534,7 +538,7 @@ void MQTT::reconnect() runASAP = true; reconnectCount = 0; isMqttServerAddressPrivate = isPrivateIpAddress(clientConnection->remoteIP()); - + isConnected = true; publishNodeInfo(); sendSubscriptions(); } else { @@ -688,7 +692,7 @@ void MQTT::publishNodeInfo() } void MQTT::publishQueuedMessages() { - if (mqttQueue.isEmpty()) + if (mqttQueue.isEmpty() || !isConnected) return; LOG_DEBUG("Publish enqueued MQTT message"); @@ -895,4 +899,4 @@ void MQTT::perhapsReportToMap() // Update the last report time last_report_to_map = millis(); -} \ No newline at end of file +}