fix: mutex FreeRTOS buffer historique, découplage LittleFS/REST
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/semphr.h>
|
||||||
|
|
||||||
// ── Constantes matérielles ──────────────────────────────────────────
|
// ── Constantes matérielles ──────────────────────────────────────────
|
||||||
#define ONE_WIRE_BUS 4 // GPIO 4 — bus OneWire DS18B20
|
#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 PointHistorique historique[HIST_TAILLE];
|
||||||
extern uint16_t histIdx;
|
extern uint16_t histIdx;
|
||||||
extern NetworkStatus netStatus;
|
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
@@ -13,10 +13,13 @@ SondeEtat sondesEtat[NB_SONDES] = {};
|
|||||||
PointHistorique historique[HIST_TAILLE] = {};
|
PointHistorique historique[HIST_TAILLE] = {};
|
||||||
uint16_t histIdx = 0;
|
uint16_t histIdx = 0;
|
||||||
NetworkStatus netStatus = {};
|
NetworkStatus netStatus = {};
|
||||||
|
SemaphoreHandle_t xHistMutex = nullptr;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
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();
|
network_init();
|
||||||
sensors_init();
|
sensors_init();
|
||||||
web_server_init();
|
web_server_init();
|
||||||
|
|||||||
+5
-2
@@ -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;
|
historique[histIdx].timestamp = maintenant;
|
||||||
for (uint8_t i = 0; i < NB_SONDES; i++) {
|
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;
|
histIdx = (histIdx + 1) % HIST_TAILLE;
|
||||||
|
xSemaphoreGive(xHistMutex);
|
||||||
|
}
|
||||||
|
|
||||||
return true; // nouvelle mesure disponible
|
return true; // nouvelle mesure disponible
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-5
@@ -32,6 +32,7 @@ static String _buildJsonSondes() {
|
|||||||
static String _buildJsonHistory() {
|
static String _buildJsonHistory() {
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonArray arr = doc.to<JsonArray>();
|
JsonArray arr = doc.to<JsonArray>();
|
||||||
|
if (xSemaphoreTake(xHistMutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
||||||
for (uint16_t i = 0; i < HIST_TAILLE; i++) {
|
for (uint16_t i = 0; i < HIST_TAILLE; i++) {
|
||||||
uint16_t idx = (histIdx + i) % HIST_TAILLE;
|
uint16_t idx = (histIdx + i) % HIST_TAILLE;
|
||||||
if (historique[idx].timestamp == 0) continue;
|
if (historique[idx].timestamp == 0) continue;
|
||||||
@@ -46,6 +47,8 @@ static String _buildJsonHistory() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
xSemaphoreGive(xHistMutex);
|
||||||
|
}
|
||||||
String out;
|
String out;
|
||||||
serializeJson(doc, out);
|
serializeJson(doc, out);
|
||||||
return out;
|
return out;
|
||||||
@@ -61,11 +64,12 @@ static void _onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void web_server_init() {
|
void web_server_init() {
|
||||||
if (!LittleFS.begin()) {
|
bool fsOk = LittleFS.begin();
|
||||||
Serial.println("[FS] Erreur montage LittleFS !");
|
if (!fsOk) {
|
||||||
return;
|
Serial.println("[FS] Erreur montage LittleFS — interface web indisponible, API REST active");
|
||||||
}
|
} else {
|
||||||
Serial.println("[FS] LittleFS monté");
|
Serial.println("[FS] LittleFS monté");
|
||||||
|
}
|
||||||
|
|
||||||
_ws.onEvent(_onWsEvent);
|
_ws.onEvent(_onWsEvent);
|
||||||
_server.addHandler(&_ws);
|
_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.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
|
||||||
|
}
|
||||||
|
|
||||||
_server.begin();
|
_server.begin();
|
||||||
Serial.println("[HTTP] Serveur web démarré sur port 80");
|
Serial.println("[HTTP] Serveur web démarré sur port 80");
|
||||||
|
|||||||
Reference in New Issue
Block a user