LittleFS mount OK, updated interface, upload to littlefs from browser

This commit is contained in:
Emanuele Trabattoni
2026-04-09 13:41:50 +02:00
parent de9ffe40e5
commit 1e068476af
12 changed files with 686 additions and 72 deletions

View File

@@ -1,4 +1,4 @@
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
// Arduino Libraries
#include <Arduino.h>
@@ -16,6 +16,9 @@
#include <datasave.h>
#include <ui.h>
static File uploadFile;
static bool uploadFailed = false;
// FreeRTOS directives
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@@ -61,11 +64,6 @@ void setup()
LOG_DEBUG("ESP32 Heap:", ESP.getHeapSize());
LOG_DEBUG("ESP32 Sketch:", ESP.getFreeSketchSpace());
// Initialize Interrupt pins on PICKUP detectors
initTriggerPinsInputs();
// Initialize Interrupt pins on SPARK detectors
initSparkPinInputs();
// Init Wifi station
LOG_INFO("Initializing WiFi...");
WiFi.mode(WIFI_AP);
@@ -87,6 +85,11 @@ void setup()
vTaskDelay(pdMS_TO_TICKS(5000));
esp_restart();
}
// Initialize Interrupt pins on PICKUP detectors
initTriggerPinsInputs();
// Initialize Interrupt pins on SPARK detectors
initSparkPinInputs();
}
void loop()
@@ -123,7 +126,15 @@ void loop()
.spark_pin_34 = SPARK_PIN_A34},
.rt_resets = rtTaskResets{.rst_io_12p = RST_EXT_A12P, .rst_io_12n = RST_EXT_A12N, .rst_io_34p = RST_EXT_A34P, .rst_io_34n = RST_EXT_A34N}};
LOG_DEBUG("Task Variables OK");
if (!rt_taskA_queue || !rt_taskB_queue)
{
LOG_ERROR("Unable To Create task queues");
LOG_ERROR("5 seconds to restart...");
vTaskDelay(pdMS_TO_TICKS(5000));
esp_restart();
}
else
LOG_DEBUG("Task Variables OK");
#ifdef CH_B_ENABLE
QueueHandle_t rt_taskB_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
@@ -190,6 +201,7 @@ void loop()
RT_TASK_PRIORITY,
&trigA_TaskHandle,
CORE_0);
delay(100);
// Ignition B on Core 1
auto ignB_task_success = pdPASS;
@@ -202,11 +214,12 @@ void loop()
RT_TASK_PRIORITY, // priorità leggermente più alta
&trigB_TaskHandle,
CORE_1);
delay(100);
#endif
if ((ignA_task_success && ignB_task_success) != pdPASS)
if (ignA_task_success != pdPASS || ignB_task_success != pdPASS)
{
LOG_ERROR("Una ble to initialize ISR task");
LOG_ERROR("Unable to initialize ISR task");
LOG_ERROR("5 seconds to restart...");
vTaskDelay(pdMS_TO_TICKS(5000));
esp_restart();
@@ -219,7 +232,8 @@ void loop()
uint32_t counter = 0;
uint32_t wait_count = 0;
ignitionBoxStatus ign_info;
ignitionBoxStatusAverage ign_info_avg(filter_k);
ignitionBoxStatusAverage ign_info_avg(filter_k);
LITTLEFSGuard fsGuard;
// Initialize Web page
AsyncWebServer server(80);
@@ -227,6 +241,67 @@ void loop()
ws.onEvent(onWsEvent);
server.addHandler(&ws);
server.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
server.on("/upload", HTTP_POST,
[](AsyncWebServerRequest *request) {
if (uploadFailed)
{
request->send(500, "text/plain", "Upload failed");
}
else
{
request->send(200, "text/plain", "Upload successful");
}
},
[](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) {
if (index == 0)
{
uploadFailed = false;
String safeName = filename;
int slashIndex = safeName.lastIndexOf('/');
if (slashIndex >= 0)
{
safeName = safeName.substring(slashIndex + 1);
}
if (safeName.length() == 0)
{
uploadFailed = true;
return;
}
String filePath = "/" + safeName;
if (LittleFS.exists(filePath))
{
LittleFS.remove(filePath);
}
uploadFile = LittleFS.open(filePath, FILE_WRITE);
if (!uploadFile)
{
uploadFailed = true;
LOG_ERROR("Failed to open upload file:", filePath);
return;
}
}
if (!uploadFailed && uploadFile)
{
if (uploadFile.write(data, len) != len)
{
uploadFailed = true;
}
}
if (final && uploadFile)
{
uploadFile.close();
if (!uploadFailed)
{
LOG_INFO("Uploaded file to LittleFS:", filename);
}
}
});
server.begin();
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
@@ -253,12 +328,12 @@ void loop()
auto &hist = *active_history;
hist[counter++ % active_history->size()] = ign_info;
ign_info_avg.update(ign_info); // update moving average with latest ignition status
Serial.print("Data Received: " + String(counter) + "/" + String(hist.size()) + '\r');
Serial.print("\033[2K Data Received: " + String(counter) + "/" + String(hist.size()) + '\r');
if (ws.count() > 0 && counter % 10 == 0) // send data every 10 samples
if (ws.count() > 0 && counter % filter_k == 0) // send data every 10 samples
{
Serial.println();
LOG_INFO("Sending average ignition status to websocket clients...");
LOG_DEBUG("Sending average ignition status to websocket clients...");
auto msg = ign_info_avg.toJson().as<String>();
ws.textAll(msg);
}