fix: use unique client ID for MQTT connection to avoid conflicts
This commit is contained in:
+56
-50
@@ -553,30 +553,34 @@ void reconnect() {
|
|||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
|
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (client.connect(bambu_serialnr, bambu_username, bambu_accesscode)) {
|
String clientId = String(bambu_serialnr) + "_" + String(random(0, 100));
|
||||||
Serial.println("MQTT re/connected");
|
if (client.connect(clientId.c_str(), bambu_username, bambu_accesscode)){
|
||||||
|
Serial.println("MQTT re/connected");
|
||||||
|
|
||||||
client.subscribe(report_topic.c_str());
|
client.subscribe(report_topic.c_str());
|
||||||
bambu_connected = true;
|
bambu_connected = true;
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
} else {
|
|
||||||
Serial.print("failed, rc=");
|
|
||||||
Serial.print(client.state());
|
|
||||||
Serial.println(" try again in 5 seconds");
|
|
||||||
bambu_connected = false;
|
|
||||||
oledShowTopRow();
|
|
||||||
|
|
||||||
yield();
|
|
||||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
||||||
if (retries > 5) {
|
|
||||||
Serial.println("Disable Bambu MQTT Task after 5 retries");
|
|
||||||
//vTaskSuspend(BambuMqttTask);
|
|
||||||
vTaskDelete(BambuMqttTask);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(client.state());
|
||||||
|
Serial.println(" try again in 5 seconds");
|
||||||
|
bambu_connected = false;
|
||||||
|
oledShowTopRow();
|
||||||
|
|
||||||
retries++;
|
yield();
|
||||||
}
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||||
|
if (retries > 5)
|
||||||
|
{
|
||||||
|
Serial.println("Disable Bambu MQTT Task after 5 retries");
|
||||||
|
// vTaskSuspend(BambuMqttTask);
|
||||||
|
vTaskDelete(BambuMqttTask);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
retries++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -619,38 +623,40 @@ bool setupMqtt() {
|
|||||||
|
|
||||||
// Verbinden mit dem MQTT-Server
|
// Verbinden mit dem MQTT-Server
|
||||||
bool connected = true;
|
bool connected = true;
|
||||||
if (client.connect(bambu_serialnr, bambu_username, bambu_accesscode))
|
String clientId = String(bambu_serialnr) + "_" + String(random(0, 100));
|
||||||
{
|
if (client.connect(clientId.c_str(), bambu_username, bambu_accesscode))
|
||||||
client.setCallback(mqtt_callback);
|
{
|
||||||
client.setBufferSize(5120);
|
client.setCallback(mqtt_callback);
|
||||||
client.subscribe(report_topic.c_str());
|
client.setBufferSize(5120);
|
||||||
//client.subscribe(request_topic.c_str());
|
client.subscribe(report_topic.c_str());
|
||||||
Serial.println("MQTT-Client initialisiert");
|
// client.subscribe(request_topic.c_str());
|
||||||
|
Serial.println("MQTT-Client initialisiert");
|
||||||
|
|
||||||
oledShowMessage("Bambu Connected");
|
oledShowMessage("Bambu Connected");
|
||||||
bambu_connected = true;
|
bambu_connected = true;
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
|
|
||||||
xTaskCreatePinnedToCore(
|
xTaskCreatePinnedToCore(
|
||||||
mqtt_loop, /* Function to implement the task */
|
mqtt_loop, /* Function to implement the task */
|
||||||
"BambuMqtt", /* Name of the task */
|
"BambuMqtt", /* Name of the task */
|
||||||
10240, /* Stack size in words */
|
10240, /* Stack size in words */
|
||||||
NULL, /* Task input parameter */
|
NULL, /* Task input parameter */
|
||||||
mqttTaskPrio, /* Priority of the task */
|
mqttTaskPrio, /* Priority of the task */
|
||||||
&BambuMqttTask, /* Task handle. */
|
&BambuMqttTask, /* Task handle. */
|
||||||
mqttTaskCore); /* Core where the task should run */
|
mqttTaskCore); /* Core where the task should run */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Fehler: Konnte sich nicht beim MQTT-Server anmelden");
|
Serial.println("Fehler: Konnte sich nicht beim MQTT-Server anmelden");
|
||||||
oledShowMessage("Bambu Connection Failed");
|
oledShowMessage("Bambu Connection Failed");
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
connected = false;
|
connected = false;
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!connected)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!connected) return false;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Fehler: Keine MQTT-Daten vorhanden");
|
Serial.println("Fehler: Keine MQTT-Daten vorhanden");
|
||||||
|
|||||||
Reference in New Issue
Block a user