fix: mutex FreeRTOS buffer historique, découplage LittleFS/REST

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-23 16:43:05 +02:00
parent 29f26c1db4
commit 49c54f4e48
4 changed files with 49 additions and 33 deletions
+5
View File
@@ -1,5 +1,7 @@
#pragma once
#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
// ── Constantes matérielles ──────────────────────────────────────────
#define ONE_WIRE_BUS 4 // GPIO 4 — bus OneWire DS18B20
@@ -62,3 +64,6 @@ extern SondeEtat sondesEtat[NB_SONDES];
extern PointHistorique historique[HIST_TAILLE];
extern uint16_t histIdx;
extern NetworkStatus netStatus;
// Mutex FreeRTOS protégeant l'accès concurrent au buffer historique
// (Core 0 : callbacks AsyncWebServer vs Core 1 : loop/sensors_update)
extern SemaphoreHandle_t xHistMutex;
+4 -1
View File
@@ -13,10 +13,13 @@ SondeEtat sondesEtat[NB_SONDES] = {};
PointHistorique historique[HIST_TAILLE] = {};
uint16_t histIdx = 0;
NetworkStatus netStatus = {};
SemaphoreHandle_t xHistMutex = nullptr;
void setup() {
Serial.begin(115200);
Serial.println("[BOOT] esp_jardin démarrage...");
delay(500);
Serial.println("\n[BOOT] esp_jardin v1.0 — démarrage...");
xHistMutex = xSemaphoreCreateMutex();
network_init();
sensors_init();
web_server_init();
+5 -2
View File
@@ -53,12 +53,15 @@ bool sensors_update() {
}
}
// Enregistrement dans le buffer circulaire
// Enregistrement dans le buffer circulaire (protégé contre Core 0)
if (xSemaphoreTake(xHistMutex, pdMS_TO_TICKS(10)) == pdTRUE) {
historique[histIdx].timestamp = maintenant;
for (uint8_t i = 0; i < NB_SONDES; i++) {
historique[histIdx].temps[i] = sondesEtat[i].tempActuelle; // NAN si erreur
historique[histIdx].temps[i] = sondesEtat[i].tempActuelle;
}
histIdx = (histIdx + 1) % HIST_TAILLE;
xSemaphoreGive(xHistMutex);
}
return true; // nouvelle mesure disponible
}
+10 -5
View File
@@ -32,6 +32,7 @@ static String _buildJsonSondes() {
static String _buildJsonHistory() {
JsonDocument doc;
JsonArray arr = doc.to<JsonArray>();
if (xSemaphoreTake(xHistMutex, pdMS_TO_TICKS(50)) == pdTRUE) {
for (uint16_t i = 0; i < HIST_TAILLE; i++) {
uint16_t idx = (histIdx + i) % HIST_TAILLE;
if (historique[idx].timestamp == 0) continue;
@@ -46,6 +47,8 @@ static String _buildJsonHistory() {
}
}
}
xSemaphoreGive(xHistMutex);
}
String out;
serializeJson(doc, out);
return out;
@@ -61,11 +64,12 @@ static void _onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client,
}
void web_server_init() {
if (!LittleFS.begin()) {
Serial.println("[FS] Erreur montage LittleFS !");
return;
}
bool fsOk = LittleFS.begin();
if (!fsOk) {
Serial.println("[FS] Erreur montage LittleFS — interface web indisponible, API REST active");
} else {
Serial.println("[FS] LittleFS monté");
}
_ws.onEvent(_onWsEvent);
_server.addHandler(&_ws);
@@ -123,8 +127,9 @@ void web_server_init() {
}
);
// Servir index.html depuis LittleFS
if (fsOk) {
_server.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
}
_server.begin();
Serial.println("[HTTP] Serveur web démarré sur port 80");