refactor: update SPIFFS initialization and partition sizes; enhance WiFi setup

This commit is contained in:
2025-02-21 10:35:52 +01:00
parent 513d74fdb0
commit b8e4af4e4d
12 changed files with 220 additions and 231 deletions

View File

@ -1,4 +1,5 @@
#include "commonFS.h"
#include <SPIFFS.h>
bool saveJsonValue(const char* filename, const JsonDocument& doc) {
File file = SPIFFS.open(filename, "w");
@ -35,23 +36,12 @@ bool loadJsonValue(const char* filename, JsonDocument& doc) {
return true;
}
bool initializeSPIFFS() {
// Erster Versuch
if (SPIFFS.begin(true)) {
Serial.println("SPIFFS mounted successfully.");
return true;
void initializeSPIFFS() {
if (!SPIFFS.begin(true, "/spiffs", 10, "spiffs")) {
Serial.println("SPIFFS Mount Failed");
return;
}
// Formatierung versuchen
Serial.println("Failed to mount SPIFFS. Formatting...");
SPIFFS.format();
// Zweiter Versuch nach Formatierung
if (SPIFFS.begin(true)) {
Serial.println("SPIFFS formatted and mounted successfully.");
return true;
}
Serial.println("SPIFFS initialization failed completely.");
return false;
Serial.printf("SPIFFS Total: %u bytes\n", SPIFFS.totalBytes());
Serial.printf("SPIFFS Used: %u bytes\n", SPIFFS.usedBytes());
Serial.printf("SPIFFS Free: %u bytes\n", SPIFFS.totalBytes() - SPIFFS.usedBytes());
}

View File

@ -7,6 +7,6 @@
bool saveJsonValue(const char* filename, const JsonDocument& doc);
bool loadJsonValue(const char* filename, JsonDocument& doc);
bool initializeSPIFFS();
void initializeSPIFFS();
#endif

View File

@ -10,7 +10,7 @@
#define UPLOAD_TIMEOUT_MS 60000 // 60 Sekunden Timeout für den gesamten Upload
#define CHUNK_RESPONSE_TIMEOUT_MS 10000 // 10 Sekunden Timeout pro Chunk
#define MAX_FAILED_CHUNKS 3 // Maximale Anzahl fehlgeschlagener Chunks bevor Abbruch
#define MAX_FILE_SIZE 4000000 // 4MB Limit
#define MAX_FILE_SIZE 4194304 // 4MB Limit
static bool tasksAreStopped = false;
static uint32_t lastChunkTime = 0;

View File

@ -8,9 +8,11 @@
#include "scale.h"
#include "esp_task_wdt.h"
#include "ota.h"
#include <Update.h>
// Cache-Control Header definieren
#define CACHE_CONTROL "max-age=31536000" // Cache für 1 Jahr
#define MAX_UPLOAD_SIZE 4194304 // 4MB maximale Upload-Größe
AsyncWebServer server(webserverPort);
AsyncWebSocket ws("/ws");
@ -156,6 +158,10 @@ void sendAmsData(AsyncWebSocketClient *client) {
}
void setupWebserver(AsyncWebServer &server) {
// Konfiguriere Server für große Uploads
server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){});
server.onFileUpload([](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final){});
// Lade die Spoolman-URL beim Booten
spoolmanUrl = loadSpoolmanUrl();
Serial.print("Geladene Spoolman-URL: ");
@ -338,28 +344,72 @@ void setupWebserver(AsyncWebServer &server) {
Serial.println("RFID.js gesendet");
});
// Route for Firmware Update
// Vereinfachter Update-Handler
server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest *request) {
// During OTA, reduce memory usage
ws.enable(false); // Temporarily disable WebSocket
ws.cleanupClients();
Serial.println("Request for /upgrade received");
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/upgrade.html.gz", "text/html");
response->addHeader("Content-Encoding", "gzip");
response->addHeader("Cache-Control", CACHE_CONTROL);
response->addHeader("Cache-Control", "no-store");
request->send(response);
});
// Update-Handler mit verbesserter Fehlerbehandlung
server.on("/update", HTTP_POST,
[](AsyncWebServerRequest *request) {
// The response will be sent from handleOTAUpload when the upload is complete
// Nach Update-Abschluss
bool success = !Update.hasError();
AsyncWebServerResponse *response = request->beginResponse(
success ? 200 : 400,
"application/json",
success ? "{\"success\":true,\"message\":\"Update successful\"}"
: "{\"success\":false,\"message\":\"Update failed\"}"
);
response->addHeader("Connection", "close");
request->send(response);
if (success) {
delay(500);
ESP.restart();
}
},
[](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
// Free memory before handling update
ws.enable(false);
ws.cleanupClients();
handleOTAUpload(request, filename, index, data, len, final);
static size_t updateSize = 0;
static int command = 0;
if (!index) {
updateSize = request->contentLength();
command = (filename.indexOf("spiffs") > -1) ? U_SPIFFS : U_FLASH;
Serial.printf("Update Start: %s\nSize: %u\nCommand: %d\n", filename.c_str(), updateSize, command);
if (!Update.begin(updateSize, command)) {
Serial.printf("Update Begin Error: ");
Update.printError(Serial);
String errorMsg = String("Update begin failed: ") + Update.errorString();
request->send(400, "application/json", "{\"success\":false,\"message\":\"" + errorMsg + "\"}");
return;
}
}
if (len) {
if (Update.write(data, len) != len) {
Serial.printf("Update Write Error: ");
Update.printError(Serial);
String errorMsg = String("Write failed: ") + Update.errorString();
request->send(400, "application/json", "{\"success\":false,\"message\":\"" + errorMsg + "\"}");
return;
}
Serial.printf("Progress: %u/%u\r", index + len, updateSize);
}
if (final) {
if (!Update.end(true)) {
Serial.printf("Update End Error: ");
Update.printError(Serial);
String errorMsg = String("Update end failed: ") + Update.errorString();
request->send(400, "application/json", "{\"success\":false,\"message\":\"" + errorMsg + "\"}");
return;
}
Serial.printf("\nUpdate Success: %uB\n", index+len);
}
}
);
@ -379,3 +429,23 @@ void setupWebserver(AsyncWebServer &server) {
server.begin();
Serial.println("Webserver gestartet");
}
// Upload-Handler für alle Datei-Uploads
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
if (!filename.endsWith(".bin")) {
request->send(400, "application/json", "{\"success\":false,\"message\":\"Invalid file type\"}");
return;
}
if (request->url() == "/update") {
handleOTAUpload(request, filename, index, data, len, final);
}
}
// Body-Handler für große Anfragen
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
// Handler für große POST-Anfragen
if (total > MAX_UPLOAD_SIZE) {
request->send(413, "application/json", "{\"success\":false,\"message\":\"File too large\"}");
}
}

View File

@ -6,8 +6,8 @@
#include "commonFS.h"
#include "api.h"
#include <ArduinoJson.h>
#include <ESPAsyncWebServer.h>
#include <AsyncWebSocket.h>
#include <Update.h>
#include <AsyncTCP.h>
#include "bambu.h"
#include "nfc.h"
#include "scale.h"
@ -17,7 +17,13 @@ extern String spoolmanUrl;
extern AsyncWebServer server;
extern AsyncWebSocket ws;
// Server-Initialisierung und Handler
void initWebServer();
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final);
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total);
void setupWebserver(AsyncWebServer &server);
// WebSocket-Funktionen
void sendAmsData(AsyncWebSocketClient *client);
void sendNfcData(AsyncWebSocketClient *client);
void foundNfcTag(AsyncWebSocketClient *client, uint8_t success);

View File

@ -12,6 +12,7 @@ bool wm_nonblocking = false;
void initWiFi() {
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
WiFi.setSleep(false); // disable sleep mode
esp_wifi_set_ps(WIFI_PS_NONE);
//esp_wifi_set_max_tx_power(72); // Setze maximale Sendeleistung auf 20dBm