From fb1eb6971e3091b8dee7a2edfa90df4f162235d3 Mon Sep 17 00:00:00 2001 From: 4m1g0 Date: Fri, 10 Jan 2020 00:48:23 +0100 Subject: [PATCH] Fix wrong client identifier causing some boards to reconnect to MQTT continuously --- FossaGroundStation/src/Mqtt/MQTT_Client.cpp | 15 ++++++--------- FossaGroundStation/src/Mqtt/MQTT_Client.h | 1 - FossaGroundStation/src/Status.h | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/FossaGroundStation/src/Mqtt/MQTT_Client.cpp b/FossaGroundStation/src/Mqtt/MQTT_Client.cpp index 40825fd6..72fd66fe 100644 --- a/FossaGroundStation/src/Mqtt/MQTT_Client.cpp +++ b/FossaGroundStation/src/Mqtt/MQTT_Client.cpp @@ -23,10 +23,7 @@ MQTT_Client::MQTT_Client(ConfigManager& x) : PubSubClient(espClient) , configManager(x) -{ - uint64_t chipId = ESP.getEfuseMac(); - clientId = String ((uint32_t)chipId, HEX); -} +{ } void MQTT_Client::loop() { if (!connected() && millis() - lastConnectionAtempt > reconnectionInterval) { @@ -55,14 +52,14 @@ void MQTT_Client::loop() { } void MQTT_Client::reconnect() { - if (!connected()) { - uint64_t chipId = ESP.getEfuseMac(); - String clientId = String ((uint32_t)chipId, HEX); - } + uint64_t chipId = ESP.getEfuseMac(); + char clientId[13]; + sprintf(clientId, "%04X%08X",(uint16_t)(chipId>>32), (uint32_t)chipId); + Serial.println(clientId); Serial.print("Attempting MQTT connection..."); Serial.println ("If this is taking more than expected, connect to the config panel on the ip: " + WiFi.localIP().toString() + " to review the MQTT connection credentials."); - if (connect(clientId.c_str(), configManager.getMqttUser(), configManager.getMqttPass(), buildTopic(topicStatus).c_str(), 2, false, "0")) { + if (connect(clientId, configManager.getMqttUser(), configManager.getMqttPass(), buildTopic(topicStatus).c_str(), 2, false, "0")) { Serial.println("connected"); subscribeToAll(); sendWelcome(); diff --git a/FossaGroundStation/src/Mqtt/MQTT_Client.h b/FossaGroundStation/src/Mqtt/MQTT_Client.h index 8dbe39f1..081cf3ae 100644 --- a/FossaGroundStation/src/Mqtt/MQTT_Client.h +++ b/FossaGroundStation/src/Mqtt/MQTT_Client.h @@ -83,7 +83,6 @@ class MQTT_Client : public PubSubClient { unsigned long lastPing = 0; unsigned long lastConnectionAtempt = 0; uint8_t connectionAtempts = 0; - String clientId; const unsigned long pingInterval = 1 * 60 * 1000; const unsigned long reconnectionInterval = 5 * 1000; diff --git a/FossaGroundStation/src/Status.h b/FossaGroundStation/src/Status.h index 3e45a0ac..581610bb 100644 --- a/FossaGroundStation/src/Status.h +++ b/FossaGroundStation/src/Status.h @@ -42,7 +42,7 @@ struct PacketInfo { }; struct Status { - const uint32_t version = 2001080; // version year month day release + const uint32_t version = 2001081; // version year month day release bool mqtt_connected = false; SysInfo sysInfo; PacketInfo lastPacketInfo;