diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/data/bambu_filaments.json b/data/bambu_filaments.json new file mode 100644 index 0000000..afe2bc7 --- /dev/null +++ b/data/bambu_filaments.json @@ -0,0 +1,70 @@ +{ + "GFU99": "TPU", + "GFN99": "PA", + "GFN98": "PA-CF", + "GFL99": "PLA", + "GFL96": "PLA Silk", + "GFL98": "PLA-CF", + "GFL95": "PLA High Speed", + "GFG99": "PETG", + "GFG98": "PETG-CF", + "GFG97": "PCTG", + "GFB99": "ABS", + "GFC99": "PC", + "GFB98": "ASA", + "GFS99": "PVA", + "GFS98": "HIPS", + "GFT98": "PPS-CF", + "GFT97": "PPS", + "GFN97": "PPA-CF", + "GFN96": "PPA-GF", + "GFP99": "PE", + "GFP98": "PE-CF", + "GFP97": "PP", + "GFP96": "PP-CF", + "GFP95": "PP-GF", + "GFR99": "EVA", + "GFR98": "PHA", + "GFS97": "BVOH", + "GFA01": "Bambu PLA Matte", + "GFA00": "Bambu PLA Basic", + "GFA09": "Bambu PLA Tough", + "GFA07": "Bambu PLA Marble", + "GFA08": "Bambu PLA Sparkle", + "GFA02": "Bambu PLA Metal", + "GFA05": "Bambu PLA Silk", + "GFS00": "Bambu Support W", + "GFL03": "eSUN PLA+", + "GFL01": "PolyTerra PLA", + "GFL00": "PolyLite PLA", + "GFA50": "Bambu PLA-CF", + "GFS02": "Bambu Support For PLA", + "GFA11": "Bambu PLA Aero", + "GFL04": "Overture PLA", + "GFL05": "Overture Matte PLA", + "GFA12": "Bambu PLA Glow", + "GFA13": "Bambu PLA Dynamic", + "GFA15": "Bambu PLA Galaxy", + "GFS05": "Bambu Support For PLA/PETG", + "GFU01": "Bambu TPU 95A", + "GFU00": "Bambu TPU 95A HF", + "GFG00": "Bambu PETG Basic", + "GFT01": "Bambu PET-CF", + "GFG50": "Bambu PETG-CF", + "GFG60": "PolyLite PETG", + "GFG01": "Bambu PETG Translucent", + "GFB00": "Bambu ABS", + "GFB60": "PolyLite ABS", + "GFB50": "Bambu ABS-GF", + "GFC00": "Bambu PC", + "GFB01": "Bambu ASA", + "GFB61": "PolyLite ASA", + "GFB02": "Bambu ASA-Aero", + "GFS04": "Bambu PVA", + "GFS01": "Bambu Support G", + "GFN03": "Bambu PA-CF", + "GFN04": "Bambu PAHT-CF", + "GFS03": "Bambu Support For PA/PET", + "GFN05": "Bambu PA6-CF", + "GFN08": "Bambu PA6-GF" +} \ No newline at end of file diff --git a/data/favicon.ico b/data/favicon.ico new file mode 100644 index 0000000..14b7586 Binary files /dev/null and b/data/favicon.ico differ diff --git a/data/header.html.gz b/data/header.html.gz new file mode 100644 index 0000000..a001e0c Binary files /dev/null and b/data/header.html.gz differ diff --git a/data/index.html.gz b/data/index.html.gz new file mode 100644 index 0000000..c181448 Binary files /dev/null and b/data/index.html.gz differ diff --git a/data/logo.png.gz b/data/logo.png.gz new file mode 100644 index 0000000..9681c15 Binary files /dev/null and b/data/logo.png.gz differ diff --git a/data/own_filaments.json b/data/own_filaments.json new file mode 100644 index 0000000..cd3e43c --- /dev/null +++ b/data/own_filaments.json @@ -0,0 +1,31 @@ +{ + "TPU": "GFU99", + "PA": "GFN99", + "PA-CF": "GFN98", + "PLA": "GFL99", + "PLA Silk": "GFL96", + "PLA-CF": "GFL98", + "PLA High Speed": "GFL95", + "PETG": "GFG99", + "PETG-CF": "GFG98", + "PCTG": "GFG97", + "ABS": "GFB99", + "ABS+HS": "GFB99", + "PC": "GFC99", + "PC/ABS": "GFC99", + "ASA": "GFB98", + "PVA": "GFS99", + "HIPS": "GFS98", + "PPS-CF": "GFT98", + "PPS": "GFT97", + "PPA-CF": "GFN97", + "PPA-GF": "GFN96", + "PE": "GFP99", + "PE-CF": "GFP98", + "PP": "GFP97", + "PP-CF": "GFP96", + "PP-GF": "GFP95", + "EVA": "GFR99", + "PHA": "GFR98", + "BVOH": "GFS97" +} \ No newline at end of file diff --git a/data/rfid.html.gz b/data/rfid.html.gz new file mode 100644 index 0000000..4c86b2b Binary files /dev/null and b/data/rfid.html.gz differ diff --git a/data/rfid.js.gz b/data/rfid.js.gz new file mode 100644 index 0000000..46476c1 Binary files /dev/null and b/data/rfid.js.gz differ diff --git a/data/set_spoolman.png.gz b/data/set_spoolman.png.gz new file mode 100644 index 0000000..d546bd8 Binary files /dev/null and b/data/set_spoolman.png.gz differ diff --git a/data/spool_in.png.gz b/data/spool_in.png.gz new file mode 100644 index 0000000..aebffe0 Binary files /dev/null and b/data/spool_in.png.gz differ diff --git a/data/spoolman.html b/data/spoolman.html new file mode 100644 index 0000000..1b615f7 --- /dev/null +++ b/data/spoolman.html @@ -0,0 +1,171 @@ + + + + + + FilaMan - Filament Management Tool + + + + + + + + + + + + +
+

Spoolman API URL / Bambu Credentials

+ +
+
+
Set URL/IP to your Spoolman-Instanz
+ +
If you want to enable sending Spool to Spoolman Octoprint Plugin:
+

+ Send to Octo-Plugin +

+ + + +

+
+
+ +
+
+
Bambu Lab Printer Credentials
+
+
+ + +
+
+ + +
+
+ + +
+
+

If activated, FilaMan will automatically update the next filled tray with the last scanned and weighed spool.

+
+ + +
+
+ + +
+ + +

+
+
+
+
+ + diff --git a/data/spoolman.js.gz b/data/spoolman.js.gz new file mode 100644 index 0000000..08a2bb8 Binary files /dev/null and b/data/spoolman.js.gz differ diff --git a/data/style.css.gz b/data/style.css.gz new file mode 100644 index 0000000..27ad44c Binary files /dev/null and b/data/style.css.gz differ diff --git a/data/upgrade.html.gz b/data/upgrade.html.gz new file mode 100644 index 0000000..7eefe82 Binary files /dev/null and b/data/upgrade.html.gz differ diff --git a/data/waage.html.gz b/data/waage.html.gz new file mode 100644 index 0000000..1ec7b06 Binary files /dev/null and b/data/waage.html.gz differ diff --git a/data/wifi.html.gz b/data/wifi.html.gz new file mode 100644 index 0000000..761fca1 Binary files /dev/null and b/data/wifi.html.gz differ diff --git a/src/bambu.cpp b/src/bambu.cpp index 9b9a67b..63d4434 100644 --- a/src/bambu.cpp +++ b/src/bambu.cpp @@ -549,20 +549,47 @@ void reconnect() { uint8_t retries = 0; while (!client.connected()) { Serial.println("Attempting MQTT re/connection..."); + Serial.print("State before connect: "); + Serial.println(client.state()); bambu_connected = false; oledShowTopRow(); - // Attempt to connect - if (client.connect(bambu_serialnr, bambu_username, bambu_accesscode)) { - Serial.println("MQTT re/connected"); + // Generate a random client ID suffix + String clientId = String(bambu_serialnr) + "_" + String(random(0xffff), HEX); + Serial.print("Reconnecting with client ID: "); + Serial.println(clientId); - client.subscribe(report_topic.c_str()); + // Attempt to connect with clean session and will message + if (client.connect(clientId.c_str(), bambu_username, bambu_accesscode, nullptr, 0, true, nullptr)) { + Serial.println("MQTT re/connected"); + + // Subscribe with QoS 1 + if (client.subscribe(report_topic.c_str(), 1)) { + Serial.println("Successfully subscribed to topic with QoS 1: " + report_topic); + } else { + Serial.println("Failed to subscribe to topic: " + report_topic); + } bambu_connected = true; oledShowTopRow(); } else { + int state = client.state(); Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); + Serial.print(state); + Serial.print(" ("); + // Print detailed error message + switch(state) { + case -4: Serial.print("MQTT_CONNECTION_TIMEOUT"); break; + case -3: Serial.print("MQTT_CONNECTION_LOST"); break; + case -2: Serial.print("MQTT_CONNECT_FAILED"); break; + case -1: Serial.print("MQTT_DISCONNECTED"); break; + case 1: Serial.print("MQTT_CONNECT_BAD_PROTOCOL"); break; + case 2: Serial.print("MQTT_CONNECT_BAD_CLIENT_ID"); break; + case 3: Serial.print("MQTT_CONNECT_UNAVAILABLE"); break; + case 4: Serial.print("MQTT_CONNECT_BAD_CREDENTIALS"); break; + case 5: Serial.print("MQTT_CONNECT_UNAUTHORIZED"); break; + default: Serial.print("UNKNOWN"); break; + } + Serial.println(") try again in 5 seconds"); bambu_connected = false; oledShowTopRow(); @@ -570,11 +597,9 @@ void reconnect() { vTaskDelay(5000 / portTICK_PERIOD_MS); if (retries > 5) { Serial.println("Disable Bambu MQTT Task after 5 retries"); - //vTaskSuspend(BambuMqttTask); vTaskDelete(BambuMqttTask); break; } - retries++; } } @@ -582,17 +607,32 @@ void reconnect() { void mqtt_loop(void * parameter) { Serial.println("Bambu MQTT Task gestartet"); + unsigned long lastCheck = 0; + for(;;) { if (pauseBambuMqttTask) { vTaskDelay(10000); + continue; } + unsigned long now = millis(); + if (!client.connected()) { + Serial.println("Connection lost, attempting reconnect..."); reconnect(); yield(); esp_task_wdt_reset(); vTaskDelay(100); + continue; } + + // Periodically check connection status + if (now - lastCheck > 30000) { // Check every 30 seconds + Serial.print("MQTT Status Check - Connected: "); + Serial.println(client.connected() ? "Yes" : "No"); + lastCheck = now; + } + client.loop(); yield(); esp_task_wdt_reset(); @@ -617,15 +657,30 @@ bool setupMqtt() { sslClient.setInsecure(); client.setServer(bambu_ip, 8883); + // Generate a random client ID suffix + String clientId = String(bambu_serialnr) + "_" + String(random(0xffff), HEX); + + // MQTT Connection Options + // client.setKeepAlive(60); + // client.setSocketTimeout(60); // Increase socket timeout + + Serial.print("Connecting with client ID: "); + Serial.println(clientId); + // Verbinden mit dem MQTT-Server bool connected = true; - if (client.connect(bambu_serialnr, bambu_username, bambu_accesscode)) + if (client.connect(clientId.c_str(), bambu_username, bambu_accesscode, nullptr, 0, true, nullptr)) { client.setCallback(mqtt_callback); - client.setBufferSize(5120); - // Optional: Topic abonnieren - client.subscribe(report_topic.c_str()); - //client.subscribe(request_topic.c_str()); + client.setBufferSize(16384); // Increased to 16KB to handle larger JSON + + // Subscribe with QoS 1 + if (client.subscribe(report_topic.c_str(), 1)) { + Serial.println("Successfully subscribed to topic with QoS 1: " + report_topic); + } else { + Serial.println("Failed to subscribe to topic: " + report_topic); + } + Serial.println("MQTT-Client initialisiert"); oledShowMessage("Bambu Connected"); @@ -635,7 +690,7 @@ bool setupMqtt() { xTaskCreatePinnedToCore( mqtt_loop, /* Function to implement the task */ "BambuMqtt", /* Name of the task */ - 8192, /* Stack size in words */ + 16384, /* Stack size in words */ NULL, /* Task input parameter */ mqttTaskPrio, /* Priority of the task */ &BambuMqttTask, /* Task handle. */