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

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

View File

@ -63,7 +63,7 @@ jobs:
# Create release files
cp spiffs.bin filaman_spiffs.bin
# Create full binary
# Create full binary with correct partition offsets
echo "Creating full binary..."
esptool.py --chip esp32 merge_bin \
--fill-flash-size 4MB \
@ -71,10 +71,10 @@ jobs:
--flash_freq 40m \
--flash_size 4MB \
-o filaman_full.bin \
0x0000 bootloader.bin \
0x1000 bootloader.bin \
0x8000 partitions.bin \
0x10000 firmware.bin \
0x390000 spiffs.bin
0x310000 spiffs.bin
# Verify file sizes
echo "File sizes:"

View File

@ -52,7 +52,7 @@ jobs:
# Create release files
cp spiffs.bin filaman_spiffs.bin
# Create full binary
# Create full binary with correct partition offsets
echo "Creating full binary..."
esptool.py --chip esp32 merge_bin \
--fill-flash-size 4MB \
@ -60,10 +60,10 @@ jobs:
--flash_freq 40m \
--flash_size 4MB \
-o filaman_full.bin \
0x0000 bootloader.bin \
0x1000 bootloader.bin \
0x8000 partitions.bin \
0x10000 firmware.bin \
0x390000 spiffs.bin
0x310000 spiffs.bin
# Verify file sizes
echo "File sizes:"

View File

@ -1,190 +1,116 @@
<!-- head --><!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FilaMan - Filament Management Tool</title>
<link rel="icon" type="image/png" href="/favicon.ico">
<title>FilaMan - Firmware Update</title>
<link rel="stylesheet" href="style.css">
<style>
.debug-log {
background: #f5f5f5;
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
font-family: monospace;
max-height: 200px;
overflow-y: auto;
display: none;
}
</style>
</head>
<body>
<div class="navbar">
<div style="display: flex; align-items: center; gap: 2rem;">
<img src="/logo.png" alt="FilaMan Logo" class="logo">
<div class="logo-text">
<h1>FilaMan<span class="version">v1.2.92</span></h1>
<h4>Filament Management Tool</h4>
</div>
</div>
<nav style="display: flex; gap: 1rem;">
<a href="/">Start</a>
<a href="/waage">Scale</a>
<a href="/spoolman">Spoolman/Bambu</a>
<a href="/about">About</a>
<a href="/upgrade">Upgrade</a>
</nav>
<div class="status-container">
<div class="status-item">
<span class="status-dot" id="bambuDot"></span>B
</div>
<div class="status-item">
<span class="status-dot" id="spoolmanDot"></span>S
</div>
<div class="ram-status" id="ramStatus"></div>
</div>
</div>
<!-- head -->
<div class="content">
<h1>Firmware Upgrade</h1>
<h1>Firmware Update</h1>
<div class="warning">
<strong>Warning:</strong> Please do not turn off or restart the device during the update.
The device will restart automatically after the update.
<strong>Warning:</strong> Do not power off the device during update.
</div>
<div class="update-form">
<form id="updateForm" enctype='multipart/form-data'>
<input type='file' name='update' accept='.bin' required>
<input type='submit' value='Start Firmware Update'>
</form>
<form id="updateForm">
<input type="file" name="update" accept=".bin">
<button type="submit">Update Firmware</button>
</form>
<div class="progress" style="display: none;">
<div class="progress-bar"></div>
<div class="status"></div>
</div>
<div class="progress-container">
<div class="progress-bar">0%</div>
</div>
<div class="status"></div>
<div class="debug-log"></div>
</div>
<script>
// Hide status indicators during update
const statusContainer = document.querySelector('.status-container');
if (statusContainer) {
statusContainer.style.display = 'none';
const form = document.getElementById('updateForm');
const progress = document.querySelector('.progress');
const progressBar = document.querySelector('.progress-bar');
const status = document.querySelector('.status');
const debugLog = document.querySelector('.debug-log');
function log(message) {
debugLog.style.display = 'block';
const time = new Date().toLocaleTimeString();
debugLog.innerHTML += `[${time}] ${message}<br>`;
debugLog.scrollTop = debugLog.scrollHeight;
}
// Größenbeschränkung für Upload
const MAX_FILE_SIZE = 4000000; // 4MB
async function checkMagicByte(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const arr = new Uint8Array(reader.result);
// Prüfe auf Magic Byte 0xE9 für ESP32 Firmware
resolve(arr[0] === 0xE9);
};
reader.onerror = () => reject(reader.error);
reader.readAsArrayBuffer(file.slice(0, 1));
});
}
document.getElementById('updateForm').addEventListener('submit', async (e) => {
form.addEventListener('submit', async (e) => {
e.preventDefault();
const form = e.target;
const file = form.update.files[0];
if (!file) {
alert('Please select a firmware file.');
alert('Please select a file');
return;
}
if (file.size > MAX_FILE_SIZE) {
alert('File too large. Maximum size is 4MB.');
return;
}
// Prüfe Magic Byte für normale Firmware-Dateien
if (!file.name.endsWith('_spiffs.bin')) {
try {
const isValidFirmware = await checkMagicByte(file);
if (!isValidFirmware) {
alert('Invalid firmware file. Missing ESP32 magic byte.');
return;
}
} catch (error) {
console.error('Error checking magic byte:', error);
alert('Could not verify firmware file.');
return;
}
}
const progress = document.querySelector('.progress-bar');
const progressContainer = document.querySelector('.progress-container');
const status = document.querySelector('.status');
log(`Selected file: ${file.name} (${file.size} bytes)`);
progressContainer.style.display = 'block';
status.style.display = 'none';
status.className = 'status';
form.querySelector('input[type=submit]').disabled = true;
// Aktiviere Fortschrittsanzeige
progress.style.display = 'block';
form.style.display = 'none';
// Chunk-basierter Upload mit Retry-Logik
const chunkSize = 8192; // Optimale Chunk-Größe
const maxRetries = 3;
let offset = 0;
async function uploadChunk(chunk, retryCount = 0) {
try {
const response = await fetch('/update', {
method: 'POST',
body: chunk,
headers: {
'Content-Type': 'application/octet-stream',
'X-File-Name': file.name,
'X-Chunk-Offset': offset.toString(),
'X-Chunk-Size': chunk.size.toString(),
'X-Total-Size': file.size.toString()
},
timeout: 30000 // 30 Sekunden Timeout
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(errorText || response.statusText);
}
return true;
} catch (error) {
console.error(`Chunk upload failed (attempt ${retryCount + 1}):`, error);
if (retryCount < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 1000)); // Warte 1 Sekunde vor Retry
return uploadChunk(chunk, retryCount + 1);
}
throw error;
}
}
// Erstelle FormData für den Upload
const formData = new FormData();
formData.append('update', file);
try {
while (offset < file.size) {
const end = Math.min(offset + chunkSize, file.size);
const chunk = file.slice(offset, end);
await uploadChunk(chunk);
offset = end;
const percentComplete = (offset / file.size) * 100;
progress.style.width = percentComplete + '%';
progress.textContent = Math.round(percentComplete) + '%';
// Kleine Pause zwischen den Chunks für bessere Stabilität
await new Promise(resolve => setTimeout(resolve, 100));
log('Starting upload...');
const response = await fetch('/update', {
method: 'POST',
body: formData
});
log(`Server responded with status: ${response.status}`);
let result;
const contentType = response.headers.get('content-type');
log(`Response content-type: ${contentType}`);
if (contentType && contentType.includes('application/json')) {
result = await response.json();
log('Received JSON response');
} else {
const text = await response.text();
log(`Received text response: ${text}`);
result = { success: response.ok, message: text };
}
// Final success handler
status.textContent = 'Update successful! Device will restart...';
status.classList.add('success');
status.style.display = 'block';
// Warte auf Neustart und Redirect
setTimeout(() => {
window.location.href = '/';
}, 20000);
log(`Update result: ${JSON.stringify(result)}`);
if (response.ok && result.success) {
status.textContent = 'Update successful! Restarting...';
status.className = 'success';
setTimeout(() => {
window.location.href = '/';
}, 5000);
} else {
throw new Error(result.message || 'Update failed');
}
} catch (error) {
status.textContent = 'Update failed: ' + error.message;
status.classList.add('error');
status.style.display = 'block';
form.querySelector('input[type=submit]').disabled = false;
log(`Error: ${error.message}`);
console.error('Update error:', error);
status.textContent = `Error: ${error.message}`;
status.className = 'error';
form.style.display = 'block';
progress.style.display = 'none';
}
});
</script>

View File

@ -1,6 +1,6 @@
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x1C0000,
app1, app, ota_1, 0x1D0000, 0x1C0000,
spiffs, data, spiffs, 0x390000, 0x60000,
app0, app, ota_0, 0x10000, 0x180000,
app1, app, ota_1, 0x190000, 0x180000,
spiffs, data, spiffs, 0x310000, 0xE0000,
1 # Name Type SubType Offset Size Flags
2 nvs data nvs 0x9000 0x5000
3 otadata data ota 0xe000 0x2000
4 app0 app ota_0 0x10000 0x1C0000 0x180000
5 app1 app ota_1 0x1D0000 0x190000 0x1C0000 0x180000
6 spiffs data spiffs 0x390000 0x310000 0x60000 0xE0000

View File

@ -19,8 +19,10 @@ monitor_speed = 115200
lib_deps =
tzapu/WiFiManager @ ^2.0.17
https://github.com/me-no-dev/ESPAsyncWebServer.git#master
me-no-dev/AsyncTCP @ ^1.1.1
#https://github.com/me-no-dev/ESPAsyncWebServer.git#master
#me-no-dev/AsyncTCP @ ^1.1.1
mathieucarbou/ESPAsyncWebServer @ ^3.6.0
esp32async/AsyncTCP @ ^3.3.5
bogde/HX711 @ ^0.7.5
adafruit/Adafruit SSD1306 @ ^2.5.13
adafruit/Adafruit GFX Library @ ^1.11.11
@ -48,38 +50,14 @@ build_flags =
-DCORE_DEBUG_LEVEL=3
-DCONFIG_ARDUHAL_LOG_COLORS=1
-DOTA_DEBUG=1
-DARDUINO_RUNNING_CORE=1
-DARDUINO_EVENT_RUNNING_CORE=1
-DCONFIG_ESP32_PANIC_PRINT_REBOOT
-DCONFIG_ARDUINO_OTA_READSIZE=8192
-DCONFIG_ASYNC_TCP_RUNNING_CORE=1
-DCONFIG_ASYNC_TCP_USE_WDT=0
-DASYNC_TCP_USE_WDT=0
-DCONFIG_LWIP_TCP_TMR_INTERVAL=25
-DCONFIG_LWIP_TCP_MSS=1436
-DCONFIG_LWIP_TCP_SND_BUF_DEFAULT=5744
-DCONFIG_LWIP_TCP_WND_DEFAULT=5744
-DCONFIG_LWIP_TCP_RCV_BUF_DEFAULT=11488
-DCONFIG_LWIP_TCP_RECVMBOX_SIZE=64
-DCONFIG_LWIP_MAX_SOCKETS=10
-DCONFIG_LWIP_MAX_ACTIVE_TCP=16
-DCONFIG_LWIP_MAX_LISTENING_TCP=16
-DCONFIG_LWIP_TCP_MAXRTX=6
-DCONFIG_LWIP_TCP_SYNMAXRTX=3
-DCONFIG_LWIP_TCP_QUEUE_OOSEQ=0
-DCONFIG_LWIP_TCP_OVERSIZE_MSS=0
-DCONFIG_LWIP_TCP_RTO_TIME=1500
-DCONFIG_LWIP_IPV6=0
-DCONFIG_LWIP_STATS=0
-DCONFIG_LWIP_USE_ONLY_LWIP_SELECT=1
-DCONFIG_LWIP_NETIF_LOOPBACK=0
-DCONFIG_LWIP_IRAM_OPTIMIZATION=1
-DCONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=10
-DCONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=32
-DCONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=32
-DCONFIG_ESP32_WIFI_TX_BA_WIN=6
-DCONFIG_ESP32_WIFI_RX_BA_WIN=6
-DCONFIG_ESP32_WIFI_CSI_ENABLED=0
-DCONFIG_PARTITION_TABLE_OFFSET=0x8000
-DCONFIG_PARTITION_TABLE_MD5=y
-DCONFIG_APP_ROLLBACK_ENABLE=1
-DSPIFFS_MAX_PARTITIONS=3
-DCONFIG_SPIFFS_LOG_BLOCK_SIZE=4096
-DCONFIG_SPIFFS_CACHE=y
extra_scripts =
scripts/extra_script.py

View File

@ -1,5 +1,23 @@
Import("env")
board_config = env.BoardConfig()
# Calculate SPIFFS size based on partition table
SPIFFS_START = 0x310000 # From partitions.csv
SPIFFS_SIZE = 0xE0000 # From partitions.csv
SPIFFS_PAGE = 256
SPIFFS_BLOCK = 4096
env.Replace(
MKSPIFFSTOOL="mkspiffs",
SPIFFSBLOCKSZ=SPIFFS_BLOCK,
SPIFFSBLOCKSIZE=SPIFFS_BLOCK,
SPIFFSSTART=SPIFFS_START,
SPIFFSEND=SPIFFS_START + SPIFFS_SIZE,
SPIFFSPAGESZ=SPIFFS_PAGE,
SPIFFSSIZE=SPIFFS_SIZE
)
# Wiederverwendung der replace_version Funktion
exec(open("./scripts/pre_build.py").read())

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