diff --git a/data/setup.html b/data/setup.html
index 80d091c..75616c1 100644
--- a/data/setup.html
+++ b/data/setup.html
@@ -41,11 +41,11 @@
System Registration
-
Current Status: {{registered}}
+
Current Status: %registered%
-
+
diff --git a/data/waage.html b/data/waage.html
index addd5ee..0b89a40 100644
--- a/data/waage.html
+++ b/data/waage.html
@@ -46,7 +46,7 @@
diff --git a/platformio.ini b/platformio.ini
index c98835e..57ed557 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -55,11 +55,29 @@ build_flags =
#-DOTA_DEBUG=1
-DCONFIG_OPTIMIZATION_LEVEL_DEBUG=1
-DBOOT_APP_PARTITION_OTA_0=1
- -DCONFIG_LWIP_TCP_MSL=60000
- -DCONFIG_LWIP_TCP_RCV_BUF_DEFAULT=4096
+
+ # ===== AsyncTCP Optimierungen - KRITISCH für WiFi-Stabilität =====
+ # Core 1 = Arduino/App Core (nicht Core 0 = WiFi/Protocol Core!)
+ # Schwere Callbacks auf Core 0 stören das WiFi-Radio
+ -DCONFIG_ASYNC_TCP_RUNNING_CORE=1
+ # Längerer ACK-Timeout für instabile Verbindungen (10s statt 5s)
+ -DCONFIG_ASYNC_TCP_MAX_ACK_TIME=10000
+ # Größere Queue für mehr gleichzeitige Requests
+ -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128
+ # Mehr Stack für komplexere Callbacks (8KB statt 16KB default, spart RAM)
+ -DCONFIG_ASYNC_TCP_STACK_SIZE=8192
+ # Standard-Priorität beibehalten
+ -DCONFIG_ASYNC_TCP_PRIORITY=10
+ # WDT aktivieren für Crash-Recovery (war deaktiviert!)
+ -DCONFIG_ASYNC_TCP_USE_WDT=1
+
+ # ===== LwIP Optimierungen für OTA-Uploads (1.5-2MB) =====
+ # Schnellere Socket-Freigabe (30s statt 60s TIME_WAIT)
+ -DCONFIG_LWIP_TCP_MSL=30000
+ # Größerer RX-Buffer für große Uploads (8KB statt 4KB)
+ -DCONFIG_LWIP_TCP_RCV_BUF_DEFAULT=8192
+ # Max aktive TCP-Verbindungen (unverändert)
-DCONFIG_LWIP_MAX_ACTIVE_TCP=16
- -DCONFIG_ASYNC_TCP_RUNNING_CORE=0
- -DCONFIG_ASYNC_TCP_USE_WDT=0
extra_scripts =
scripts/extra_script.py
diff --git a/src/api.cpp b/src/api.cpp
index 70c9f5b..c1fab76 100644
--- a/src/api.cpp
+++ b/src/api.cpp
@@ -80,10 +80,12 @@ bool registerDevice(const String& deviceCode) {
bool sendHeartbeat() {
if (!checkFilamanRegistration() || WiFi.status() != WL_CONNECTED) return false;
HTTPClient http;
- http.setTimeout(3000);
+ http.setTimeout(10000); // 10s statt 3s - wichtig für instabile Verbindungen
+ http.setReuse(true); // Keep-Alive aktivieren für bessere Performance
http.begin(filamanUrl + "/api/v1/devices/heartbeat");
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Device " + filamanToken);
+ http.addHeader("Connection", "keep-alive"); // Explizit Keep-Alive anfordern
JsonDocument doc;
doc["ip_address"] = WiFi.localIP().toString();
String payload;
@@ -94,6 +96,21 @@ bool sendHeartbeat() {
return filamanConnected;
}
+// Heartbeat mit Retry-Logik für mehr Stabilität
+bool sendHeartbeatWithRetry(int maxRetries = 2) {
+ for (int attempt = 0; attempt <= maxRetries; attempt++) {
+ if (sendHeartbeat()) {
+ return true;
+ }
+ if (attempt < maxRetries) {
+ Serial.printf("Heartbeat failed, retry %d/%d...\n", attempt + 1, maxRetries);
+ vTaskDelay(pdMS_TO_TICKS(1000)); // 1s warten vor Retry
+ }
+ }
+ Serial.println("Heartbeat failed after all retries");
+ return false;
+}
+
bool sendWeight(int spoolId, String tagUuid, float measuredWeight) {
Serial.printf("sendWeight: sending to API - spoolId=%d, tagUuid=%s, weight=%.1f\n", spoolId, tagUuid.c_str(), measuredWeight);
if (!checkFilamanRegistration() || WiFi.status() != WL_CONNECTED) {
@@ -101,10 +118,12 @@ bool sendWeight(int spoolId, String tagUuid, float measuredWeight) {
return false;
}
HTTPClient http;
- http.setTimeout(3000);
+ http.setTimeout(10000); // 10s Timeout
+ http.setReuse(true);
http.begin(filamanUrl + "/api/v1/devices/scale/weight");
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Device " + filamanToken);
+ http.addHeader("Connection", "keep-alive");
JsonDocument doc;
// Only add spool_id if it's > 0 (for NTAG tags with spool ID)
// For Bambu tags (spoolId == 0), only send tag_uuid
@@ -146,10 +165,12 @@ bool sendWeight(int spoolId, String tagUuid, float measuredWeight) {
bool sendLocation(int spoolId, String spoolTagUuid, int locationId, String locationTagUuid) {
if (!checkFilamanRegistration() || WiFi.status() != WL_CONNECTED) return false;
HTTPClient http;
- http.setTimeout(3000);
+ http.setTimeout(10000); // 10s Timeout
+ http.setReuse(true);
http.begin(filamanUrl + "/api/v1/devices/scale/locate");
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Device " + filamanToken);
+ http.addHeader("Connection", "keep-alive");
JsonDocument doc;
if (spoolId > 0) doc["spool_id"] = spoolId;
if (spoolTagUuid.length() > 0) doc["spool_tag_uuid"] = spoolTagUuid;
@@ -165,10 +186,12 @@ bool sendLocation(int spoolId, String spoolTagUuid, int locationId, String locat
bool sendRfidResult(String tagUuid, int spoolId, int locationId, bool success, String errorMessage, float remainingWeight) {
if (!checkFilamanRegistration() || WiFi.status() != WL_CONNECTED) return false;
HTTPClient http;
- http.setTimeout(5000);
+ http.setTimeout(10000); // 10s Timeout
+ http.setReuse(true);
http.begin(filamanUrl + "/api/v1/devices/rfid-result");
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Device " + filamanToken);
+ http.addHeader("Connection", "keep-alive");
JsonDocument doc;
doc["success"] = success;
@@ -205,7 +228,7 @@ void filamanApiTask(void* pvParameters) {
if (hasReq) {
filamanApiState = API_TRANSMITTING;
switch (req.type) {
- case API_REQUEST_HEARTBEAT: sendHeartbeat(); break;
+ case API_REQUEST_HEARTBEAT: sendHeartbeatWithRetry(2); break; // Mit Retry-Logik
case API_REQUEST_WEIGHT: sendWeight(req.id1, req.str1, req.val); break;
case API_REQUEST_LOCATE: sendLocation(req.id1, req.str1, req.id2, req.str2); break;
case API_REQUEST_RFID_RESULT: sendRfidResult(req.str1, req.id1, req.id2, req.bool1, req.str3, req.remainingWeight); break;
diff --git a/src/commonFS.cpp b/src/commonFS.cpp
index b14736f..71aec12 100644
--- a/src/commonFS.cpp
+++ b/src/commonFS.cpp
@@ -57,10 +57,20 @@ String readFile(const char* filename) {
Serial.println(filename);
return "";
}
- String content = "";
+
+ // Effizient: Speicher vorab allokieren und in Blöcken lesen
+ // Alte Implementierung war O(n²) wegen Byte-für-Byte String-Konkatenation
+ size_t fileSize = file.size();
+ String content;
+ content.reserve(fileSize + 1); // Speicher vorab allokieren
+
+ // Lese in 512-Byte Blöcken statt Byte für Byte
+ char buffer[512];
while (file.available()) {
- content += (char)file.read();
+ size_t bytesRead = file.readBytes(buffer, sizeof(buffer));
+ content.concat(buffer, bytesRead);
}
+
file.close();
return content;
}
diff --git a/src/main.cpp b/src/main.cpp
index be47186..ce2ec08 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -141,8 +141,14 @@ void loop() {
{
oledShowTopRow();
oledCheckSleep(); // Put display to sleep if timeout elapsed
- // Clean up dead websocket clients periodically instead of on connect
- if(currentMillis % 10000 < 50) ws.cleanupClients();
+ }
+
+ // WebSocket Cleanup alle 5 Sekunden (häufiger als vorher)
+ // Tote Clients können WiFi-Ressourcen blockieren
+ static unsigned long lastWsCleanup = 0;
+ if (currentMillis - lastWsCleanup >= 5000) {
+ ws.cleanupClients();
+ lastWsCleanup = currentMillis;
}
// Periodic FilaMan heartbeat
diff --git a/src/ota.cpp b/src/ota.cpp
index eb02ee8..5cadbb0 100644
--- a/src/ota.cpp
+++ b/src/ota.cpp
@@ -16,6 +16,7 @@ static int currentUpdateCommand = 0;
static size_t updateTotalSize = 0;
static size_t updateWritten = 0;
static bool isSpiffsUpdate = false;
+static size_t lastYieldAt = 0; // Für WiFi-Yield während großer Uploads
/**
* Compares two version strings and determines if version1 is less than version2
@@ -64,11 +65,18 @@ void espRestart() {
void sendUpdateProgress(int progress, const char* status = nullptr, const char* message = nullptr) {
static int lastSentProgress = -1;
+ static unsigned long lastSendTime = 0;
- // Verhindere zu häufige Updates
+ unsigned long now = millis();
+
+ // Verhindere zu häufige Updates - mindestens 500ms zwischen Progress-Updates
+ // Dies reduziert WebSocket-Traffic während des Uploads erheblich
if (progress == lastSentProgress && !status && !message) {
return;
}
+ if (now - lastSendTime < 500 && progress < 100 && !status) {
+ return; // Throttle normale Progress-Updates
+ }
String progressMsg = "{\"type\":\"updateProgress\",\"progress\":" + String(progress);
if (status) {
@@ -79,23 +87,13 @@ void sendUpdateProgress(int progress, const char* status = nullptr, const char*
}
progressMsg += "}";
- if (progress >= 100) {
- // Sende die Nachricht nur einmal für den Abschluss
- ws.textAll("{\"type\":\"updateProgress\",\"progress\":100,\"status\":\"success\",\"message\":\"Update successful! Restarting device...\"}");
- delay(50);
- }
-
- // Sende die Nachricht mehrmals mit Verzögerung für wichtige Updates
- if (status || abs(progress - lastSentProgress) >= 10 || progress == 100) {
- for (int i = 0; i < 2; i++) {
- ws.textAll(progressMsg);
- delay(100); // Längerer Delay zwischen Nachrichten
- }
- } else {
- ws.textAll(progressMsg);
- delay(50);
- }
+ // Sende Progress-Update (nur einmal, nicht mehrfach)
+ ws.textAll(progressMsg);
+ // Yield für WiFi-Stack nach jedem WebSocket-Send
+ vTaskDelay(pdMS_TO_TICKS(10));
+
+ lastSendTime = now;
lastSentProgress = progress;
}
@@ -132,6 +130,7 @@ void handleUpdate(AsyncWebServer &server) {
if (!index) {
updateTotalSize = request->contentLength();
updateWritten = 0;
+ lastYieldAt = 0; // Reset yield counter
isSpiffsUpdate = (filename.indexOf("website") > -1);
if (isSpiffsUpdate) {
@@ -147,14 +146,14 @@ void handleUpdate(AsyncWebServer &server) {
return;
}
sendUpdateProgress(5, "starting", "Starting SPIFFS update...");
- vTaskDelay(pdMS_TO_TICKS(200));
+ vTaskDelay(pdMS_TO_TICKS(100));
} else {
if (!Update.begin(updateTotalSize)) {
request->send(400, "application/json", "{\"success\":false,\"message\":\"Update initialization failed\"}");
return;
}
sendUpdateProgress(0, "starting", "Starting firmware update...");
- vTaskDelay(pdMS_TO_TICKS(200));
+ vTaskDelay(pdMS_TO_TICKS(100));
}
}
@@ -165,22 +164,30 @@ void handleUpdate(AsyncWebServer &server) {
}
updateWritten += len;
+
+ // KRITISCH: Yield alle 32KB für WiFi-Stack bei großen Uploads (1.5-2MB)
+ // Ohne dies kann der WiFi-Stack blockieren und der Upload hängen bleiben
+ if (updateWritten - lastYieldAt >= 32768) {
+ vTaskDelay(pdMS_TO_TICKS(5)); // Kurze Pause für WiFi-Stack
+ lastYieldAt = updateWritten;
+ }
+
int currentProgress;
// Berechne den Fortschritt basierend auf dem Update-Typ
if (isSpiffsUpdate) {
- // SPIFFS: 5-75% für Upload
- currentProgress = 6 + (updateWritten * 100) / updateTotalSize;
+ // SPIFFS: 5-100% für Upload
+ currentProgress = 5 + (updateWritten * 95) / updateTotalSize;
} else {
// Firmware: 0-100% für Upload
- currentProgress = 1 + (updateWritten * 100) / updateTotalSize;
+ currentProgress = (updateWritten * 100) / updateTotalSize;
}
static int lastProgress = -1;
- if (currentProgress != lastProgress && (currentProgress % 10 == 0 || final)) {
+ // Nur alle 5% Progress-Updates senden (reduziert WebSocket-Traffic)
+ if (currentProgress != lastProgress && (currentProgress % 5 == 0 || final)) {
sendUpdateProgress(currentProgress, "uploading");
oledShowProgressBar(currentProgress, 100, tr(STR_UPDATE), tr(STR_DOWNLOAD));
- vTaskDelay(pdMS_TO_TICKS(50));
lastProgress = currentProgress;
}
}
diff --git a/src/website.cpp b/src/website.cpp
index 551499b..bda4534 100644
--- a/src/website.cpp
+++ b/src/website.cpp
@@ -18,6 +18,7 @@
#endif
#define NO_CACHE "no-cache, no-store, must-revalidate"
+#define CACHE_ASSETS "max-age=86400" // 24h Cache für statische Assets
AsyncWebServer server(webserverPort);
AsyncWebSocket ws("/ws");
@@ -25,6 +26,15 @@ AsyncWebSocket ws("/ws");
uint8_t lastSuccess = 0;
nfcReaderStateType lastnfcReaderState = NFC_IDLE;
+// Template-Processor für dynamische Seiten (ESPAsyncWebServer built-in)
+// Ersetzt %variable% Platzhalter in HTML-Dateien
+String templateProcessor(const String& var) {
+ if (var == "registered") return filamanRegistered ? "Registered" : "Not Registered";
+ if (var == "filamanUrl") return filamanUrl;
+ if (var == "autoTare") return autoTare ? "checked" : "";
+ return String(); // Unbekannte Variable - leer zurückgeben
+}
+
void sendNfcDataToClient(AsyncWebSocketClient *client) {
if(!client) return;
switch(nfcReaderState){
@@ -119,69 +129,47 @@ void setupWebserver(AsyncWebServer &server) {
ws.onEvent(onWsEvent);
server.addHandler(&ws);
- // Static Routes
+ // ===== STATISCHE ASSETS mit serveStatic() =====
+ // Optimiert für concurrent Requests, mit 24h Browser-Cache
+ // serveStatic() ist nicht-blockierend und handhabt parallele Requests besser
+ server.serveStatic("/style.css", LittleFS, "/style.css")
+ .setCacheControl(CACHE_ASSETS);
+ server.serveStatic("/logo.png", LittleFS, "/logo.png")
+ .setCacheControl(CACHE_ASSETS);
+ server.serveStatic("/favicon.ico", LittleFS, "/favicon.ico")
+ .setCacheControl(CACHE_ASSETS);
+
+ // ===== STATISCHE HTML-SEITEN (ohne Template-Ersetzung) =====
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Web: Request /");
- AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/index.html", "text/html");
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
- });
-
- server.on("/waage", HTTP_GET, [](AsyncWebServerRequest *request){
- Serial.println("Web: Request /waage");
- String html = readFile("/waage.html");
- html.replace("{{autoTare}}", autoTare ? "checked" : "");
- auto response = request->beginResponse(200, "text/html", html);
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
- });
-
- server.on("/setup", HTTP_GET, [](AsyncWebServerRequest *request){
- Serial.println("Web: Request /setup");
- String html = readFile("/setup.html");
- html.replace("{{registered}}", filamanRegistered ? "Registered" : "Not Registered");
- html.replace("{{filamanUrl}}", filamanUrl);
- auto response = request->beginResponse(200, "text/html", html);
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
+ request->send(LittleFS, "/index.html", "text/html");
});
server.on("/wifi", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Web: Request /wifi");
- AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/wifi.html", "text/html");
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
+ request->send(LittleFS, "/wifi.html", "text/html");
});
server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println("Web: Request /upgrade");
- AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/upgrade.html", "text/html");
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
+ request->send(LittleFS, "/upgrade.html", "text/html");
});
server.on("/version.txt", HTTP_GET, [](AsyncWebServerRequest *request){
- AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/version.txt", "text/plain");
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
+ request->send(LittleFS, "/version.txt", "text/plain");
});
- server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
- AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/style.css", "text/css");
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
+ // ===== DYNAMISCHE HTML-SEITEN (mit Template-Processor) =====
+ // Nutzt den eingebauten Template-Processor für %variable% Ersetzung
+ // Nicht-blockierend: Datei wird gestreamt und Variablen on-the-fly ersetzt
+ server.on("/waage", HTTP_GET, [](AsyncWebServerRequest *request){
+ Serial.println("Web: Request /waage");
+ request->send(LittleFS, "/waage.html", "text/html", false, templateProcessor);
});
- server.on("/logo.png", HTTP_GET, [](AsyncWebServerRequest *request){
- AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/logo.png", "image/png");
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
- });
-
- server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){
- AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/favicon.ico", "image/x-icon");
- response->addHeader("Cache-Control", NO_CACHE);
- request->send(response);
+ server.on("/setup", HTTP_GET, [](AsyncWebServerRequest *request){
+ Serial.println("Web: Request /setup");
+ request->send(LittleFS, "/setup.html", "text/html", false, templateProcessor);
});
// API Routes
diff --git a/src/wlan.cpp b/src/wlan.cpp
index 9e49e34..52ff330 100644
--- a/src/wlan.cpp
+++ b/src/wlan.cpp
@@ -17,15 +17,15 @@ void wifiSettings() {
WiFi.mode(WIFI_STA);
WiFi.setHostname("FilaMan");
- // Wir lassen das Power Management (Sleep) auf dem ESP-Standard.
- // Ein erzwungenes Ausschalten führt zu RF-Abstürzen bei vielen asynchronen HTTP-Requests.
+ // KRITISCH: Power-Save deaktivieren für stabilen Webserver-Betrieb
+ // Ohne dies geht WiFi in Modem-Sleep und verpasst Requests/Responses
+ // Dies ist die Hauptursache für Verbindungsabbrüche bei regelmäßigen API-Calls
+ esp_wifi_set_ps(WIFI_PS_NONE);
// Angemessene Sendeleistung (reduziert Hitzeprobleme)
- WiFi.setTxPower(WIFI_POWER_17dBm); // Set stable transmit power (default is 19.5 which gets hot)
-
- // Wir überlassen die Protokoll-Aushandlung (b/g/n) dem Treiber. Harte Vorgaben crashen unter Last.
+ WiFi.setTxPower(WIFI_POWER_17dBm);
- // Aktiviere WiFi-Roaming für bessere Stabilität
+ // Aktiviere WiFi-Roaming für bessere Stabilität bei schwachem Signal
esp_wifi_set_rssi_threshold(-80);
}
@@ -76,10 +76,13 @@ void checkWiFiConnection() {
if (WiFi.status() != WL_CONNECTED)
{
if (wifiOn) {
- Serial.println("WiFi connection lost. LwIP is auto-reconnecting...");
+ Serial.println("WiFi connection lost. Attempting active reconnect...");
wifiOn = false;
oledShowTopRow();
oledDisplayText(tr(STR_WIFI_RECONNECTING));
+
+ // Aktiver Reconnect-Versuch statt nur auf LwIP auto-reconnect zu warten
+ WiFi.reconnect();
}
wifiErrorCounter++;
@@ -95,10 +98,13 @@ void checkWiFiConnection() {
else
{
if (!wifiOn) {
- Serial.println("WiFi reconnected.");
+ Serial.println("WiFi reconnected successfully.");
wifiErrorCounter = 0;
wifiOn = true;
oledShowTopRow();
+
+ // Power-Save erneut deaktivieren nach Reconnect (wird manchmal zurückgesetzt)
+ esp_wifi_set_ps(WIFI_PS_NONE);
} else {
// Reset error counter if we are connected (just to be safe)
wifiErrorCounter = 0;