mirror of
https://github.com/tugsi/Filaman.git
synced 2026-07-10 10:12:50 +02:00
Compare commits
1 Commits
f1a9c3c5d4
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ff4e81826 |
@@ -1,5 +0,0 @@
|
|||||||
.pio
|
|
||||||
.vscode/.browse.c_cpp.db*
|
|
||||||
.vscode/c_cpp_properties.json
|
|
||||||
.vscode/launch.json
|
|
||||||
.vscode/ipch
|
|
||||||
Vendored
-10
@@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
// 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"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
+1
-1
@@ -490,7 +490,7 @@ function handleSpoolIn(amsId, trayId) {
|
|||||||
nozzle_temp_max: parseInt(maxTemp),
|
nozzle_temp_max: parseInt(maxTemp),
|
||||||
type: selectedSpool.filament.material,
|
type: selectedSpool.filament.material,
|
||||||
brand: selectedSpool.filament.vendor.name,
|
brand: selectedSpool.filament.vendor.name,
|
||||||
tray_info_idx: selectedSpool.filament.extra.bambu_idx?.replace(/['"]+/g, '').trim() || '',
|
tray_info_idx: selectedSpool.filament.extra.bambu_idx.replace(/['"]+/g, '').trim(),
|
||||||
cali_idx: "-1" // Default-Wert setzen
|
cali_idx: "-1" // Default-Wert setzen
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+42
-153
@@ -18,7 +18,7 @@ PubSubClient client(sslClient);
|
|||||||
TaskHandle_t BambuMqttTask;
|
TaskHandle_t BambuMqttTask;
|
||||||
|
|
||||||
String report_topic = "";
|
String report_topic = "";
|
||||||
String request_topic = "";
|
//String request_topic = "";
|
||||||
const char* bambu_username = "bblp";
|
const char* bambu_username = "bblp";
|
||||||
const char* bambu_ip = nullptr;
|
const char* bambu_ip = nullptr;
|
||||||
const char* bambu_accesscode = nullptr;
|
const char* bambu_accesscode = nullptr;
|
||||||
@@ -92,7 +92,7 @@ bool loadBambuCredentials() {
|
|||||||
bambu_serialnr = g_bambu_serialnr.c_str();
|
bambu_serialnr = g_bambu_serialnr.c_str();
|
||||||
|
|
||||||
report_topic = "device/" + String(bambu_serialnr) + "/report";
|
report_topic = "device/" + String(bambu_serialnr) + "/report";
|
||||||
request_topic = "device/" + String(bambu_serialnr) + "/request";
|
//request_topic = "device/" + String(bambu_serialnr) + "/request";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Serial.println("Keine gültigen Bambu-Credentials gefunden.");
|
Serial.println("Keine gültigen Bambu-Credentials gefunden.");
|
||||||
@@ -197,69 +197,18 @@ FilamentResult findFilamentIdx(String brand, String type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool sendMqttMessage(const String& payload) {
|
bool sendMqttMessage(const String& payload) {
|
||||||
// Check MQTT client state first
|
Serial.println("Sending MQTT message");
|
||||||
if (!client.connected()) {
|
Serial.println(payload);
|
||||||
Serial.println("Error: MQTT client not connected when trying to send message");
|
if (client.publish(report_topic.c_str(), payload.c_str()))
|
||||||
Serial.print("MQTT client state: ");
|
{
|
||||||
Serial.println(client.state());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if request_topic is empty
|
|
||||||
if (request_topic.isEmpty()) {
|
|
||||||
Serial.println("Error: request_topic is empty!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check payload
|
|
||||||
if (payload.isEmpty()) {
|
|
||||||
Serial.println("Error: Empty payload!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Attempting to send MQTT message:");
|
|
||||||
Serial.println("Topic: " + request_topic);
|
|
||||||
Serial.println("Payload: " + payload);
|
|
||||||
Serial.println("Payload length: " + String(payload.length()));
|
|
||||||
|
|
||||||
// Try to publish with QoS 1 and retain flag false
|
|
||||||
bool published = client.publish(request_topic.c_str(), payload.c_str(), false);
|
|
||||||
|
|
||||||
if (published) {
|
|
||||||
Serial.println("MQTT message published successfully");
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
Serial.println("Failed to publish MQTT message");
|
|
||||||
Serial.print("MQTT client state after publish attempt: ");
|
|
||||||
Serial.println(client.state());
|
|
||||||
|
|
||||||
// Additional error information
|
|
||||||
switch(client.state()) {
|
|
||||||
case -4:
|
|
||||||
Serial.println("Reason: Connection timeout");
|
|
||||||
break;
|
|
||||||
case -3:
|
|
||||||
Serial.println("Reason: Connection lost");
|
|
||||||
break;
|
|
||||||
case -2:
|
|
||||||
Serial.println("Reason: Connect failed");
|
|
||||||
break;
|
|
||||||
case -1:
|
|
||||||
Serial.println("Reason: Disconnected");
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
Serial.println("Reason: Connected but publish failed");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Serial.println("Reason: Unknown error");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool setBambuSpool(String payload) {
|
bool setBambuSpool(String payload) {
|
||||||
Serial.println("Spool settings received from WebSocket");
|
Serial.println("Spool settings in");
|
||||||
Serial.println(payload);
|
Serial.println(payload);
|
||||||
|
|
||||||
// Parse the JSON
|
// Parse the JSON
|
||||||
@@ -271,28 +220,21 @@ bool setBambuSpool(String payload) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have all required fields
|
int amsId = doc["amsId"];
|
||||||
if (!doc["amsId"].is<int>() || !doc["trayId"].is<int>()) {
|
int trayId = doc["trayId"];
|
||||||
Serial.println("Error: Missing or invalid required fields in payload");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int amsId = doc["amsId"].as<int>();
|
|
||||||
int trayId = doc["trayId"].as<int>();
|
|
||||||
String color = doc["color"].as<String>();
|
String color = doc["color"].as<String>();
|
||||||
color.toUpperCase();
|
color.toUpperCase();
|
||||||
int minTemp = doc["nozzle_temp_min"] | 0; // Default to 0 if not present
|
int minTemp = doc["nozzle_temp_min"];
|
||||||
int maxTemp = doc["nozzle_temp_max"] | 0; // Default to 0 if not present
|
int maxTemp = doc["nozzle_temp_max"];
|
||||||
String type = doc["type"] | ""; // Default to empty string if not present
|
String type = doc["type"].as<String>();
|
||||||
(type == "PLA+") ? type = "PLA" : type;
|
(type == "PLA+") ? type = "PLA" : type;
|
||||||
String brand = doc["brand"] | ""; // Default to empty string if not present
|
String brand = doc["brand"].as<String>();
|
||||||
String tray_info_idx = (doc["tray_info_idx"].as<String>() != "-1") ? doc["tray_info_idx"].as<String>() : "";
|
String tray_info_idx = (doc["tray_info_idx"].as<String>() != "-1") ? doc["tray_info_idx"].as<String>() : "";
|
||||||
if (tray_info_idx == "") {
|
if (tray_info_idx == "") {
|
||||||
if (brand != "" && type != "") {
|
if (brand != "" && type != "") {
|
||||||
FilamentResult result = findFilamentIdx(brand, type);
|
FilamentResult result = findFilamentIdx(brand, type);
|
||||||
tray_info_idx = result.key;
|
tray_info_idx = result.key;
|
||||||
type = result.type; // Aktualisiere den type mit dem gefundenen Basistyp
|
type = result.type; // Aktualisiere den type mit dem gefundenen Basistyp
|
||||||
Serial.println("Found filament idx: " + tray_info_idx + " for type: " + type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String setting_id = doc["bambu_setting_id"].as<String>();
|
String setting_id = doc["bambu_setting_id"].as<String>();
|
||||||
@@ -300,7 +242,6 @@ bool setBambuSpool(String payload) {
|
|||||||
|
|
||||||
doc.clear();
|
doc.clear();
|
||||||
|
|
||||||
// Create MQTT message
|
|
||||||
doc["print"]["sequence_id"] = "0";
|
doc["print"]["sequence_id"] = "0";
|
||||||
doc["print"]["command"] = "ams_filament_setting";
|
doc["print"]["command"] = "ams_filament_setting";
|
||||||
doc["print"]["ams_id"] = amsId < 200 ? amsId : 255;
|
doc["print"]["ams_id"] = amsId < 200 ? amsId : 255;
|
||||||
@@ -309,25 +250,26 @@ bool setBambuSpool(String payload) {
|
|||||||
doc["print"]["nozzle_temp_min"] = minTemp;
|
doc["print"]["nozzle_temp_min"] = minTemp;
|
||||||
doc["print"]["nozzle_temp_max"] = maxTemp;
|
doc["print"]["nozzle_temp_max"] = maxTemp;
|
||||||
doc["print"]["tray_type"] = type;
|
doc["print"]["tray_type"] = type;
|
||||||
|
//doc["print"]["cali_idx"] = (cali_idx != "") ? cali_idx : "";
|
||||||
doc["print"]["tray_info_idx"] = tray_info_idx;
|
doc["print"]["tray_info_idx"] = tray_info_idx;
|
||||||
doc["print"]["setting_id"] = setting_id;
|
doc["print"]["setting_id"] = setting_id;
|
||||||
|
|
||||||
// Serialize and send MQTT message
|
// Serialize the JSON
|
||||||
String output;
|
String output;
|
||||||
serializeJson(doc, output);
|
serializeJson(doc, output);
|
||||||
Serial.println("Sending to Bambu printer:");
|
|
||||||
Serial.println(output);
|
|
||||||
|
|
||||||
if (!sendMqttMessage(output)) {
|
if (sendMqttMessage(output)) {
|
||||||
Serial.println("Failed to send filament settings to printer");
|
Serial.println("Spool successfully set");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("Failed to set spool");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("Filament settings sent successfully");
|
|
||||||
doc.clear();
|
doc.clear();
|
||||||
yield();
|
yield();
|
||||||
|
|
||||||
// Send calibration if available
|
|
||||||
if (cali_idx != "") {
|
if (cali_idx != "") {
|
||||||
yield();
|
yield();
|
||||||
doc["print"]["sequence_id"] = "0";
|
doc["print"]["sequence_id"] = "0";
|
||||||
@@ -336,18 +278,21 @@ bool setBambuSpool(String payload) {
|
|||||||
doc["print"]["nozzle_diameter"] = "0.4";
|
doc["print"]["nozzle_diameter"] = "0.4";
|
||||||
doc["print"]["cali_idx"] = cali_idx.toInt();
|
doc["print"]["cali_idx"] = cali_idx.toInt();
|
||||||
doc["print"]["tray_id"] = trayId < 200 ? trayId : 254;
|
doc["print"]["tray_id"] = trayId < 200 ? trayId : 254;
|
||||||
|
//doc["print"]["ams_id"] = amsId < 200 ? amsId : 255;
|
||||||
|
|
||||||
|
// Serialize the JSON
|
||||||
String output;
|
String output;
|
||||||
serializeJson(doc, output);
|
serializeJson(doc, output);
|
||||||
Serial.println("Sending calibration to printer:");
|
|
||||||
Serial.println(output);
|
|
||||||
|
|
||||||
if (!sendMqttMessage(output)) {
|
if (sendMqttMessage(output)) {
|
||||||
Serial.println("Failed to send calibration settings to printer");
|
Serial.println("Extrusion calibration successfully set");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("Failed to set extrusion calibration");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("Calibration settings sent successfully");
|
|
||||||
doc.clear();
|
doc.clear();
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
@@ -604,47 +549,20 @@ void reconnect() {
|
|||||||
uint8_t retries = 0;
|
uint8_t retries = 0;
|
||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
Serial.println("Attempting MQTT re/connection...");
|
Serial.println("Attempting MQTT re/connection...");
|
||||||
Serial.print("State before connect: ");
|
|
||||||
Serial.println(client.state());
|
|
||||||
bambu_connected = false;
|
bambu_connected = false;
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
|
|
||||||
// Generate a random client ID suffix
|
// Attempt to connect
|
||||||
String clientId = String(bambu_serialnr) + "_" + String(random(0xffff), HEX);
|
if (client.connect(bambu_serialnr, bambu_username, bambu_accesscode)) {
|
||||||
Serial.print("Reconnecting with client ID: ");
|
|
||||||
Serial.println(clientId);
|
|
||||||
|
|
||||||
// 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");
|
Serial.println("MQTT re/connected");
|
||||||
|
|
||||||
// Subscribe with QoS 1
|
client.subscribe(report_topic.c_str());
|
||||||
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;
|
bambu_connected = true;
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
} else {
|
} else {
|
||||||
int state = client.state();
|
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
Serial.print(state);
|
Serial.print(client.state());
|
||||||
Serial.print(" (");
|
Serial.println(" try again in 5 seconds");
|
||||||
// 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;
|
bambu_connected = false;
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
|
|
||||||
@@ -652,9 +570,11 @@ void reconnect() {
|
|||||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||||
if (retries > 5) {
|
if (retries > 5) {
|
||||||
Serial.println("Disable Bambu MQTT Task after 5 retries");
|
Serial.println("Disable Bambu MQTT Task after 5 retries");
|
||||||
|
//vTaskSuspend(BambuMqttTask);
|
||||||
vTaskDelete(BambuMqttTask);
|
vTaskDelete(BambuMqttTask);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
retries++;
|
retries++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -662,32 +582,17 @@ void reconnect() {
|
|||||||
|
|
||||||
void mqtt_loop(void * parameter) {
|
void mqtt_loop(void * parameter) {
|
||||||
Serial.println("Bambu MQTT Task gestartet");
|
Serial.println("Bambu MQTT Task gestartet");
|
||||||
unsigned long lastCheck = 0;
|
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
if (pauseBambuMqttTask) {
|
if (pauseBambuMqttTask) {
|
||||||
vTaskDelay(10000);
|
vTaskDelay(10000);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long now = millis();
|
|
||||||
|
|
||||||
if (!client.connected()) {
|
if (!client.connected()) {
|
||||||
Serial.println("Connection lost, attempting reconnect...");
|
|
||||||
reconnect();
|
reconnect();
|
||||||
yield();
|
yield();
|
||||||
esp_task_wdt_reset();
|
esp_task_wdt_reset();
|
||||||
vTaskDelay(100);
|
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();
|
client.loop();
|
||||||
yield();
|
yield();
|
||||||
esp_task_wdt_reset();
|
esp_task_wdt_reset();
|
||||||
@@ -712,30 +617,14 @@ bool setupMqtt() {
|
|||||||
sslClient.setInsecure();
|
sslClient.setInsecure();
|
||||||
client.setServer(bambu_ip, 8883);
|
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
|
// Verbinden mit dem MQTT-Server
|
||||||
bool connected = true;
|
bool connected = true;
|
||||||
if (client.connect(clientId.c_str(), bambu_username, bambu_accesscode, nullptr, 0, true, nullptr))
|
if (client.connect(bambu_serialnr, bambu_username, bambu_accesscode))
|
||||||
{
|
{
|
||||||
client.setCallback(mqtt_callback);
|
client.setCallback(mqtt_callback);
|
||||||
client.setBufferSize(16384); // Increased to 16KB to handle larger JSON
|
client.setBufferSize(5120);
|
||||||
|
client.subscribe(report_topic.c_str());
|
||||||
// Subscribe with QoS 1
|
//client.subscribe(request_topic.c_str());
|
||||||
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");
|
Serial.println("MQTT-Client initialisiert");
|
||||||
|
|
||||||
oledShowMessage("Bambu Connected");
|
oledShowMessage("Bambu Connected");
|
||||||
@@ -745,7 +634,7 @@ bool setupMqtt() {
|
|||||||
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 */
|
||||||
16384, /* Stack size in words */
|
8192, /* 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. */
|
||||||
|
|||||||
Reference in New Issue
Block a user