6 Commits
vibe ... rev2

Author SHA1 Message Date
d337784faa Moved a lot of global vars to queues
All checks were successful
Test project compilation / test (push) Successful in 3m52s
2026-04-01 20:11:53 +02:00
f1d9abc4c7 Switched to queues for webserver
All checks were successful
Test project compilation / test (push) Successful in 3m44s
2026-03-30 18:34:14 +02:00
752cd86234 refractor 2026-03-30 17:20:06 +02:00
e5fecde7f5 Factored out webserver stuff to own task/file 2026-03-30 17:08:36 +02:00
cfc8438042 Cleanup + Bugfix
All checks were successful
Test project compilation / test (push) Successful in 3m43s
2026-03-29 17:46:41 +02:00
aa69687ee8 comment
All checks were successful
Test project compilation / test (push) Successful in 3m56s
2026-03-26 17:06:02 +01:00
24 changed files with 953 additions and 335 deletions

View File

@@ -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"

View File

@@ -1 +1 @@
9 10

View File

@@ -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:

View File

@@ -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.21
build_flags = ${env:esp32_base.build_flags} -DUSE_INA226 build_flags = ${env:esp32_base.build_flags} -DUSE_INA226
[env:native] [env:native]

View File

@@ -4,22 +4,20 @@
#include "../tools/tools.h" #include "../tools/tools.h"
#include <tools/log.h> #include <tools/log.h>
extern ActiveErrors active_errors; // TODO: Rewrite so that this task does not do anything smart, just receives data over queue and displays it
extern WaterData water_data; // So it just receives: OK, Error, error code X, warning, warning X (blink)
void display_task(void* parameter) void display_task(void* parameter)
{ {
LOG(ELOG_LEVEL_DEBUG, "Starting display tasks"); LOG(ELOG_LEVEL_DEBUG, "Starting LED tasks");
led_setup();
while (true) { while (true) {
if (!is_error(active_errors)) { ledcWrite(LED_RED, 0);
// We have no error, refresh status display and wait half a second ledcWrite(LED_GREEN, 255);
ledcWrite(LED_GREEN, 255); delay(500);
ledcWrite(LED_RED, 0); ledcWrite(LED_RED, LED_RED_HIGH);
} else { ledcWrite(LED_GREEN, 0);
ledcWrite(LED_RED, LED_RED_HIGH); delay(500);
ledcWrite(LED_GREEN, 0);
}
delay(250);
} }
} }

View File

@@ -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

View File

@@ -1,12 +1,3 @@
#include "global_data.h" #include "global_data.h"
NetworkData wifi_data;
NetworkData ethernet_data;
DeviceTelemetry telemetry;
SensorData shunt_data;
WaterData water_data;
OTAStatus ota_status; OTAStatus ota_status;
ActiveErrors active_errors = { false, false, false, false, false, false };

View File

@@ -3,48 +3,112 @@
#pragma once #pragma once
/**
* @brief Structure to hold sensor data.
*
* Contains voltage and current measurements from the sensor.
*/
struct SensorData { struct SensorData {
float bus_voltage; float bus_voltage; /**< Bus voltage in volts */
float shunt_voltage; float shunt_voltage; /**< Shunt voltage in millivolts */
float shunt_current; float shunt_current; /**< Shunt current in milliamps */
}; };
/**
* @brief Structure to hold water data.
*
* Contains water level, volume, and percentage measurements.
*/
struct WaterData{ struct WaterData{
// Water level in cm float level; /**< Water level in cm */
float level; float liters; /**< Water volume in liters */
// Water volume in liters float percentage; /**< Water level as a percentage */
float liters;
// Percentage
float percentage;
}; };
/**
* @brief Structure to hold network data.
*
* Contains network-related information such as IP address and signal strength.
*/
struct NetworkData { struct NetworkData {
String ip_address; char ip_address[16]; /**< IP address of the device (max 15 chars + null terminator) */
bool link; bool link; /**< Whether the network link is active */
float rssi; float rssi; /**< Received signal strength indicator */
String network_name; char network_name[33]; /**< Name of the network (max 32 chars + null terminator) */
}; };
/**
* @brief Structure to hold device telemetry data.
*
* Contains information about the device's internal state.
*/
struct DeviceTelemetry { struct DeviceTelemetry {
float heap_used_percent; float heap_used_percent; /**< Percentage of heap memory used */
int uptime_seconds; int uptime_seconds; /**< Uptime of the device in seconds */
float temperature; float temperature; /**< Temperature of the device in Celsius */
}; };
/**
* @brief Structure to hold active error flags.
*
* Contains boolean flags indicating various error conditions.
*/
struct ActiveErrors { struct ActiveErrors {
bool voltage_low; bool voltage_low; /**< Indicates if the voltage is too low */
bool voltage_high; bool voltage_high; /**< Indicates if the voltage is too high */
bool current_low; bool current_low; /**< Indicates if the current is too low */
bool current_high; bool current_high; /**< Indicates if the current is too high */
bool level_low; bool level_low; /**< Indicates if the water level is too low */
bool level_high; bool level_high; /**< Indicates if the water level is too high */
}; };
/**
* @brief Structure to hold OTA (Over-The-Air) update status.
*
* Contains information about the OTA update process.
*/
struct OTAStatus { struct OTAStatus {
bool update_available; bool update_available; /**< Indicates if an update is available */
Version current_version; Version current_version; /**< Current version of the firmware */
Version latest_version; Version latest_version; /**< Latest available version of the firmware */
int update_progress; int update_progress; /**< Progress of the update as a percentage */
String update_url; String update_url; /**< URL to download the update */
}; };
/**
* @brief Structure to hold the current state of the system.
*
* Contains all data structs to represent the current state of the system.
*/
typedef struct {
WaterData waterData; /**< Latest water data */
SensorData sensorData; /**< Latest sensor data */
NetworkData wifiData; /**< Latest WiFi network data */
NetworkData ethernetData; /**< Latest Ethernet network data */
DeviceTelemetry telemetryData; /**< Latest telemetry data */
OTAStatus otaStatus; /**< Latest OTA status */
ActiveErrors activeErrors; /**< Latest active errors */
} CurrentState;
// Enum to represent different types of data that can be sent between tasks
typedef enum {
DATA_TYPE_SENSOR,
DATA_TYPE_WATER,
DATA_TYPE_WIFI,
DATA_TYPE_ETHERNET,
DATA_TYPE_TELEMETRY
} DataType;
// Union to hold different types of data
typedef union {
SensorData sensorData;
WaterData waterData;
NetworkData networkData;
DeviceTelemetry telemetryData;
} DataUnion;
// Structure to hold data type and the actual data
typedef struct {
DataType type;
DataUnion data;
} DataMessage;

View File

@@ -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>
@@ -13,8 +12,10 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "global_data/defines.h" #include "global_data/defines.h"
#include "global_data/global_data.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,36 +23,44 @@
#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"
#include "tools/ota_handler.h"
#include <LittleFS.h> #include <LittleFS.h>
#include "esp_heap_caps.h"
#define MYLOG 0 #define MYLOG 0
Preferences prefs; Preferences prefs;
extern WaterData water_data; extern AsyncWebSocket webSocket;
extern DeviceTelemetry telemetry;
extern NetworkData wifi_data;
extern NetworkData ethernet_data;
extern SensorData shunt_data;
extern ActiveErrors active_errors;
Version current_spiffs_version; Version current_spiffs_version;
AsyncWebServer server(80); // Variable to store the current state of the system
AsyncWebSocket webSocket("/webSocket"); CurrentState current_state = {
{-999.0f, -999.0f, -999.0f},
{-999.0f, -999.0f, -999.0f},
{"0.0.0.0", false, -999.0f, "UNKNOWN"}, // wifiData
{"0.0.0.0", false, -999.0f, "UNKNOWN"}, // ethernetData
{-999.0f, -999, -999.0f},
{false, {0, 0, 0}, {0, 0, 0}, -999, "UNKNOWN"},
{false, false, false, false, false, false}
};
#define FORMAT_LITTLEFS_IF_FAILED true #define FORMAT_LITTLEFS_IF_FAILED true
// Queue for sending data from producers to the processor in main
QueueHandle_t dataQueue = xQueueCreate(5, sizeof(DataMessage));
// Queue for sending data from processor to different tasks
QueueHandle_t stateForWebserverQueue = xQueueCreate(2, sizeof(CurrentState));
void setup() void setup()
{ {
Logger.registerSerial(MYLOG, ELOG_LEVEL_DEBUG, "Serial"); Logger.registerSerial(MYLOG, ELOG_LEVEL_DEBUG, "Serial");
LOG(ELOG_LEVEL_DEBUG, "Init LEDs");
led_setup();
LOG(ELOG_LEVEL_DEBUG, "Init Starting prefs and Serial output"); LOG(ELOG_LEVEL_DEBUG, "Init Starting prefs and Serial output");
prefs.begin("waterlevel", false); prefs.begin("waterlevel", false);
Serial.begin(115200); Serial.begin(115200);
@@ -74,132 +83,75 @@ void setup()
LOG(ELOG_LEVEL_DEBUG, "Current LittleFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch); LOG(ELOG_LEVEL_DEBUG, "Current LittleFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch);
} }
LOG(ELOG_LEVEL_DEBUG, "LittleFS initialized"); LOG(ELOG_LEVEL_DEBUG, "LittleFS initialized");
/////////////////////////////// ROUTES ///////////////////////////////
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");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
LOG(ELOG_LEVEL_DEBUG, "Start updating %s", type); })
.onEnd([]() { LOG(ELOG_LEVEL_DEBUG, "\nEnd"); })
.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) LOG(ELOG_LEVEL_DEBUG, "Auth Failed");
else if (error == OTA_BEGIN_ERROR) LOG(ELOG_LEVEL_DEBUG, "Begin Failed");
else if (error == OTA_CONNECT_ERROR) LOG(ELOG_LEVEL_DEBUG, "Connect Failed");
else if (error == OTA_RECEIVE_ERROR) LOG(ELOG_LEVEL_DEBUG, "Receive Failed");
else if (error == OTA_END_ERROR) LOG(ELOG_LEVEL_DEBUG, "End Failed"); });
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);
xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, NULL, 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);
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
LOG(ELOG_LEVEL_DEBUG, "Starting webserver"); if (dataQueue == NULL) {
server.begin(); LOG(ELOG_LEVEL_ERROR, "Failed to create data queue");
LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler"); } else {
ArduinoOTA.begin(); xTaskCreate(ethernet_task, "EthernetTask", 1024 * 4, dataQueue, 1, NULL);
xTaskCreate(wifi_task, "WiFiTask", 1024 * 4, dataQueue, 1, NULL);
xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, dataQueue, 1, NULL);
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 1024 * 2, dataQueue, 1, NULL);
xTaskCreate(display_task, "DisplayTask", 1024 * 2, NULL, 1, NULL);
xTaskCreate(get_time_task, "GetTimeTask", 1024 * 2, NULL, 1, NULL);
delay(5000);
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 6, NULL, 1, NULL);
xTaskCreate(webserver_task, "WebServerTask", 1024 * 4, stateForWebserverQueue, 1, NULL);
xTaskCreate(ota_handler_task, "OTAHandlerTask", 1024 * 4, NULL, 3, NULL);
}
} }
void loop() void loop()
{ {
ArduinoOTA.handle(); size_t free_heap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
Serial.println(free_heap);
// Check if there is new data in the queue
DataMessage dataMessage;
if (xQueueReceive(dataQueue, &dataMessage, portMAX_DELAY) == pdTRUE) {
// Decode the data based on its type and update the current state
switch (dataMessage.type) {
case DATA_TYPE_WATER:
current_state.waterData = dataMessage.data.waterData;
LOG(ELOG_LEVEL_DEBUG, "Received water data: level=%F, liters=%F, percentage=%F",
current_state.waterData.level, current_state.waterData.liters, current_state.waterData.percentage);
break;
case DATA_TYPE_SENSOR:
current_state.sensorData = dataMessage.data.sensorData;
LOG(ELOG_LEVEL_DEBUG, "Received sensor data: bus_voltage=%F, shunt_voltage=%F, shunt_current=%F",
current_state.sensorData.bus_voltage, current_state.sensorData.shunt_voltage, current_state.sensorData.shunt_current);
break;
case DATA_TYPE_WIFI:
current_state.wifiData = dataMessage.data.networkData;
LOG(ELOG_LEVEL_DEBUG, "Received WiFi data: ip=%s, link=%d, rssi=%F, name=%s",
current_state.wifiData.ip_address, current_state.wifiData.link,
current_state.wifiData.rssi, current_state.wifiData.network_name);
break;
case DATA_TYPE_ETHERNET:
current_state.ethernetData = dataMessage.data.networkData;
LOG(ELOG_LEVEL_DEBUG, "Received Ethernet data: ip=%s, link=%d, rssi=%F, name=%s",
current_state.ethernetData.ip_address, current_state.ethernetData.link,
current_state.ethernetData.rssi, current_state.ethernetData.network_name);
break;
case DATA_TYPE_TELEMETRY:
current_state.telemetryData = dataMessage.data.telemetryData;
LOG(ELOG_LEVEL_DEBUG, "Received telemetry data: heap=%F, uptime=%d, temperature=%F",
current_state.telemetryData.heap_used_percent, current_state.telemetryData.uptime_seconds,
current_state.telemetryData.temperature);
break;
default:
LOG(ELOG_LEVEL_ERROR, "Unknown data type received");
break;
}
// Send the updated current_state to the webserver queue
if (xQueueSendToBack(stateForWebserverQueue, &current_state, 250 / portTICK_PERIOD_MS) != pdTRUE) {
LOG(ELOG_LEVEL_ERROR, "Failed to send current_state to webserver queue");
}
} else {
LOG(ELOG_LEVEL_WARNING, "No message received within max wait");
}
delay(1000); delay(1000);
} }

View File

@@ -1,38 +1,34 @@
#include "json_builder.h" #include "json_builder.h"
#include <Elog.h> #include <Elog.h>
extern WaterData water_data; // Refactored: no return by value, serialize directly to output String
extern DeviceTelemetry telemetry; // Reduced document sizes based on actual JSON output requirements
extern NetworkData wifi_data; void build_shunt_data_json(SensorData data, String& output) {
extern NetworkData ethernet_data; StaticJsonDocument<96> doc; // Reduced from 128: 3 floats ~70-80 bytes
extern SensorData shunt_data;
StaticJsonDocument<128> build_shunt_data_json(SensorData data) {
StaticJsonDocument<128> doc;
doc["bus_voltage"] = data.bus_voltage; doc["bus_voltage"] = data.bus_voltage;
doc["shunt_voltage"] = data.shunt_voltage; doc["shunt_voltage"] = data.shunt_voltage;
doc["current"] = data.shunt_current; doc["current"] = data.shunt_current;
return doc; serializeJson(doc, output);
} }
StaticJsonDocument<128> build_water_data_json(WaterData data) { void build_water_data_json(WaterData data, String& output) {
StaticJsonDocument<128> doc; StaticJsonDocument<96> doc; // Reduced from 128: 3 floats ~70-80 bytes
doc["percentage"] = data.percentage; doc["percentage"] = data.percentage;
doc["water_height"] = data.level; doc["water_height"] = data.level;
doc["liters"] = data.liters; doc["liters"] = data.liters;
return doc; serializeJson(doc, output);
} }
StaticJsonDocument<128> build_telemetry_json(DeviceTelemetry data) { void build_telemetry_json(DeviceTelemetry data, String& output) {
StaticJsonDocument<128> doc; StaticJsonDocument<96> doc; // Reduced from 128: 1 int + 2 floats ~70-80 bytes
doc["uptime_seconds"] = data.uptime_seconds; doc["uptime_seconds"] = data.uptime_seconds;
doc["heap_percent"] = data.heap_used_percent; doc["heap_percent"] = data.heap_used_percent;
doc["temperature"] = data.temperature; doc["temperature"] = data.temperature;
return doc; serializeJson(doc, output);
} }
StaticJsonDocument<256> build_network_json(NetworkData wired, NetworkData wireless) { void build_network_json(NetworkData wired, NetworkData wireless, String& output) {
StaticJsonDocument<256> doc; StaticJsonDocument<256> doc; // Keep 256: nested objects + strings ~200 bytes
doc["wifi"]["ip"] = wireless.ip_address; doc["wifi"]["ip"] = wireless.ip_address;
doc["wifi"]["rssi"] = wireless.rssi; doc["wifi"]["rssi"] = wireless.rssi;
doc["wifi"]["link"] = wireless.link; doc["wifi"]["link"] = wireless.link;
@@ -42,15 +38,15 @@ StaticJsonDocument<256> build_network_json(NetworkData wired, NetworkData wirele
doc["ethernet"]["rssi"] = wired.rssi; doc["ethernet"]["rssi"] = wired.rssi;
doc["ethernet"]["link"] = wired.link; doc["ethernet"]["link"] = wired.link;
return doc; serializeJson(doc, output);
} }
StaticJsonDocument<128> build_ota_json(OTAStatus status) { void build_ota_json(OTAStatus status, String& output) {
StaticJsonDocument<256> doc; StaticJsonDocument<192> doc; // Reduced from 256: bool + 2 version strings + int ~150-170 bytes
doc["update_available"] = status.update_available; doc["update_available"] = status.update_available;
doc["current_version"] = String(status.current_version.major) + "." + String(status.current_version.minor) + "." + String(status.current_version.patch); doc["current_version"] = String(status.current_version.major) + "." + String(status.current_version.minor) + "." + String(status.current_version.patch);
doc["new_version"] = String(status.latest_version.major) + "." + String(status.latest_version.minor) + "." + String(status.latest_version.patch); doc["new_version"] = String(status.latest_version.major) + "." + String(status.latest_version.minor) + "." + String(status.latest_version.patch);
doc["progress"] = status.update_progress; doc["progress"] = status.update_progress;
return doc; serializeJson(doc, output);
} }

View File

@@ -1,8 +1,9 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "../global_data/global_data.h" #include "../global_data/global_data.h"
StaticJsonDocument<128> build_shunt_data_json(SensorData data); // Refactored to pass by reference - no expensive copies on stack
StaticJsonDocument<128> build_water_data_json(WaterData data); void build_shunt_data_json(SensorData data, String& output);
StaticJsonDocument<128> build_telemetry_json(DeviceTelemetry data); void build_water_data_json(WaterData data, String& output);
StaticJsonDocument<256> build_network_json(NetworkData wired, NetworkData wireless); void build_telemetry_json(DeviceTelemetry data, String& output);
StaticJsonDocument<128> build_ota_json(OTAStatus status); void build_network_json(NetworkData wired, NetworkData wireless, String& output);
void build_ota_json(OTAStatus status, String& output);

View File

@@ -4,6 +4,7 @@
#include <Preferences.h> #include <Preferences.h>
#include "../global_data/global_data.h" #include "../global_data/global_data.h"
#include <tools/log.h> #include <tools/log.h>
#include "freertos/queue.h"
#define ETH_PHY_TYPE ETH_PHY_LAN8720 #define ETH_PHY_TYPE ETH_PHY_LAN8720
#define ETH_PHY_ADDR 0 #define ETH_PHY_ADDR 0
@@ -18,8 +19,9 @@
int64_t mac_address = ESP.getEfuseMac(); int64_t mac_address = ESP.getEfuseMac();
uint8_t failed_connection_attempts = 0; uint8_t failed_connection_attempts = 0;
extern NetworkData wifi_data; NetworkData wifi_data;
extern NetworkData ethernet_data; NetworkData ethernet_data;
extern Preferences prefs; extern Preferences prefs;
// Defines the type of connection for which the hostname should be created // Defines the type of connection for which the hostname should be created
@@ -45,6 +47,14 @@ const char * get_hostname(HostnameType host_type) {
void wifi_task(void* parameter) void wifi_task(void* parameter)
{ {
// Extract the queue handle from the task parameters
QueueHandle_t dataQueue = (QueueHandle_t)parameter;
if (dataQueue == NULL) {
LOG(ELOG_LEVEL_ERROR, "Data queue is NULL");
vTaskDelete(NULL);
return;
}
LOG(ELOG_LEVEL_DEBUG, "Starting WiFi Task"); LOG(ELOG_LEVEL_DEBUG, "Starting WiFi Task");
WiFi.setHostname(get_hostname(Wireless)); WiFi.setHostname(get_hostname(Wireless));
while (true) { while (true) {
@@ -72,8 +82,21 @@ void wifi_task(void* parameter)
failed_connection_attempts = 0; failed_connection_attempts = 0;
wifi_data.rssi = WiFi.RSSI(); wifi_data.rssi = WiFi.RSSI();
wifi_data.link = true; wifi_data.link = true;
wifi_data.network_name = WiFi.SSID(); strncpy(wifi_data.network_name, WiFi.SSID().c_str(), sizeof(wifi_data.network_name) - 1);
wifi_data.ip_address = WiFi.localIP().toString(); wifi_data.network_name[sizeof(wifi_data.network_name) - 1] = '\0';
strncpy(wifi_data.ip_address, WiFi.localIP().toString().c_str(), sizeof(wifi_data.ip_address) - 1);
wifi_data.ip_address[sizeof(wifi_data.ip_address) - 1] = '\0';
// Create a DataMessage for WiFi data
DataMessage dataMessage;
dataMessage.type = DATA_TYPE_WIFI;
dataMessage.data.networkData = wifi_data;
// Send the WiFi data to the queue
if (xQueueSend(dataQueue, &dataMessage, 0) != pdTRUE) {
LOG(ELOG_LEVEL_ERROR, "Failed to send WiFi data to queue");
}
LOG(ELOG_LEVEL_DEBUG, "WIFI connected; RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID")); LOG(ELOG_LEVEL_DEBUG, "WIFI connected; RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID"));
delay(1000 * 60); delay(1000 * 60);
} else { } else {
@@ -92,13 +115,33 @@ void wifi_task(void* parameter)
void ethernet_task(void* parameter) void ethernet_task(void* parameter)
{ {
// Extract the queue handle from the task parameters
QueueHandle_t dataQueue = (QueueHandle_t)parameter;
if (dataQueue == NULL) {
LOG(ELOG_LEVEL_ERROR, "Data queue is NULL");
vTaskDelete(NULL);
return;
}
LOG(ELOG_LEVEL_DEBUG, "Starting Ethernet Task"); LOG(ELOG_LEVEL_DEBUG, "Starting Ethernet Task");
ETH.begin(); ETH.begin();
ETH.setHostname(get_hostname(Ethernet)); ETH.setHostname(get_hostname(Ethernet));
while (true) { while (true) {
ethernet_data.link = ETH.linkUp(); ethernet_data.link = ETH.linkUp();
ethernet_data.rssi = ETH.linkSpeed(); ethernet_data.rssi = ETH.linkSpeed();
ethernet_data.ip_address = ETH.localIP().toString(); strncpy(ethernet_data.ip_address, ETH.localIP().toString().c_str(), sizeof(ethernet_data.ip_address) - 1);
ethernet_data.ip_address[sizeof(ethernet_data.ip_address) - 1] = '\0';
// Create a DataMessage for Ethernet data
DataMessage dataMessage;
dataMessage.type = DATA_TYPE_ETHERNET;
dataMessage.data.networkData = ethernet_data;
// Send the Ethernet data to the queue
if (xQueueSend(dataQueue, &dataMessage, 0) != pdTRUE) {
LOG(ELOG_LEVEL_ERROR, "Failed to send Ethernet data to queue");
}
LOG(ELOG_LEVEL_DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ETH.localIP().toString(), String(ethernet_data.link)); LOG(ELOG_LEVEL_DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ETH.localIP().toString(), String(ethernet_data.link));
if (ETH.linkUp() && !ETH.isDefault() && ETH.localIP().toString() != "0.0.0.0") { if (ETH.linkUp() && !ETH.isDefault() && ETH.localIP().toString() != "0.0.0.0") {

View File

@@ -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);
});
}

View File

@@ -1,4 +0,0 @@
#include <ESPAsyncWebServer.h>
#include <LittleFS.h>
void setup_api_endpoints();

View File

@@ -0,0 +1,371 @@
#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;
AsyncWebSocket webSocket("/webSocket");
AsyncWebServer server(80);
WaterData local_water_data;
SensorData local_sensor_data;
NetworkData local_wifi_data;
NetworkData local_ethernet_data;
DeviceTelemetry local_telemetry;
OTAStatus local_ota_status;
ReadersWriterLock waterDataLock;
ReadersWriterLock sensorDataLock;
ReadersWriterLock networkDataLock;
ReadersWriterLock telemetryDataLock;
ReadersWriterLock otaStatusLock;
// ======================
// 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) {
rwLockAcquireRead(&sensorDataLock);
String output;
build_shunt_data_json(local_sensor_data, output);
rwLockReleaseRead(&sensorDataLock);
request->send(200, "application/json", output); });
server.on("/water_data", HTTP_GET, [](AsyncWebServerRequest* request) {
rwLockAcquireRead(&waterDataLock);
String output;
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) {
rwLockAcquireRead(&telemetryDataLock);
String output;
build_telemetry_json(local_telemetry, output);
rwLockReleaseRead(&telemetryDataLock);
request->send(200, "application/json", output); });
server.on("/network_info", HTTP_GET, [](AsyncWebServerRequest* request) {
rwLockAcquireRead(&networkDataLock);
String output;
build_network_json(local_ethernet_data, local_wifi_data, output);
rwLockReleaseRead(&networkDataLock);
request->send(200, "application/json", output);
});
server.on("/ota_update_status", HTTP_GET, [](AsyncWebServerRequest* request) {
rwLockAcquireRead(&otaStatusLock);
String output;
build_ota_json(local_ota_status, output);
rwLockReleaseRead(&otaStatusLock);
request->send(200, "application/json", output); });
server.on("/run_ota_update", HTTP_GET, [](AsyncWebServerRequest* request) {
if (local_ota_status.update_progress > -1) {
request->send(200, "text/plain", "OTA Update already in progress");
return;
} else if (!local_ota_status.update_available) {
request->send(200, "text/plain", "No update available");
return;
}
static TaskArgs_t args = {
.ota_status = local_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 the current state from the main 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 state queue).
*/
void webserver_task(void *pvParameters) {
// Extract the queue handle from the task parameters
QueueHandle_t stateQueue = (QueueHandle_t)pvParameters;
if (stateQueue == NULL) {
LOG(ELOG_LEVEL_ERROR, "State queue is NULL");
vTaskDelete(NULL);
return;
}
LOG(ELOG_LEVEL_DEBUG, "Setting up routes");
setup_routes();
// Initialize the readers-writer locks
if (!rwLockInit(&waterDataLock)) {
LOG(ELOG_LEVEL_ERROR, "Failed to initialize water data readers-writer lock");
vTaskDelete(NULL);
return;
}
if (!rwLockInit(&sensorDataLock)) {
LOG(ELOG_LEVEL_ERROR, "Failed to initialize sensor data readers-writer lock");
vTaskDelete(NULL);
return;
}
if (!rwLockInit(&networkDataLock)) {
LOG(ELOG_LEVEL_ERROR, "Failed to initialize network data readers-writer lock");
vTaskDelete(NULL);
return;
}
if (!rwLockInit(&telemetryDataLock)) {
LOG(ELOG_LEVEL_ERROR, "Failed to initialize telemetry data readers-writer lock");
vTaskDelete(NULL);
return;
}
if (!rwLockInit(&otaStatusLock)) {
LOG(ELOG_LEVEL_ERROR, "Failed to initialize OTA status readers-writer lock");
vTaskDelete(NULL);
return;
}
// Initialize local water data with static values
rwLockAcquireWrite(&waterDataLock);
local_water_data.level = -1.0;
local_water_data.liters = -1.0;
local_water_data.percentage = -1.0;
rwLockReleaseWrite(&waterDataLock);
// Initialize local sensor data with static values
rwLockAcquireWrite(&sensorDataLock);
local_sensor_data.bus_voltage = -1.0;
local_sensor_data.shunt_voltage = -1.0;
local_sensor_data.shunt_current = -1.0;
rwLockReleaseWrite(&sensorDataLock);
// Initialize local network data with static values
rwLockAcquireWrite(&networkDataLock);
strcpy(local_wifi_data.ip_address, "0.0.0.0");
local_wifi_data.link = false;
local_wifi_data.rssi = -1.0;
strcpy(local_wifi_data.network_name, "UNKNOWN");
strcpy(local_ethernet_data.ip_address, "0.0.0.0");
local_ethernet_data.link = false;
local_ethernet_data.rssi = -1.0;
strcpy(local_ethernet_data.network_name, "UNKNOWN");
rwLockReleaseWrite(&networkDataLock);
// Initialize local telemetry data with static values
rwLockAcquireWrite(&telemetryDataLock);
local_telemetry.heap_used_percent = -1.0;
local_telemetry.uptime_seconds = -1;
local_telemetry.temperature = -1.0;
rwLockReleaseWrite(&telemetryDataLock);
// Initialize local OTA status with static values
rwLockAcquireWrite(&otaStatusLock);
local_ota_status.update_available = false;
local_ota_status.current_version = {0, 0, 0};
local_ota_status.latest_version = {0, 0, 0};
local_ota_status.update_progress = -1;
local_ota_status.update_url = "UNKNOWN";
rwLockReleaseWrite(&otaStatusLock);
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
server.begin();
while (1) {
// Check if there is new state data in the queue
CurrentState currentState;
if (xQueueReceive(stateQueue, &currentState, portMAX_DELAY) == pdTRUE) {
// Update local_water_data with the new data from the queue
rwLockAcquireWrite(&waterDataLock);
local_water_data = currentState.waterData;
rwLockReleaseWrite(&waterDataLock);
// Update local_sensor_data with the new data from the queue
rwLockAcquireWrite(&sensorDataLock);
local_sensor_data = currentState.sensorData;
rwLockReleaseWrite(&sensorDataLock);
// Update local network data with the new data from the queue
rwLockAcquireWrite(&networkDataLock);
local_wifi_data = currentState.wifiData;
local_ethernet_data = currentState.ethernetData;
rwLockReleaseWrite(&networkDataLock);
// Update local telemetry data with the new data from the queue
rwLockAcquireWrite(&telemetryDataLock);
local_telemetry = currentState.telemetryData;
rwLockReleaseWrite(&telemetryDataLock);
// Update local OTA status with the new data from the queue
rwLockAcquireWrite(&otaStatusLock);
local_ota_status = currentState.otaStatus;
rwLockReleaseWrite(&otaStatusLock);
}
}
}

View 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

View File

@@ -18,9 +18,8 @@ INA233 ina_sensor(0x40);
#include "freertos/task.h" #include "freertos/task.h"
extern Preferences prefs; extern Preferences prefs;
extern WaterData water_data;
extern ActiveErrors active_errors; WaterData water_data;
extern SensorData shunt_data;
// Calibration variables // Calibration variables
float zero_value = 0.03; // Measured shunt voltage with nothing connected, used to fix measuring offset float zero_value = 0.03; // Measured shunt voltage with nothing connected, used to fix measuring offset
@@ -33,6 +32,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 +46,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 dataQueue = (QueueHandle_t)parameter;
if (dataQueue == NULL) {
LOG(ELOG_LEVEL_ERROR, "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 +61,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;
@@ -60,6 +68,22 @@ void read_sensor_task(void* parameter)
float shunt_current = shunt_voltage / RESISTOR_VALUE; float shunt_current = shunt_voltage / RESISTOR_VALUE;
// Build SensorData object
SensorData sensor_data;
sensor_data.bus_voltage = bus_voltage;
sensor_data.shunt_current = shunt_current;
sensor_data.shunt_voltage = shunt_voltage;
// Send the sensor data to the data queue
DataMessage dataMessageSensor;
dataMessageSensor.type = DATA_TYPE_SENSOR;
dataMessageSensor.data.sensorData = sensor_data;
if (xQueueSend(dataQueue, &dataMessageSensor, 0) != pdTRUE) {
LOG(ELOG_LEVEL_ERROR, "Failed to send water data to queue");
}
// Get values from storage // Get values from storage
float sensor_range = prefs.getFloat(level_sensor_range_key, 200); float sensor_range = prefs.getFloat(level_sensor_range_key, 200);
float max_water_level = prefs.getFloat(water_level_max_key, sensor_range); float max_water_level = prefs.getFloat(water_level_max_key, sensor_range);
@@ -93,23 +117,31 @@ void read_sensor_task(void* parameter)
float liters_raw = max_liters * percentage_raw; float liters_raw = max_liters * percentage_raw;
int liters = round(liters_raw); int liters = round(liters_raw);
active_errors.current_low = shunt_current < 3.8;
active_errors.current_high = shunt_current > 20.2;
active_errors.voltage_low = bus_voltage < 23;
active_errors.voltage_high = bus_voltage > 25;
LOG(ELOG_LEVEL_DEBUG, "Shunt current: %F", shunt_current); LOG(ELOG_LEVEL_DEBUG, "Shunt current: %F", shunt_current);
LOG(ELOG_LEVEL_DEBUG, "Shunt voltage: %F", shunt_voltage); LOG(ELOG_LEVEL_DEBUG, "Shunt voltage: %F", shunt_voltage);
LOG(ELOG_LEVEL_DEBUG, "Bus voltage: %F", bus_voltage); LOG(ELOG_LEVEL_DEBUG, "Bus voltage: %F", bus_voltage);
LOG(ELOG_LEVEL_DEBUG, "cm_over_zero: %F", cm_over_zero); LOG(ELOG_LEVEL_DEBUG, "cm_over_zero: %F", cm_over_zero);
shunt_data.bus_voltage = bus_voltage;
shunt_data.shunt_voltage = shunt_voltage;
shunt_data.shunt_current = shunt_current;
water_data.level = cm_over_zero; water_data.level = cm_over_zero;
water_data.liters = liters; water_data.liters = liters;
water_data.percentage = percentage_rounded; water_data.percentage = percentage_rounded;
// Send the water data to the data queue
DataMessage dataMessageWater;
dataMessageWater.type = DATA_TYPE_WATER;
dataMessageWater.data.waterData = water_data;
// Log the data being sent
LOG(ELOG_LEVEL_DEBUG, "Sending water data to queue: level=%F, liters=%F, percentage=%F",
water_data.level, water_data.liters, water_data.percentage);
if (xQueueSend(dataQueue, &dataMessageWater, 0) != pdTRUE) {
LOG(ELOG_LEVEL_ERROR, "Failed to send water data to queue");
// Log the queue status
UBaseType_t messagesWaiting = uxQueueMessagesWaiting(dataQueue);
LOG(ELOG_LEVEL_ERROR, "Queue messages waiting: %d", messagesWaiting);
}
delay(20000); delay(20000);
} }
} }

View File

@@ -2,21 +2,38 @@
#include <Arduino.h> #include <Arduino.h>
#include "../global_data/global_data.h" #include "../global_data/global_data.h"
#include "tools/log.h" #include "tools/log.h"
#include <freertos/queue.h>
extern "C" uint8_t temprature_sens_read(); extern "C" uint8_t temprature_sens_read();
extern DeviceTelemetry telemetry;
void collect_internal_telemetry_task(void* parameter) void collect_internal_telemetry_task(void* parameter)
{ {
// Extract the queue handle from the task parameters
QueueHandle_t dataQueue = (QueueHandle_t)parameter;
if (dataQueue == NULL) {
LOG(ELOG_LEVEL_ERROR, "Data queue is NULL");
vTaskDelete(NULL);
return;
}
LOG(ELOG_LEVEL_DEBUG, "Starting internal telemetry tasks"); LOG(ELOG_LEVEL_DEBUG, "Starting internal telemetry tasks");
while (true) { while (true) {
float heap_usage = (float(ESP.getFreeHeap()) / float(ESP.getHeapSize())) * 100; float heap_usage = (float(ESP.getFreeHeap()) / float(ESP.getHeapSize())) * 100;
uint64_t uptime_seconds = millis() / 1000; uint64_t uptime_seconds = millis() / 1000;
float temperature = (temprature_sens_read()-32) / 1.8;
telemetry.heap_used_percent = heap_usage; // Create a DataMessage for telemetry data
telemetry.uptime_seconds = uptime_seconds; DataMessage dataMessage;
telemetry.temperature = (temprature_sens_read()-32) / 1.8; dataMessage.type = DATA_TYPE_TELEMETRY;
delay(60000); dataMessage.data.telemetryData.heap_used_percent = heap_usage;
dataMessage.data.telemetryData.uptime_seconds = uptime_seconds;
dataMessage.data.telemetryData.temperature = temperature;
// Send the telemetry data to the queue
if (xQueueSend(dataQueue, &dataMessage, 0) != pdTRUE) {
LOG(ELOG_LEVEL_ERROR, "Failed to send telemetry data to queue");
}
delay(10 * 1000);
} }
} }

59
src/tools/ota_handler.cpp Normal file
View File

@@ -0,0 +1,59 @@
#include <ArduinoOTA.h>
#include <Elog.h>
#include "tools/log.h"
/**
* @brief Task to handle ArduinoOTA updates.
*
* This task initializes the ArduinoOTA library and handles OTA updates.
* It runs indefinitely to process OTA update requests.
*
* @param pvParameters Task parameters (unused).
*/
void ota_handler_task(void *pvParameters) {
(void)pvParameters; // Suppress unused parameter warning
LOG(ELOG_LEVEL_DEBUG, "Setting up OTA handler");
// Configure OTA settings
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
LOG(ELOG_LEVEL_DEBUG, "Start updating %s", type.c_str());
})
.onEnd([]() {
LOG(ELOG_LEVEL_DEBUG, "\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
LOG(ELOG_LEVEL_DEBUG, "Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
LOG(ELOG_LEVEL_DEBUG, "Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
LOG(ELOG_LEVEL_DEBUG, "Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
LOG(ELOG_LEVEL_DEBUG, "Receive Failed");
} else if (error == OTA_END_ERROR) {
LOG(ELOG_LEVEL_DEBUG, "End Failed");
}
});
// Start the OTA handler
ArduinoOTA.begin();
// Run indefinitely to handle OTA updates
while (1) {
ArduinoOTA.handle();
vTaskDelay(250 / portTICK_PERIOD_MS); // Even more frequent handling
}
}

14
src/tools/ota_handler.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef OTA_HANDLER_H
#define OTA_HANDLER_H
/**
* @brief Task to handle ArduinoOTA updates.
*
* This task initializes the ArduinoOTA library and handles OTA updates.
* It runs indefinitely to process OTA update requests.
*
* @param pvParameters Task parameters (unused).
*/
void ota_handler_task(void *pvParameters);
#endif // OTA_HANDLER_H

View 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);
}

View 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

View File

@@ -21,10 +21,6 @@ extern AsyncWebSocket webSocket;
extern Version current_spiffs_version; extern Version current_spiffs_version;
bool is_error(ActiveErrors active_errors) {
return active_errors.current_high || active_errors.current_low || active_errors.level_high || active_errors.level_low || active_errors.voltage_high || active_errors.voltage_low;
}
String processor(const String& var) String processor(const String& var)
{ {
if (var == level_sensor_range_key) { if (var == level_sensor_range_key) {
@@ -49,9 +45,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 +100,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)) {

View File

@@ -6,7 +6,6 @@
void printSuffix(Print* _logOutput, int logLevel); void printSuffix(Print* _logOutput, int logLevel);
void print_prefix(Print* _logOutput, int logLevel); void print_prefix(Print* _logOutput, int logLevel);
bool is_error(ActiveErrors active_errors);
String processor(const String& var); String processor(const String& var);
void check_update_task(void* parameter); void check_update_task(void* parameter);
void run_ota_update_task(void* parameter); void run_ota_update_task(void* parameter);