From ca662b86c7970559b8f6c46b90076257e823d100 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Fri, 6 Dec 2024 14:57:49 +0100 Subject: [PATCH] This PR (hopefully) fixes the MQTT disconnections happening all the time on some devices (e.g., when there is lots of busy traffic on BSB) --- BSB_LAN/include/mqtt_handler.h | 2 ++ BSB_LAN/src/PubSubClient/src/PubSubClient.cpp | 3 +++ 2 files changed, 5 insertions(+) diff --git a/BSB_LAN/include/mqtt_handler.h b/BSB_LAN/include/mqtt_handler.h index c14bed7bf..f17cd8c4b 100644 --- a/BSB_LAN/include/mqtt_handler.h +++ b/BSB_LAN/include/mqtt_handler.h @@ -196,6 +196,8 @@ bool mqtt_connect() { mqtt_client= new ComClient(); MQTTPubSubClient = new PubSubClient(mqtt_client[0]); MQTTPubSubClient->setBufferSize(2048); + MQTTPubSubClient->setKeepAlive(30); + MQTTPubSubClient->setSocketTimeout(120); mqtt_reconnect_timer = 0; first_connect = true; } diff --git a/BSB_LAN/src/PubSubClient/src/PubSubClient.cpp b/BSB_LAN/src/PubSubClient/src/PubSubClient.cpp index 25e74257d..e8b5ed1c1 100644 --- a/BSB_LAN/src/PubSubClient/src/PubSubClient.cpp +++ b/BSB_LAN/src/PubSubClient/src/PubSubClient.cpp @@ -253,6 +253,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass write(MQTTCONNECT,this->buffer,length-MQTT_MAX_HEADER_SIZE); lastInActivity = lastOutActivity = millis(); + pingOutstanding = false; while (!_client->available()) { unsigned long t = millis(); @@ -374,6 +375,7 @@ boolean PubSubClient::loop() { if (pingOutstanding) { this->_state = MQTT_CONNECTION_TIMEOUT; _client->stop(); + pingOutstanding = false; return false; } else { this->buffer[0] = MQTTPINGREQ; @@ -665,6 +667,7 @@ void PubSubClient::disconnect() { _client->flush(); _client->stop(); lastInActivity = lastOutActivity = millis(); + pingOutstanding = false; } uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {