Skip to content

Commit 79a9157

Browse files
authored
mqtt: do not try to send packets when it disconnected (#8658)
1 parent 17cd830 commit 79a9157

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/mqtt/MQTT.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ constexpr int reconnectMax = 5;
5151
static uint8_t bytes[meshtastic_MqttClientProxyMessage_size + 30]; // 12 for channel name and 16 for nodeid
5252

5353
static bool isMqttServerAddressPrivate = false;
54+
static bool isConnected = false;
5455

5556
inline void onReceiveProto(char *topic, byte *payload, size_t length)
5657
{
@@ -320,8 +321,10 @@ bool connectPubSub(const PubSubConfig &config, PubSubClient &pubSub, Client &cli
320321
std::string nodeId = nodeDB->getNodeId();
321322
const bool connected = pubSub.connect(nodeId.c_str(), config.mqttUsername, config.mqttPassword);
322323
if (connected) {
324+
isConnected = true;
323325
LOG_INFO("MQTT connected");
324326
} else {
327+
isConnected = false;
325328
LOG_WARN("Failed to connect to MQTT server");
326329
}
327330
return connected;
@@ -507,6 +510,7 @@ bool MQTT::publish(const char *topic, const uint8_t *payload, size_t length, boo
507510

508511
void MQTT::reconnect()
509512
{
513+
isConnected = false;
510514
if (wantsLink()) {
511515
if (moduleConfig.mqtt.proxy_to_client_enabled) {
512516
LOG_INFO("MQTT connect via client proxy instead");
@@ -534,7 +538,7 @@ void MQTT::reconnect()
534538
runASAP = true;
535539
reconnectCount = 0;
536540
isMqttServerAddressPrivate = isPrivateIpAddress(clientConnection->remoteIP());
537-
541+
isConnected = true;
538542
publishNodeInfo();
539543
sendSubscriptions();
540544
} else {
@@ -688,7 +692,7 @@ void MQTT::publishNodeInfo()
688692
}
689693
void MQTT::publishQueuedMessages()
690694
{
691-
if (mqttQueue.isEmpty())
695+
if (mqttQueue.isEmpty() || !isConnected)
692696
return;
693697

694698
LOG_DEBUG("Publish enqueued MQTT message");
@@ -895,4 +899,4 @@ void MQTT::perhapsReportToMap()
895899

896900
// Update the last report time
897901
last_report_to_map = millis();
898-
}
902+
}

0 commit comments

Comments
 (0)