Compare commits
5 Commits
vibe
...
f1d9abc4c7
| Author | SHA1 | Date | |
|---|---|---|---|
| f1d9abc4c7 | |||
| 752cd86234 | |||
| e5fecde7f5 | |||
| cfc8438042 | |||
| aa69687ee8 |
2
.vscode/extensions.json
vendored
2
.vscode/extensions.json
vendored
@@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
|
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||||
|
// for the documentation about the extensions.json format
|
||||||
"recommendations": [
|
"recommendations": [
|
||||||
"pioarduino.pioarduino-ide",
|
"pioarduino.pioarduino-ide",
|
||||||
"platformio.platformio-ide"
|
"platformio.platformio-ide"
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
9
|
10
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
|
#include <WiFiClientSecure.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <Elog.h>
|
#include <Elog.h>
|
||||||
#include <HTTPUpdate.h>
|
#include <HTTPUpdate.h>
|
||||||
@@ -97,11 +98,11 @@ void run_ota_update(String url, std::function<void()> callback_started, std::fu
|
|||||||
httpUpdate.onError(callback_error);
|
httpUpdate.onError(callback_error);
|
||||||
Logger.log(0, ELOG_LEVEL_DEBUG, "Defined callbacks, Starting update now");
|
Logger.log(0, ELOG_LEVEL_DEBUG, "Defined callbacks, Starting update now");
|
||||||
|
|
||||||
t_httpUpdate_return ret;
|
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
|
||||||
|
|
||||||
if (url.startsWith("https")) {
|
if (url.startsWith("https")) {
|
||||||
Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL");
|
Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL");
|
||||||
WiFiClient client;
|
WiFiClientSecure client;
|
||||||
// client.setInsecure();
|
// client.setInsecure();
|
||||||
ret = httpUpdate.update(client, url);
|
ret = httpUpdate.update(client, url);
|
||||||
} else if (url.startsWith("http")) {
|
} else if (url.startsWith("http")) {
|
||||||
@@ -110,15 +111,20 @@ void run_ota_update(String url, std::function<void()> callback_started, std::fu
|
|||||||
ret = httpUpdate.update(client, url);
|
ret = httpUpdate.update(client, url);
|
||||||
} else {
|
} else {
|
||||||
Logger.log(0, ELOG_LEVEL_ERROR, "URL is not valid: \n%s", url.c_str());
|
Logger.log(0, ELOG_LEVEL_ERROR, "URL is not valid: \n%s", url.c_str());
|
||||||
|
if (callback_error) callback_error(-1);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case HTTP_UPDATE_FAILED:
|
case HTTP_UPDATE_FAILED:
|
||||||
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
|
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
|
||||||
|
// Don't restart ESP on HTTP errors, just return
|
||||||
|
if (callback_error) callback_error(httpUpdate.getLastError());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case HTTP_UPDATE_NO_UPDATES:
|
case HTTP_UPDATE_NO_UPDATES:
|
||||||
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES");
|
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES");
|
||||||
|
if (callback_error) callback_error(-2);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case HTTP_UPDATE_OK:
|
case HTTP_UPDATE_OK:
|
||||||
@@ -136,12 +142,12 @@ void run_ota_spiffs_update(String url, std::function<void()> callback_started,
|
|||||||
httpUpdate.onError(callback_error);
|
httpUpdate.onError(callback_error);
|
||||||
Logger.log(0, ELOG_LEVEL_DEBUG, "Defined callbacks, Starting update now");
|
Logger.log(0, ELOG_LEVEL_DEBUG, "Defined callbacks, Starting update now");
|
||||||
|
|
||||||
t_httpUpdate_return ret;
|
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
|
||||||
|
|
||||||
if (url.startsWith("https")) {
|
if (url.startsWith("https")) {
|
||||||
Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL");
|
Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL");
|
||||||
WiFiClient client;
|
WiFiClientSecure client;
|
||||||
// client.setInsecure();
|
client.setInsecure();
|
||||||
ret = httpUpdate.updateSpiffs(client, url);
|
ret = httpUpdate.updateSpiffs(client, url);
|
||||||
} else if (url.startsWith("http")) {
|
} else if (url.startsWith("http")) {
|
||||||
Logger.log(0, ELOG_LEVEL_DEBUG, "HTTP URL");
|
Logger.log(0, ELOG_LEVEL_DEBUG, "HTTP URL");
|
||||||
@@ -149,15 +155,20 @@ void run_ota_spiffs_update(String url, std::function<void()> callback_started,
|
|||||||
ret = httpUpdate.updateSpiffs(client, url);
|
ret = httpUpdate.updateSpiffs(client, url);
|
||||||
} else {
|
} else {
|
||||||
Logger.log(0, ELOG_LEVEL_ERROR, "URL is not valid: \n%s", url.c_str());
|
Logger.log(0, ELOG_LEVEL_ERROR, "URL is not valid: \n%s", url.c_str());
|
||||||
|
if (callback_error) callback_error(-1);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case HTTP_UPDATE_FAILED:
|
case HTTP_UPDATE_FAILED:
|
||||||
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
|
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
|
||||||
|
// Don't restart ESP on HTTP errors, just return
|
||||||
|
if (callback_error) callback_error(httpUpdate.getLastError());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case HTTP_UPDATE_NO_UPDATES:
|
case HTTP_UPDATE_NO_UPDATES:
|
||||||
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES");
|
Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES");
|
||||||
|
if (callback_error) callback_error(-2);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case HTTP_UPDATE_OK:
|
case HTTP_UPDATE_OK:
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ extends = env:esp32_base
|
|||||||
lib_deps =
|
lib_deps =
|
||||||
${env:esp32_base.lib_deps}
|
${env:esp32_base.lib_deps}
|
||||||
INA233
|
INA233
|
||||||
upload_protocol = espota
|
; upload_protocol = espota
|
||||||
upload_port = 192.168.5.205
|
; upload_port = 192.168.18.18
|
||||||
|
|
||||||
[env:ESP32_INA226]
|
[env:ESP32_INA226]
|
||||||
extends = env:esp32_base
|
extends = env:esp32_base
|
||||||
@@ -43,7 +43,7 @@ lib_deps =
|
|||||||
${env:esp32_base.lib_deps}
|
${env:esp32_base.lib_deps}
|
||||||
robtillaart/INA226@ ~0.6.4
|
robtillaart/INA226@ ~0.6.4
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = 192.168.6.45
|
upload_port = 192.168.18.18
|
||||||
build_flags = ${env:esp32_base.build_flags} -DUSE_INA226
|
build_flags = ${env:esp32_base.build_flags} -DUSE_INA226
|
||||||
|
|
||||||
[env:native]
|
[env:native]
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#define water_level_min_key "water_level_min"
|
#define water_level_min_key "water_level_min"
|
||||||
#define water_level_max_key "water_level_max"
|
#define water_level_max_key "water_level_max"
|
||||||
#define water_volume_key "water_volume"
|
#define water_volume_key "water_volume"
|
||||||
#define current_software_version Version{2, 2, 0}
|
#define current_software_version Version{2, 5, 0}
|
||||||
#define REQUIRED_SPIFFS_VERSION Version{9, 0, 0}
|
#define REQUIRED_SPIFFS_VERSION Version{10, 0, 0}
|
||||||
|
|
||||||
#define RESISTOR_VALUE 4
|
#define RESISTOR_VALUE 4
|
||||||
117
src/main.cpp
117
src/main.cpp
@@ -1,7 +1,6 @@
|
|||||||
#include "SPIFFS.h"
|
#include "SPIFFS.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <AsyncTCP.h>
|
#include <AsyncTCP.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include <ETH.h>
|
#include <ETH.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <HTTPUpdate.h>
|
#include <HTTPUpdate.h>
|
||||||
@@ -15,6 +14,7 @@
|
|||||||
#include "global_data/defines.h"
|
#include "global_data/defines.h"
|
||||||
|
|
||||||
#include "networking/networking.h"
|
#include "networking/networking.h"
|
||||||
|
#include "networking/webserver.h"
|
||||||
|
|
||||||
#include "external_interfacing/leds.h"
|
#include "external_interfacing/leds.h"
|
||||||
#include "sensor/sensor.h"
|
#include "sensor/sensor.h"
|
||||||
@@ -22,7 +22,6 @@
|
|||||||
#include "tools/tools.h"
|
#include "tools/tools.h"
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
#include "networking/json_builder.h"
|
#include "networking/json_builder.h"
|
||||||
#include "networking/responses.h"
|
|
||||||
#include <fetchOTA.h>
|
#include <fetchOTA.h>
|
||||||
#include "time.h"
|
#include "time.h"
|
||||||
#include "tools/log.h"
|
#include "tools/log.h"
|
||||||
@@ -32,17 +31,16 @@
|
|||||||
|
|
||||||
Preferences prefs;
|
Preferences prefs;
|
||||||
|
|
||||||
extern WaterData water_data;
|
|
||||||
extern DeviceTelemetry telemetry;
|
extern DeviceTelemetry telemetry;
|
||||||
extern NetworkData wifi_data;
|
extern NetworkData wifi_data;
|
||||||
extern NetworkData ethernet_data;
|
extern NetworkData ethernet_data;
|
||||||
extern SensorData shunt_data;
|
extern SensorData shunt_data;
|
||||||
extern ActiveErrors active_errors;
|
extern ActiveErrors active_errors;
|
||||||
|
|
||||||
|
extern AsyncWebSocket webSocket;
|
||||||
|
|
||||||
Version current_spiffs_version;
|
Version current_spiffs_version;
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
|
||||||
AsyncWebSocket webSocket("/webSocket");
|
|
||||||
#define FORMAT_LITTLEFS_IF_FAILED true
|
#define FORMAT_LITTLEFS_IF_FAILED true
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
@@ -59,7 +57,7 @@ void setup()
|
|||||||
delay(500);
|
delay(500);
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Init Sensor");
|
LOG(ELOG_LEVEL_DEBUG, "Init Sensor");
|
||||||
init_sensor();
|
init_sensor();
|
||||||
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Beginning LittleFS");
|
LOG(ELOG_LEVEL_DEBUG, "Beginning LittleFS");
|
||||||
LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED);
|
LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED);
|
||||||
|
|
||||||
@@ -78,85 +76,6 @@ void setup()
|
|||||||
/////////////////////////////// ROUTES ///////////////////////////////
|
/////////////////////////////// ROUTES ///////////////////////////////
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Route Setup");
|
LOG(ELOG_LEVEL_DEBUG, "Route Setup");
|
||||||
|
|
||||||
// Normal HTML stuff
|
|
||||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/status.html", "text/html", false, processor); });
|
|
||||||
|
|
||||||
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/settings.html", "text/html", false, processor); });
|
|
||||||
|
|
||||||
server.on("/export", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/data_export.html", "text/html", false); });
|
|
||||||
|
|
||||||
server.on("/logic.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/logic.js", "application/javascript", false, processor); });
|
|
||||||
|
|
||||||
server.on("/update", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/update_progress.html", "text/html", false, processor); });
|
|
||||||
|
|
||||||
// API stuff - internal
|
|
||||||
server.on("/update_wifi_credentials", HTTP_POST, [](AsyncWebServerRequest* request) {
|
|
||||||
// For settings SSID
|
|
||||||
if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) {
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Updating SSID config");
|
|
||||||
const AsyncWebParameter* ssid_param = request->getParam(ssid_key, true);
|
|
||||||
const AsyncWebParameter* password_param = request->getParam(wifi_password_key, true);
|
|
||||||
prefs.putString(ssid_key, ssid_param->value().c_str());
|
|
||||||
prefs.putString(wifi_password_key, password_param->value().c_str());
|
|
||||||
|
|
||||||
} else {
|
|
||||||
request->send(400, "text/plain", "Missing parameters"); // TODO add proper error messages
|
|
||||||
}
|
|
||||||
request->send(LittleFS, "/settings.html", "text/html", false, processor); // TODO add proper return templating
|
|
||||||
});
|
|
||||||
|
|
||||||
server.on("/update_sensor_settings", HTTP_POST, [](AsyncWebServerRequest* request) {
|
|
||||||
int params = request->params();
|
|
||||||
|
|
||||||
if (request->hasParam(level_sensor_range_key, true) && request->hasParam(water_level_min_key, true) && request->hasParam(water_level_max_key, true) && request->hasParam(water_volume_key, true)) {
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Updating Sensor config");
|
|
||||||
const AsyncWebParameter* range_param = request->getParam(level_sensor_range_key, true);
|
|
||||||
const AsyncWebParameter* level_min_param = request->getParam(water_level_min_key, true);
|
|
||||||
const AsyncWebParameter* level_max_param = request->getParam(water_level_max_key, true);
|
|
||||||
const AsyncWebParameter* liters_param = request->getParam(water_volume_key, true);
|
|
||||||
|
|
||||||
String range_str = range_param->value();
|
|
||||||
String level_min_str = level_min_param->value();
|
|
||||||
String level_max_str = level_max_param->value();
|
|
||||||
String liters_str = liters_param->value();
|
|
||||||
|
|
||||||
float range_float = range_str.toFloat();
|
|
||||||
float level_min_float = level_min_str.toFloat();
|
|
||||||
float level_max_float = level_max_str.toFloat();
|
|
||||||
float liters_float = liters_str.toFloat();
|
|
||||||
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "range_float:%D:", range_float);
|
|
||||||
|
|
||||||
prefs.putFloat(level_sensor_range_key, range_float);
|
|
||||||
prefs.putFloat(water_level_min_key, level_min_float);
|
|
||||||
prefs.putFloat(water_level_max_key, level_max_float);
|
|
||||||
prefs.putFloat(water_volume_key, liters_float);
|
|
||||||
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0));
|
|
||||||
} else {
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "!!!! FAIL lo");
|
|
||||||
for (int i = 0; i < params; i++) {
|
|
||||||
const AsyncWebParameter* p = request->getParam(i);
|
|
||||||
if (p->isFile()) { // p->isPost() is also true
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
|
||||||
} else if (p->isPost()) {
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
|
||||||
} else {
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
request->send(400, "text/plain", "Missing parameters"); // TODO add proper error messages
|
|
||||||
}
|
|
||||||
request->send(LittleFS, "/settings.html", "text/html", false); // TODO add proper return templating
|
|
||||||
});
|
|
||||||
|
|
||||||
setup_api_endpoints();
|
|
||||||
webSocket.onEvent(onWsEvent);
|
|
||||||
server.addHandler(&webSocket);
|
|
||||||
|
|
||||||
server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/chota.css", "text/css", false); });
|
|
||||||
server.on("/gauge.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/gauge.js", "application/javascript", false); });
|
|
||||||
|
|
||||||
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "OTA Setup");
|
LOG(ELOG_LEVEL_DEBUG, "OTA Setup");
|
||||||
ArduinoOTA
|
ArduinoOTA
|
||||||
@@ -181,23 +100,31 @@ void setup()
|
|||||||
|
|
||||||
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Starting main tasks");
|
LOG(ELOG_LEVEL_DEBUG, "Starting main tasks");
|
||||||
xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL);
|
|
||||||
xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL);
|
// Create a queue for water data communication between sensor and webserver tasks
|
||||||
xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, NULL, 1, NULL);
|
QueueHandle_t webserverWaterDataQueue = xQueueCreate(10, sizeof(WaterData));
|
||||||
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL);
|
if (webserverWaterDataQueue == NULL) {
|
||||||
xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL);
|
LOG(ELOG_LEVEL_ERROR, "Failed to create webserver water data queue");
|
||||||
xTaskCreate(get_time_task, "GetTimeTask", 1024 * 4, NULL, 1, NULL);
|
} else {
|
||||||
|
xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL);
|
||||||
|
xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL);
|
||||||
|
xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, webserverWaterDataQueue, 1, NULL);
|
||||||
|
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL);
|
||||||
|
xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL);
|
||||||
|
xTaskCreate(get_time_task, "GetTimeTask", 1024 * 4, NULL, 1, NULL);
|
||||||
|
|
||||||
delay(5000);
|
delay(5000);
|
||||||
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||||
|
xTaskCreate(webserver_task, "WebServerTask", 1024 * 8, webserverWaterDataQueue, 1, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
|
||||||
server.begin();
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler");
|
LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler");
|
||||||
ArduinoOTA.begin();
|
ArduinoOTA.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "json_builder.h"
|
#include "json_builder.h"
|
||||||
#include <Elog.h>
|
#include <Elog.h>
|
||||||
|
|
||||||
extern WaterData water_data;
|
|
||||||
extern DeviceTelemetry telemetry;
|
extern DeviceTelemetry telemetry;
|
||||||
extern NetworkData wifi_data;
|
extern NetworkData wifi_data;
|
||||||
extern NetworkData ethernet_data;
|
extern NetworkData ethernet_data;
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
#include "responses.h"
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include "AsyncJson.h"
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <Elog.h>
|
|
||||||
#include "json_builder.h"
|
|
||||||
#include "../tools/tools.h"
|
|
||||||
#include <SPIFFS.h>
|
|
||||||
|
|
||||||
extern WaterData water_data;
|
|
||||||
extern DeviceTelemetry telemetry;
|
|
||||||
extern NetworkData wifi_data;
|
|
||||||
extern NetworkData ethernet_data;
|
|
||||||
extern SensorData shunt_data;
|
|
||||||
extern OTAStatus ota_status;
|
|
||||||
|
|
||||||
extern AsyncWebServer server;
|
|
||||||
|
|
||||||
void setup_api_endpoints(){
|
|
||||||
server.on("/sensor_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
String output;
|
|
||||||
serializeJson(build_shunt_data_json(shunt_data), output);
|
|
||||||
request->send(200, "application/json", output); });
|
|
||||||
|
|
||||||
server.on("/water_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
String output;
|
|
||||||
serializeJson(build_water_data_json(water_data), output);
|
|
||||||
request->send(200, "application/json", output); });
|
|
||||||
|
|
||||||
server.on("/raw_percent", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
String output;
|
|
||||||
output = water_data.percentage;
|
|
||||||
request->send(200, "text/raw", output); });
|
|
||||||
|
|
||||||
server.on("/raw_level", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
String output;
|
|
||||||
output = water_data.level;
|
|
||||||
request->send(200, "text/raw", output); });
|
|
||||||
|
|
||||||
|
|
||||||
server.on("/telemetry", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
String output;
|
|
||||||
serializeJson(build_telemetry_json(telemetry), output);
|
|
||||||
request->send(200, "application/json", output); });
|
|
||||||
|
|
||||||
server.on("/network_info", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
String output;
|
|
||||||
serializeJson(build_network_json(ethernet_data, wifi_data), output);
|
|
||||||
request->send(200, "application/json", output);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.on("/ota_udpate_status", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
String output;
|
|
||||||
serializeJson(build_ota_json(ota_status), output);
|
|
||||||
request->send(200, "application/json", output);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.on("/run_ota_update", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
||||||
if (ota_status.update_progress > -1) {
|
|
||||||
request->send(200, "text/plain", "OTA Update already in progress");
|
|
||||||
return;
|
|
||||||
} else if (!ota_status.update_available) {
|
|
||||||
request->send(200, "text/plain", "No update available");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
static TaskArgs_t args = {
|
|
||||||
.ota_status = ota_status
|
|
||||||
};
|
|
||||||
|
|
||||||
xTaskCreate(run_ota_update_task, "RunOTAUpdate", 1024 * 8, (void *)&args, 1, NULL);
|
|
||||||
request->send(LittleFS, "/update_progress.html", "text/html", false, processor);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include <LittleFS.h>
|
|
||||||
|
|
||||||
void setup_api_endpoints();
|
|
||||||
284
src/networking/webserver.cpp
Normal file
284
src/networking/webserver.cpp
Normal file
@@ -0,0 +1,284 @@
|
|||||||
|
#include "webserver.h"
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
#include <LittleFS.h>
|
||||||
|
#include <Elog.h>
|
||||||
|
#include <AsyncTCP.h>
|
||||||
|
#include <Preferences.h>
|
||||||
|
#include "tools/log.h"
|
||||||
|
#include "AsyncJson.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/semphr.h>
|
||||||
|
#include <freertos/queue.h>
|
||||||
|
|
||||||
|
#include "../global_data/global_data.h"
|
||||||
|
#include "../tools/tools.h"
|
||||||
|
#include "../tools/readers_writer_lock.h"
|
||||||
|
#include "../global_data/defines.h"
|
||||||
|
#include "json_builder.h"
|
||||||
|
|
||||||
|
extern Preferences prefs;
|
||||||
|
extern DeviceTelemetry telemetry;
|
||||||
|
extern NetworkData wifi_data;
|
||||||
|
extern NetworkData ethernet_data;
|
||||||
|
extern SensorData shunt_data;
|
||||||
|
extern OTAStatus ota_status;
|
||||||
|
|
||||||
|
AsyncWebSocket webSocket("/webSocket");
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
|
// Local water data cache
|
||||||
|
WaterData local_water_data;
|
||||||
|
|
||||||
|
// Readers-writer lock for local_water_data
|
||||||
|
ReadersWriterLock waterDataLock;
|
||||||
|
|
||||||
|
// Queue to receive water data from the sensor task
|
||||||
|
QueueHandle_t webserverWaterDataQueue;
|
||||||
|
|
||||||
|
|
||||||
|
// ======================
|
||||||
|
// Webserver Setup
|
||||||
|
// ======================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets up all routes for the webserver.
|
||||||
|
*
|
||||||
|
* Configures routes for serving HTML pages, handling form submissions,
|
||||||
|
* REST API endpoints, WebSocket connections, and static assets.
|
||||||
|
*
|
||||||
|
* @note Calls setup_api_endpoints() to configure API-specific routes.
|
||||||
|
*/
|
||||||
|
void setup_routes() {
|
||||||
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/status.html", "text/html", false, processor); });
|
||||||
|
|
||||||
|
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/settings.html", "text/html", false, processor); });
|
||||||
|
|
||||||
|
server.on("/export", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/data_export.html", "text/html", false); });
|
||||||
|
|
||||||
|
server.on("/logic.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/logic.js", "application/javascript", false, processor); });
|
||||||
|
|
||||||
|
server.on("/update", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/update_progress.html", "text/html", false, processor); });
|
||||||
|
|
||||||
|
// API stuff - internal
|
||||||
|
server.on("/update_wifi_credentials", HTTP_POST, [](AsyncWebServerRequest* request) {
|
||||||
|
handle_update_wifi_credentials(request);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("/update_sensor_settings", HTTP_POST, [](AsyncWebServerRequest* request) {
|
||||||
|
handle_update_sensor_settings(request);
|
||||||
|
});
|
||||||
|
|
||||||
|
setup_api_endpoints();
|
||||||
|
webSocket.onEvent(onWsEvent);
|
||||||
|
server.addHandler(&webSocket);
|
||||||
|
|
||||||
|
server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/chota.css", "text/css", false); });
|
||||||
|
server.on("/gauge.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/gauge.js", "application/javascript", false); });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Configures REST API endpoints for data retrieval.
|
||||||
|
*
|
||||||
|
* Sets up endpoints to fetch sensor data, water data, telemetry,
|
||||||
|
* network information, and OTA update status in JSON format.
|
||||||
|
*
|
||||||
|
* @note These endpoints are used by the frontend to dynamically
|
||||||
|
* display device status and configuration.
|
||||||
|
*/
|
||||||
|
void setup_api_endpoints(){
|
||||||
|
server.on("/sensor_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
String output;
|
||||||
|
serializeJson(build_shunt_data_json(shunt_data), output);
|
||||||
|
request->send(200, "application/json", output); });
|
||||||
|
|
||||||
|
server.on("/water_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
rwLockAcquireRead(&waterDataLock);
|
||||||
|
String output;
|
||||||
|
serializeJson(build_water_data_json(local_water_data), output);
|
||||||
|
rwLockReleaseRead(&waterDataLock);
|
||||||
|
request->send(200, "application/json", output);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("/raw_percent", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
rwLockAcquireRead(&waterDataLock);
|
||||||
|
String output = String(local_water_data.percentage);
|
||||||
|
rwLockReleaseRead(&waterDataLock);
|
||||||
|
request->send(200, "text/raw", output);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("/raw_level", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
rwLockAcquireRead(&waterDataLock);
|
||||||
|
String output = String(local_water_data.level);
|
||||||
|
rwLockReleaseRead(&waterDataLock);
|
||||||
|
request->send(200, "text/raw", output);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("/telemetry", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
String output;
|
||||||
|
serializeJson(build_telemetry_json(telemetry), output);
|
||||||
|
request->send(200, "application/json", output); });
|
||||||
|
|
||||||
|
server.on("/network_info", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
String output;
|
||||||
|
serializeJson(build_network_json(ethernet_data, wifi_data), output);
|
||||||
|
request->send(200, "application/json", output);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("/ota_update_status", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
String output;
|
||||||
|
serializeJson(build_ota_json(ota_status), output);
|
||||||
|
request->send(200, "application/json", output);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("/run_ota_update", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
if (ota_status.update_progress > -1) {
|
||||||
|
request->send(200, "text/plain", "OTA Update already in progress");
|
||||||
|
return;
|
||||||
|
} else if (!ota_status.update_available) {
|
||||||
|
request->send(200, "text/plain", "No update available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
static TaskArgs_t args = {
|
||||||
|
.ota_status = ota_status
|
||||||
|
};
|
||||||
|
|
||||||
|
xTaskCreate(run_ota_update_task, "RunOTAUpdate", 1024 * 8, (void *)&args, 1, NULL);
|
||||||
|
request->send(LittleFS, "/update_progress.html", "text/html", false, processor);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ======================
|
||||||
|
// Helper Functions
|
||||||
|
// ======================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles the update of WiFi credentials.
|
||||||
|
*
|
||||||
|
* Validates and updates the SSID and password in preferences.
|
||||||
|
*
|
||||||
|
* @param request The web server request object.
|
||||||
|
*/
|
||||||
|
void handle_update_wifi_credentials(AsyncWebServerRequest* request) {
|
||||||
|
if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) {
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "Updating SSID config");
|
||||||
|
const AsyncWebParameter* ssid_param = request->getParam(ssid_key, true);
|
||||||
|
const AsyncWebParameter* password_param = request->getParam(wifi_password_key, true);
|
||||||
|
prefs.putString(ssid_key, ssid_param->value().c_str());
|
||||||
|
prefs.putString(wifi_password_key, password_param->value().c_str());
|
||||||
|
} else {
|
||||||
|
request->send(400, "text/plain", "Missing parameters");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
request->send(LittleFS, "/settings.html", "text/html", false, processor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles the update of sensor settings.
|
||||||
|
*
|
||||||
|
* Validates and updates sensor configuration parameters in preferences.
|
||||||
|
*
|
||||||
|
* @param request The web server request object.
|
||||||
|
*/
|
||||||
|
void handle_update_sensor_settings(AsyncWebServerRequest* request) {
|
||||||
|
int params = request->params();
|
||||||
|
|
||||||
|
if (request->hasParam(level_sensor_range_key, true) && request->hasParam(water_level_min_key, true) &&
|
||||||
|
request->hasParam(water_level_max_key, true) && request->hasParam(water_volume_key, true)) {
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "Updating Sensor config");
|
||||||
|
const AsyncWebParameter* range_param = request->getParam(level_sensor_range_key, true);
|
||||||
|
const AsyncWebParameter* level_min_param = request->getParam(water_level_min_key, true);
|
||||||
|
const AsyncWebParameter* level_max_param = request->getParam(water_level_max_key, true);
|
||||||
|
const AsyncWebParameter* liters_param = request->getParam(water_volume_key, true);
|
||||||
|
|
||||||
|
String range_str = range_param->value();
|
||||||
|
String level_min_str = level_min_param->value();
|
||||||
|
String level_max_str = level_max_param->value();
|
||||||
|
String liters_str = liters_param->value();
|
||||||
|
|
||||||
|
float range_float = range_str.toFloat();
|
||||||
|
float level_min_float = level_min_str.toFloat();
|
||||||
|
float level_max_float = level_max_str.toFloat();
|
||||||
|
float liters_float = liters_str.toFloat();
|
||||||
|
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "range_float:%D:", range_float);
|
||||||
|
|
||||||
|
prefs.putFloat(level_sensor_range_key, range_float);
|
||||||
|
prefs.putFloat(water_level_min_key, level_min_float);
|
||||||
|
prefs.putFloat(water_level_max_key, level_max_float);
|
||||||
|
prefs.putFloat(water_volume_key, liters_float);
|
||||||
|
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0));
|
||||||
|
} else {
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "!!!! FAIL lo");
|
||||||
|
for (int i = 0; i < params; i++) {
|
||||||
|
const AsyncWebParameter* p = request->getParam(i);
|
||||||
|
if (p->isFile()) {
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
||||||
|
} else if (p->isPost()) {
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
||||||
|
} else {
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
request->send(400, "text/plain", "Missing parameters");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
request->send(LittleFS, "/settings.html", "text/html", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main task for the webserver.
|
||||||
|
*
|
||||||
|
* Initializes all routes, starts the webserver, and sets up local water data.
|
||||||
|
* Receives water data from the sensor task via a queue and updates local_water_data.
|
||||||
|
* Runs indefinitely to keep the server active.
|
||||||
|
*
|
||||||
|
* @param pvParameters Task parameters (expected to be a QueueHandle_t for the water data queue).
|
||||||
|
*/
|
||||||
|
void webserver_task(void *pvParameters) {
|
||||||
|
// Extract the queue handle from the task parameters
|
||||||
|
webserverWaterDataQueue = (QueueHandle_t)pvParameters;
|
||||||
|
if (webserverWaterDataQueue == NULL) {
|
||||||
|
LOG(ELOG_LEVEL_ERROR, "Webserver water data queue is NULL");
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "Setting up routes");
|
||||||
|
setup_routes();
|
||||||
|
|
||||||
|
// Initialize the readers-writer lock
|
||||||
|
if (!rwLockInit(&waterDataLock)) {
|
||||||
|
LOG(ELOG_LEVEL_ERROR, "Failed to initialize readers-writer lock");
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize local water data with static values
|
||||||
|
rwLockAcquireWrite(&waterDataLock);
|
||||||
|
local_water_data.level = 50.0f;
|
||||||
|
local_water_data.liters = 100.0f;
|
||||||
|
local_water_data.percentage = 50.0f;
|
||||||
|
rwLockReleaseWrite(&waterDataLock);
|
||||||
|
|
||||||
|
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
||||||
|
server.begin();
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
// Check if there is new water data in the queue
|
||||||
|
WaterData newWaterData;
|
||||||
|
if (xQueueReceive(webserverWaterDataQueue, &newWaterData, 0) == pdTRUE) {
|
||||||
|
// Update local_water_data with the new data from the queue
|
||||||
|
rwLockAcquireWrite(&waterDataLock);
|
||||||
|
local_water_data = newWaterData;
|
||||||
|
rwLockReleaseWrite(&waterDataLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS); // Small delay to reduce CPU usage
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/networking/webserver.h
Normal file
16
src/networking/webserver.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef ASYNC_WEBSERVER_H
|
||||||
|
#define ASYNC_WEBSERVER_H
|
||||||
|
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
#include <LittleFS.h>
|
||||||
|
|
||||||
|
extern AsyncWebServer server;
|
||||||
|
|
||||||
|
void setup_api_endpoints();
|
||||||
|
void setup_routes();
|
||||||
|
void webserver_task(void *pvParameters);
|
||||||
|
void handle_update_wifi_credentials(AsyncWebServerRequest* request);
|
||||||
|
void handle_update_sensor_settings(AsyncWebServerRequest* request);
|
||||||
|
|
||||||
|
#endif // ASYNC_WEBSERVER_H
|
||||||
|
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <Elog.h>
|
#include <Elog.h>
|
||||||
#include "Wire.h"
|
#include "Wire.h"
|
||||||
#include "../global_data/global_data.h"
|
#include "../global_data/global_data.h"
|
||||||
|
#include "../networking/webserver.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_INA226
|
#ifdef USE_INA226
|
||||||
@@ -33,6 +34,8 @@ void init_sensor(){
|
|||||||
ina_sensor.setBusVoltageConversionTime(7);
|
ina_sensor.setBusVoltageConversionTime(7);
|
||||||
ina_sensor.setShuntVoltageConversionTime(7);
|
ina_sensor.setShuntVoltageConversionTime(7);
|
||||||
ina_sensor.setAverage(4);
|
ina_sensor.setAverage(4);
|
||||||
|
// 111 - Shunt + Bus continous
|
||||||
|
ina_sensor.setMode(7);
|
||||||
#else
|
#else
|
||||||
ina_sensor.begin(33, 32);
|
ina_sensor.begin(33, 32);
|
||||||
ina_sensor.reset();
|
ina_sensor.reset();
|
||||||
@@ -45,6 +48,14 @@ void init_sensor(){
|
|||||||
|
|
||||||
void read_sensor_task(void* parameter)
|
void read_sensor_task(void* parameter)
|
||||||
{
|
{
|
||||||
|
// Extract the queue handle from the task parameters
|
||||||
|
QueueHandle_t webserverWaterDataQueue = (QueueHandle_t)parameter;
|
||||||
|
if (webserverWaterDataQueue == NULL) {
|
||||||
|
LOG(ELOG_LEVEL_ERROR, "Webserver water data queue is NULL");
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Starting read sensor tasks");
|
LOG(ELOG_LEVEL_DEBUG, "Starting read sensor tasks");
|
||||||
while (true) {
|
while (true) {
|
||||||
// Get Values from sensor
|
// Get Values from sensor
|
||||||
@@ -52,7 +63,6 @@ void read_sensor_task(void* parameter)
|
|||||||
String chip_id = ina_sensor.get_device_model();
|
String chip_id = ina_sensor.get_device_model();
|
||||||
LOG(ELOG_LEVEL_DEBUG, "Chip Model: %s", chip_id.c_str());
|
LOG(ELOG_LEVEL_DEBUG, "Chip Model: %s", chip_id.c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
float bus_voltage = ina_sensor.getBusVoltage();
|
float bus_voltage = ina_sensor.getBusVoltage();
|
||||||
float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value;
|
float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value;
|
||||||
|
|
||||||
@@ -110,6 +120,11 @@ void read_sensor_task(void* parameter)
|
|||||||
water_data.liters = liters;
|
water_data.liters = liters;
|
||||||
water_data.percentage = percentage_rounded;
|
water_data.percentage = percentage_rounded;
|
||||||
|
|
||||||
|
// Send the water data to the webserver task via the queue
|
||||||
|
if (xQueueSend(webserverWaterDataQueue, &water_data, 0) != pdTRUE) {
|
||||||
|
LOG(ELOG_LEVEL_ERROR, "Failed to send water data to webserver queue");
|
||||||
|
}
|
||||||
|
|
||||||
delay(20000);
|
delay(20000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
70
src/tools/readers_writer_lock.cpp
Normal file
70
src/tools/readers_writer_lock.cpp
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#include "readers_writer_lock.h"
|
||||||
|
#include <freertos/task.h>
|
||||||
|
|
||||||
|
bool rwLockInit(ReadersWriterLock* rwLock) {
|
||||||
|
if (rwLock == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
rwLock->mutex = xSemaphoreCreateMutex();
|
||||||
|
if (rwLock->mutex == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
rwLock->readersCount = 0;
|
||||||
|
rwLock->writerActive = false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rwLockAcquireRead(ReadersWriterLock* rwLock) {
|
||||||
|
if (rwLock == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xSemaphoreTake(rwLock->mutex, portMAX_DELAY);
|
||||||
|
// Wait until no writer is active
|
||||||
|
while (rwLock->writerActive) {
|
||||||
|
xSemaphoreGive(rwLock->mutex);
|
||||||
|
vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to avoid busy-waiting
|
||||||
|
xSemaphoreTake(rwLock->mutex, portMAX_DELAY);
|
||||||
|
}
|
||||||
|
rwLock->readersCount++;
|
||||||
|
xSemaphoreGive(rwLock->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rwLockReleaseRead(ReadersWriterLock* rwLock) {
|
||||||
|
if (rwLock == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xSemaphoreTake(rwLock->mutex, portMAX_DELAY);
|
||||||
|
rwLock->readersCount--;
|
||||||
|
xSemaphoreGive(rwLock->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rwLockAcquireWrite(ReadersWriterLock* rwLock) {
|
||||||
|
if (rwLock == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xSemaphoreTake(rwLock->mutex, portMAX_DELAY);
|
||||||
|
// Set the writer active flag to block new readers
|
||||||
|
rwLock->writerActive = true;
|
||||||
|
// Wait until no readers are active
|
||||||
|
while (rwLock->readersCount > 0) {
|
||||||
|
xSemaphoreGive(rwLock->mutex);
|
||||||
|
vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to avoid busy-waiting
|
||||||
|
xSemaphoreTake(rwLock->mutex, portMAX_DELAY);
|
||||||
|
}
|
||||||
|
// Writer now has exclusive access
|
||||||
|
}
|
||||||
|
|
||||||
|
void rwLockReleaseWrite(ReadersWriterLock* rwLock) {
|
||||||
|
if (rwLock == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
rwLock->writerActive = false;
|
||||||
|
xSemaphoreGive(rwLock->mutex);
|
||||||
|
}
|
||||||
65
src/tools/readers_writer_lock.h
Normal file
65
src/tools/readers_writer_lock.h
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#ifndef READERS_WRITER_LOCK_H
|
||||||
|
#define READERS_WRITER_LOCK_H
|
||||||
|
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/semphr.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Readers-Writer Lock structure.
|
||||||
|
*
|
||||||
|
* This structure holds the state for a readers-writer lock,
|
||||||
|
* allowing multiple readers or a single writer to access a shared resource.
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
SemaphoreHandle_t mutex; /**< Mutex to protect the lock state. */
|
||||||
|
int readersCount; /**< Number of active readers. */
|
||||||
|
bool writerActive; /**< Flag to indicate if a writer is active. */
|
||||||
|
} ReadersWriterLock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize a readers-writer lock.
|
||||||
|
*
|
||||||
|
* Initializes the mutex and sets the initial state of the lock.
|
||||||
|
*
|
||||||
|
* @param rwLock Pointer to the ReadersWriterLock structure to initialize.
|
||||||
|
* @return true if initialization succeeded, false otherwise.
|
||||||
|
*/
|
||||||
|
bool rwLockInit(ReadersWriterLock* rwLock);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Acquire a read lock.
|
||||||
|
*
|
||||||
|
* Blocks if a writer is active, otherwise allows multiple readers to access the data simultaneously.
|
||||||
|
*
|
||||||
|
* @param rwLock Pointer to the ReadersWriterLock structure.
|
||||||
|
*/
|
||||||
|
void rwLockAcquireRead(ReadersWriterLock* rwLock);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Release a read lock.
|
||||||
|
*
|
||||||
|
* Decrements the readers count.
|
||||||
|
*
|
||||||
|
* @param rwLock Pointer to the ReadersWriterLock structure.
|
||||||
|
*/
|
||||||
|
void rwLockReleaseRead(ReadersWriterLock* rwLock);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Acquire a write lock.
|
||||||
|
*
|
||||||
|
* Blocks until all readers have finished and sets the writer active flag.
|
||||||
|
*
|
||||||
|
* @param rwLock Pointer to the ReadersWriterLock structure.
|
||||||
|
*/
|
||||||
|
void rwLockAcquireWrite(ReadersWriterLock* rwLock);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Release a write lock.
|
||||||
|
*
|
||||||
|
* Clears the writer active flag and allows readers and writers.
|
||||||
|
*
|
||||||
|
* @param rwLock Pointer to the ReadersWriterLock structure.
|
||||||
|
*/
|
||||||
|
void rwLockReleaseWrite(ReadersWriterLock* rwLock);
|
||||||
|
|
||||||
|
#endif // READERS_WRITER_LOCK_H
|
||||||
@@ -49,9 +49,11 @@ void update_started() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void update_finished() {
|
void update_finished() {
|
||||||
LOG(ELOG_LEVEL_DEBUG, "OTA Update finished");
|
LOG(ELOG_LEVEL_DEBUG, "OTA Update finished, rebooting after 2 seconds");
|
||||||
ota_status.update_progress = -1;
|
ota_status.update_progress = -1;
|
||||||
webSocket.textAll(String(-1).c_str());
|
webSocket.textAll(String(-1).c_str());
|
||||||
|
delay(2000);
|
||||||
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_progress(int cur, int total) {
|
void update_progress(int cur, int total) {
|
||||||
@@ -102,7 +104,6 @@ void check_and_update_littleFS(OTA littlefs) {
|
|||||||
if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION) && isVersionNewer(current_spiffs_version, latest_fs_version.version)) {
|
if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION) && isVersionNewer(current_spiffs_version, latest_fs_version.version)) {
|
||||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now");
|
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now");
|
||||||
run_ota_spiffs_update(latest_fs_version.url, update_started, update_finished, update_progress, update_error);
|
run_ota_spiffs_update(latest_fs_version.url, update_started, update_finished, update_progress, update_error);
|
||||||
ESP.restart();
|
|
||||||
|
|
||||||
// If we do not need a new version but one is available on the server
|
// If we do not need a new version but one is available on the server
|
||||||
} else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_fs_version.version)) {
|
} else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_fs_version.version)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user