From 27ebbe22aca3917e77aea4d12f46707e1d6f799a Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 12:50:10 +0100 Subject: [PATCH 01/16] Updates --- .vscode/extensions.json | 3 +- .vscode/settings.json | 21 +++- documentation/old.md | 6 +- lib/fetchOTA/fetchOTA.cpp | 48 ++++----- platformio.ini | 24 ++--- src/external_interfacing/leds.cpp | 98 ++++-------------- src/external_interfacing/leds.h | 20 ++-- src/main.cpp | 158 +++++++++++++----------------- src/networking/networking.cpp | 40 +++++--- src/sensor/sensor.cpp | 19 ++-- src/tools/log.h | 3 +- src/tools/tools.cpp | 52 +++++----- src/tools/tools.h | 2 +- upload.sh | 3 + 14 files changed, 221 insertions(+), 276 deletions(-) create mode 100755 upload.sh diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 080e70d..8057bc7 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,7 +1,6 @@ { - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format "recommendations": [ + "pioarduino.pioarduino-ide", "platformio.platformio-ide" ], "unwantedRecommendations": [ diff --git a/.vscode/settings.json b/.vscode/settings.json index 5601ac9..a8b8076 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,25 @@ "files.associations": { "iostream": "cpp", "random": "cpp", - "vector": "cpp" + "vector": "cpp", + "array": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "string_view": "cpp", + "memory": "cpp", + "initializer_list": "cpp", + "regex": "cpp", + "istream": "cpp", + "ostream": "cpp", + "chrono": "cpp", + "list": "cpp", + "format": "cpp", + "mutex": "cpp", + "span": "cpp", + "text_encoding": "cpp", + "thread": "cpp", + "*.inc": "cpp" } } \ No newline at end of file diff --git a/documentation/old.md b/documentation/old.md index d17c7b9..1fa3ec8 100644 --- a/documentation/old.md +++ b/documentation/old.md @@ -17,7 +17,7 @@ void listNetworkInterfaces() { } void setEthernetAsDefault() { - logger.log(0, DEBUG, "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + Logger.log(0, ELOG_LEVEL_DEBUG, "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); listNetworkInterfaces(); // Attempt to locate the Ethernet network interface. // (The name may vary—on some systems it might be "eth0") @@ -31,8 +31,8 @@ void setEthernetAsDefault() { struct netif *eth_netif2 = netif_find("ETH_DEF"); if (netif) { netif_set_default(eth_netif2); - logger.log(0, DEBUG, "Ethernet set as default network interface."); + Logger.log(0, ELOG_LEVEL_DEBUG, "Ethernet set as default network interface."); } else { - logger.log(0, DEBUG, "Could not find Ethernet netif."); + Logger.log(0, ELOG_LEVEL_DEBUG, "Could not find Ethernet netif."); } } \ No newline at end of file diff --git a/lib/fetchOTA/fetchOTA.cpp b/lib/fetchOTA/fetchOTA.cpp index ccf7421..8b08b54 100644 --- a/lib/fetchOTA/fetchOTA.cpp +++ b/lib/fetchOTA/fetchOTA.cpp @@ -22,14 +22,14 @@ Firmware OTA::getLatestVersionOnServer() { HTTPClient http; http.begin(_serverUrl); int httpCode = http.GET(); - logger.log(0, DEBUG, "HTTP Code: %d", httpCode); + Logger.log(0, ELOG_LEVEL_DEBUG, "HTTP Code: %d", httpCode); if (httpCode != 200) { return createErrorResponse("HTTP GET request failed with code " + String(httpCode)); } String payload = http.getString(); - logger.log(0, DEBUG, "Payload: %s", payload.c_str()); + Logger.log(0, ELOG_LEVEL_DEBUG, "Payload: %s", payload.c_str()); DynamicJsonDocument doc(4096); DeserializationError error = deserializeJson(doc, payload); @@ -54,7 +54,7 @@ Firmware OTA::getLatestVersionOnServer() { deviceConfig }); } else { - logger.log(0, DEBUG, "Configuration %s does not match current device configuration %s", deviceConfig.c_str(), _current_device_configuration.c_str()); + Logger.log(0, ELOG_LEVEL_DEBUG, "Configuration %s does not match current device configuration %s", deviceConfig.c_str(), _current_device_configuration.c_str()); } } } @@ -67,7 +67,7 @@ Firmware OTA::getLatestVersionOnServer() { Configuration latest = getLatestConfiguration(configs.data(), configs.size()); if (!isVersionNewer(_currentVersion, latest.version)) { - logger.log(0, DEBUG, "No newer version found. Server version: %d.%d.%d", latest.version.major, latest.version.minor, latest.version.patch); + Logger.log(0, ELOG_LEVEL_DEBUG, "No newer version found. Server version: %d.%d.%d", latest.version.major, latest.version.minor, latest.version.patch); } return Firmware{ @@ -88,79 +88,79 @@ Firmware OTA::createErrorResponse(const String& errorMsg) { } void run_ota_update(String url, std::function callback_started, std::function callback_finished, std::function callback_progress, std::function callback_error) { - logger.log(0, DEBUG, "Starting OTA upgrade"); + Logger.log(0, ELOG_LEVEL_DEBUG, "Starting OTA upgrade"); HTTPUpdate httpUpdate; httpUpdate.onStart(callback_started); httpUpdate.onEnd(callback_finished); httpUpdate.onProgress(callback_progress); httpUpdate.onError(callback_error); - logger.log(0, DEBUG, "Defined callbacks, Starting update now"); + Logger.log(0, ELOG_LEVEL_DEBUG, "Defined callbacks, Starting update now"); t_httpUpdate_return ret; if (url.startsWith("https")) { - logger.log(0, DEBUG, "HTTPS URL"); - WiFiClientSecure client; - client.setInsecure(); + Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL"); + WiFiClient client; + // client.setInsecure(); ret = httpUpdate.update(client, url); } else if (url.startsWith("http")) { - logger.log(0, DEBUG, "HTTP URL"); + Logger.log(0, ELOG_LEVEL_DEBUG, "HTTP URL"); WiFiClient client; ret = httpUpdate.update(client, url); } else { - logger.log(0, 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()); } switch (ret) { case HTTP_UPDATE_FAILED: - logger.log(0, 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()); break; case HTTP_UPDATE_NO_UPDATES: - logger.log(0, ERROR, "HTTP_UPDATE_NO_UPDATES"); + Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES"); break; case HTTP_UPDATE_OK: - logger.log(0, DEBUG, "Update done"); + Logger.log(0, ELOG_LEVEL_DEBUG, "Update done"); break; } } void run_ota_spiffs_update(String url, std::function callback_started, std::function callback_finished, std::function callback_progress, std::function callback_error) { - logger.log(0, DEBUG, "Starting OTA SPIFFS upgrade"); + Logger.log(0, ELOG_LEVEL_DEBUG, "Starting OTA SPIFFS upgrade"); HTTPUpdate httpUpdate; httpUpdate.onStart(callback_started); httpUpdate.onEnd(callback_finished); httpUpdate.onProgress(callback_progress); httpUpdate.onError(callback_error); - logger.log(0, DEBUG, "Defined callbacks, Starting update now"); + Logger.log(0, ELOG_LEVEL_DEBUG, "Defined callbacks, Starting update now"); t_httpUpdate_return ret; if (url.startsWith("https")) { - logger.log(0, DEBUG, "HTTPS URL"); - WiFiClientSecure client; - client.setInsecure(); + Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL"); + WiFiClient client; + // client.setInsecure(); ret = httpUpdate.updateSpiffs(client, url); } else if (url.startsWith("http")) { - logger.log(0, DEBUG, "HTTP URL"); + Logger.log(0, ELOG_LEVEL_DEBUG, "HTTP URL"); WiFiClient client; ret = httpUpdate.updateSpiffs(client, url); } else { - logger.log(0, 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()); } switch (ret) { case HTTP_UPDATE_FAILED: - logger.log(0, 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()); break; case HTTP_UPDATE_NO_UPDATES: - logger.log(0, ERROR, "HTTP_UPDATE_NO_UPDATES"); + Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES"); break; case HTTP_UPDATE_OK: - logger.log(0, DEBUG, "SPIFFS Update done"); + Logger.log(0, ELOG_LEVEL_DEBUG, "SPIFFS Update done"); break; } } diff --git a/platformio.ini b/platformio.ini index 408d44d..0bda890 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,7 @@ default_envs = ESP32_INA233, ESP32_INA226 [env:ESP32_INA233] -platform = espressif32 +platform = espressif32 @ 6.0.0 board = esp32dev framework = arduino monitor_speed = 115200 @@ -22,28 +22,28 @@ lib_deps = fetchOTA INA233 ESP32Ping - https://github.com/tobimai/elog.git#fix-syslog + x385832/Elog board_build.partitions = default.csv upload_protocol = espota upload_port = 192.168.5.205 -build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE +build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE -DELOG_SYSLOG_ENABLE -fexceptions [env:ESP32_INA226] -platform = espressif32 +platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.32/platform-espressif32.zip board = esp32dev framework = arduino monitor_speed = 115200 +; monitor_filters = esp32_exception_decoder lib_deps = - ottowinter/ESPAsyncWebServer-esphome@^3.3.0 - robtillaart/INA226@^0.4.4 + esp32async/AsyncTCP @ ~3.4.9 + esp32async/ESPAsyncWebServer @ ~3.8.1 + robtillaart/INA226@ ~0.6.4 bblanchon/ArduinoJson@^6.21.3 - jsc/ArduinoLog - ESP32Ping - https://github.com/tobimai/elog.git#fix-syslog -board_build.partitions = default.csv + x385832/Elog@~2.0.10 +board_build.partitions = min_spiffs.csv upload_protocol = espota -upload_port = 192.168.4.18 -build_flags = -Wall -Wextra -DUSE_INA226 -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE +upload_port = 192.168.6.45 +build_flags = -Wall -Wextra -DUSE_INA226 -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE -DELOG_SYSLOG_ENABLE -fexceptions [env:native] platform = native diff --git a/src/external_interfacing/leds.cpp b/src/external_interfacing/leds.cpp index 6f94ff0..f6e1e0a 100644 --- a/src/external_interfacing/leds.cpp +++ b/src/external_interfacing/leds.cpp @@ -7,93 +7,29 @@ extern ActiveErrors active_errors; extern WaterData water_data; -void display_percentage(float percentage) -{ - digitalWrite(LED_RED, 0); - - if (percentage > 0) { - digitalWrite(LED_1, 1); - } else { - digitalWrite(LED_1, 0); - } - if (percentage > 20) { - digitalWrite(LED_2, 1); - } else { - digitalWrite(LED_2, 0); - } - if (percentage > 40) { - digitalWrite(LED_3, 1); - } else { - digitalWrite(LED_3, 0); - } - if (percentage > 60) { - digitalWrite(LED_4, 1); - } else { - digitalWrite(LED_4, 0); - } - if (percentage > 80) { - digitalWrite(LED_5, 1); - } else { - digitalWrite(LED_5, 0); - } - - if (percentage > 10) { - delay(3000); - } else if (percentage > 0) { - for (int i = 0; i < 3; i++) { - digitalWrite(LED_1, 1); - delay(500); - digitalWrite(LED_1, 0); - delay(500); - } - } else if (percentage <= 0) { - for (int i = 0; i < 15; i++) { - digitalWrite(LED_1, 1); - delay(100); - digitalWrite(LED_1, 0); - delay(100); - } - } -} - -void display_error_code(byte err_code) -{ - digitalWrite(LED_RED, 1); - digitalWrite(LED_1, bitRead(err_code, 0)); - digitalWrite(LED_2, bitRead(err_code, 1)); - digitalWrite(LED_3, bitRead(err_code, 2)); - digitalWrite(LED_4, bitRead(err_code, 3)); - digitalWrite(LED_5, bitRead(err_code, 4)); -} - void display_task(void* parameter) { while (true) { if (!is_error(active_errors)) { // We have no error, refresh status display and wait half a second - display_percentage(water_data.percentage); + ledcWrite(LED_GREEN, 255); + ledcWrite(LED_RED, 0); } else { - // We have an error, display error code for 3 seconds and then water level for 3 seconds - if (active_errors.voltage_low) { - display_error_code(1); - LOG(WARNING, "Sensor Error - Voltage low"); - delay(3000); - } else if (active_errors.voltage_high) { - display_error_code(2); - LOG(WARNING, "Sensor Error - Voltage High"); - delay(3000); - } else if (active_errors.current_low) { - display_error_code(3); - LOG(WARNING, "Sensor Error - Current low"); - delay(3000); - } else if (active_errors.current_high) { - display_error_code(4); - LOG(WARNING, "Sensor Error - Current high"); - delay(3000); - } else { - delay(3000); - } - display_percentage(water_data.percentage); + ledcWrite(LED_RED, LED_RED_HIGH); + ledcWrite(LED_GREEN, 0); } + delay(250); } +} + + +// Setup pin modes etc. +void led_setup() { + pinMode(LED_RED, OUTPUT); + pinMode(LED_GREEN, OUTPUT); + + ledcAttach(LED_RED, LED_PWM_FREQUENCY, LED_PWM_RESOLUTION); + ledcAttach(LED_GREEN, LED_PWM_FREQUENCY, LED_PWM_RESOLUTION); + ledcWrite(LED_RED, LED_RED_HIGH); + ledcWrite(LED_GREEN, 255); } \ No newline at end of file diff --git a/src/external_interfacing/leds.h b/src/external_interfacing/leds.h index 2b6d858..05c0f6d 100644 --- a/src/external_interfacing/leds.h +++ b/src/external_interfacing/leds.h @@ -1,12 +1,14 @@ #include -#define LED_1 4 -#define LED_2 2 -#define LED_3 15 -#define LED_4 13 -#define LED_5 12 -#define LED_RED 14 +#define LED_RED 15 +#define LED_GREEN 2 +#define LED_PWM_FREQUENCY 5000 +#define LED_PWM_RESOLUTION 8 +#define LED_CHANNEL_RED 0 +#define LED_CHANNEL_GREEN 1 -void display_percentage(float percentage); -void display_error_code(byte err_code); -void display_task(void* parameter); \ No newline at end of file +#define LED_RED_HIGH 150 + +// void display_error_code(byte err_code); +void display_task(void* parameter); +void led_setup(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f21cfe2..0fcad25 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,9 +24,9 @@ #include "networking/json_builder.h" #include "networking/responses.h" #include -#include #include "time.h" #include "tools/log.h" +#include #define MYLOG 0 @@ -37,68 +37,64 @@ extern DeviceTelemetry telemetry; extern NetworkData wifi_data; extern NetworkData ethernet_data; extern SensorData shunt_data; +extern ActiveErrors active_errors; extern "C" int rom_phy_get_vdd33(); Version current_spiffs_version; AsyncWebServer server(80); -AsyncWebSocket webSocket("/webSocket"); +// AsyncWebSocket webSocket("/webSocket"); #define FORMAT_LITTLEFS_IF_FAILED true void setup() -{ - logger.registerSerial(MYLOG, DEBUG, "tst"); +{ + Logger.registerSerial(MYLOG, ELOG_LEVEL_DEBUG, "Serial"); + LOG(ELOG_LEVEL_DEBUG, "Init LEDs"); + led_setup(); + + prefs.begin("waterlevel", false); Serial.begin(115200); - LOG(DEBUG, "Init LEDs"); - pinMode(LED_1, OUTPUT); - pinMode(LED_2, OUTPUT); - pinMode(LED_3, OUTPUT); - pinMode(LED_4, OUTPUT); - pinMode(LED_5, OUTPUT); - pinMode(LED_RED, OUTPUT); - display_error_code(31); + delay(500); - display_error_code(17); init_sensor(); - LOG(DEBUG, "Beginning SPIFFS"); - SPIFFS.begin(true); + LOG(ELOG_LEVEL_DEBUG, "Beginning SPIFFS"); + //SPIFFS.begin(FORMAT_LITTLEFS_IF_FAILED); + LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED); // Read the current SPIFFS version from the versions file on the spiffs - File file = SPIFFS.open("/version", FILE_READ); + File file = LittleFS.open("/version", FILE_READ); if (!file) { Serial.println("Failed to open version file for reading"); } else { String version = file.readStringUntil('\n'); - LOG(DEBUG, "Version: %s", version); + LOG(ELOG_LEVEL_DEBUG, "Version: %s", version); current_spiffs_version = parseVersion(version.c_str()); - LOG(DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch); + LOG(ELOG_LEVEL_DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch); } - LOG(DEBUG, "SPIFFS initialized"); - display_error_code(19); + LOG(ELOG_LEVEL_DEBUG, "SPIFFS initialized"); - LOG(DEBUG, "Begin INA"); + LOG(ELOG_LEVEL_DEBUG, "Begin INA"); - display_error_code(22); /////////////////////////////// ROUTES /////////////////////////////// - LOG(DEBUG, "Route Setup"); + LOG(ELOG_LEVEL_DEBUG, "Route Setup"); // Normal HTML stuff - server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/status.html", "text/html", false, processor); }); + server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/status.html", "text/html", false, processor); }); - server.on("/settings", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/settings.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(SPIFFS, "/data_export.html", "text/html", false); }); + 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(SPIFFS, "/logic.js", "application/javascript", false, processor); }); + 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(SPIFFS, "/update_progress.html", "text/html", 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) { @@ -106,27 +102,27 @@ void setup() // For settings SSID if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) { - LOG(DEBUG, "Updating SSID config"); - AsyncWebParameter* ssid_param = request->getParam(ssid_key, true); - AsyncWebParameter* password_param = request->getParam(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(SPIFFS, "/settings.html", "text/html", false, processor); // TODO add proper return templating + 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(DEBUG, "Updating Sensor config"); - AsyncWebParameter* range_param = request->getParam(level_sensor_range_key, true); - AsyncWebParameter* level_min_param = request->getParam(water_level_min_key, true); - AsyncWebParameter* level_max_param = request->getParam(water_level_max_key, true); - AsyncWebParameter* liters_param = request->getParam(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(); @@ -144,44 +140,42 @@ void setup() // Convert the C string to a float using strtod float value = strtod(paramCStr, &endPtr); - LOG(DEBUG, "range_float:%D:", range_float); + 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(DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0)); + LOG(ELOG_LEVEL_DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0)); } else { - LOG(DEBUG, "!!!! FAIL lo"); + LOG(ELOG_LEVEL_DEBUG, "!!!! FAIL lo"); for (int i = 0; i < params; i++) { - AsyncWebParameter* p = request->getParam(i); + const AsyncWebParameter* p = request->getParam(i); if (p->isFile()) { // p->isPost() is also true - LOG(DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + LOG(ELOG_LEVEL_DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); } else if (p->isPost()) { - LOG(DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + LOG(ELOG_LEVEL_DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); } else { - LOG(DEBUG, "GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); + 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(SPIFFS, "/settings.html", "text/html", false); // TODO add proper return templating + request->send(LittleFS, "/settings.html", "text/html", false); // TODO add proper return templating }); setup_api_endpoints(); - display_error_code(23); - webSocket.onEvent(onWsEvent); - server.addHandler(&webSocket); + // webSocket.onEvent(onWsEvent); + // server.addHandler(&webSocket); - server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/chota.css", "text/css", false); }); - server.on("/gauge.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/gauge.js", "application/javascript", false); }); + 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); }); - display_error_code(24); - LOG(DEBUG, "OTA Setup"); + LOG(ELOG_LEVEL_DEBUG, "OTA Setup"); ArduinoOTA .onStart([]() { String type; @@ -191,39 +185,39 @@ void setup() type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() - LOG(DEBUG, "Start updating %s", type); }) - .onEnd([]() { LOG(DEBUG, "\nEnd"); }) + 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(DEBUG, "Auth Failed"); - else if (error == OTA_BEGIN_ERROR) LOG(DEBUG, "Begin Failed"); - else if (error == OTA_CONNECT_ERROR) LOG(DEBUG, "Connect Failed"); - else if (error == OTA_RECEIVE_ERROR) LOG(DEBUG, "Receive Failed"); - else if (error == OTA_END_ERROR) LOG(DEBUG, "End Failed"); }); + 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"); }); - display_error_code(26); digitalWrite(LED_RED, 0); // Starting bootup sequence xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL); + xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); + + esp_netif_t* eth = esp_netif_get_handle_from_ifkey("ETH_DEF"); + esp_netif_t* wifi = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); - // Create Etnernet task and wait a second to see if there is connection - LOG(DEBUG, "Started Ethernet, waiting"); - delay(2000); if (ETH.linkUp()){ - LOG(DEBUG, "Ethernet connected, starting update checker"); + LOG(ELOG_LEVEL_DEBUG, "Ethernet connected, starting update checker"); xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); } else { - LOG(DEBUG, "Ethernet not connected, starting update checker and WiFi Task"); - xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); + LOG(ELOG_LEVEL_DEBUG, "Ethernet not connected, starting update checker and WiFi Task"); + // esp_netif_set_default_netif(esp_netif_get_handle_from_ifkey("ETH_DEF")); xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); delay(2000); } - LOG(DEBUG, "Getting time now"); + LOG(ELOG_LEVEL_DEBUG, "Getting time now"); // Configure time to UTC configTime(0, 0, "pool.ntp.org"); @@ -245,44 +239,24 @@ void setup() // Sanity check: Ensure the year is at least 2020. int currentYear = timeinfo.tm_year + 1900; // tm_year is years since 1900 if (currentYear < 2020) { - LOG(DEBUG, "Time not set properly: "); + LOG(ELOG_LEVEL_DEBUG, "Time not set properly: "); } else { - LOG(DEBUG, "Time is valid: %s", asctime(&timeinfo)); + LOG(ELOG_LEVEL_DEBUG, "Time is valid: %s", asctime(&timeinfo)); } // Setup syslog - logger.configureSyslog("192.168.6.11", 5514, "esp32"); - logger.registerSyslog(MYLOG, DEBUG, FAC_USER, "waterlevel"); - LOG(ERROR, "Here is an error message, error code: %d", 17); + LOG(ELOG_LEVEL_ERROR, "Here is an error message, error code: %d", 17); - LOG(DEBUG, "Starting webserver"); + LOG(ELOG_LEVEL_DEBUG, "Starting webserver"); server.begin(); ArduinoOTA.begin(); - display_error_code(25); - xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL); xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, NULL, 1, NULL); xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL); - - // Wait until there is network connection - while (true) { - if (WiFi.status() == WL_CONNECTED || ETH.localIP()) { - int pingResult = Ping.ping("8.8.8.8"); // Use Google's public DNS server as a test IP - if (pingResult >= 0) { - LOG(DEBUG, "Network connection established"); - break; - } else { - LOG(DEBUG, "Network not ready, retrying..."); - } - } else { - LOG(DEBUG, "No WiFi or Ethernet connection, retrying..."); - } - delay(1000); // Delay to prevent rapid retry - } - + xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL); } diff --git a/src/networking/networking.cpp b/src/networking/networking.cpp index 484b1d1..a461700 100644 --- a/src/networking/networking.cpp +++ b/src/networking/networking.cpp @@ -1,11 +1,20 @@ #include -#include #include #include "../global_data/defines.h" #include #include "../global_data/global_data.h" #include +#define ETH_PHY_TYPE ETH_PHY_LAN8720 +#define ETH_PHY_ADDR 0 +#define ETH_PHY_MDC 23 +#define ETH_PHY_MDIO 18 +#define ETH_PHY_POWER 14 +#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN + + +#include + int64_t mac_address = ESP.getEfuseMac(); uint8_t failed_connection_attempts = 0; @@ -13,6 +22,7 @@ extern NetworkData wifi_data; extern NetworkData ethernet_data; extern Preferences prefs; +// Defines the type of connection for which the hostname should be created enum HostnameType { Wireless, Ethernet, @@ -28,28 +38,28 @@ const char * get_hostname(HostnameType host_type) { return (hostname + "-wl").c_str(); case Ethernet: return (hostname + "-eth").c_str(); - case Generic: + default: return hostname.c_str(); } } void wifi_task(void* parameter) { - LOG(DEBUG, "Starting WiFi Task"); + LOG(ELOG_LEVEL_DEBUG, "Starting WiFi Task"); WiFi.setHostname(get_hostname(Wireless)); while (true) { if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) { wifi_data.link = false; if (failed_connection_attempts > 5) { - LOG(DEBUG, "Failed to connecto to currently saved SSID, starting SoftAP"); + LOG(ELOG_LEVEL_DEBUG, "Failed to connect to currently saved SSID, starting SoftAP"); } else { - LOG(DEBUG, "No SSID saved, starting SoftAP"); + LOG(ELOG_LEVEL_DEBUG, "No SSID saved, starting SoftAP"); } String ap_ssid = get_hostname(Wireless); - WiFi.softAP(ap_ssid, ""); + WiFi.softAP(ap_ssid.c_str(), ""); - LOG(DEBUG, "[WIFI_TASK] Waiting for SSID now..."); + LOG(ELOG_LEVEL_DEBUG, "[WIFI_TASK] Waiting for SSID now..."); String old_ssid = prefs.getString(ssid_key, "xxx"); while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) { @@ -65,13 +75,14 @@ void wifi_task(void* parameter) wifi_data.network_name = WiFi.SSID(); wifi_data.ip_address = WiFi.localIP().toString(); - LOG(DEBUG, "RSSI: %F, IP Address, %p, SSID: %s", float(WiFi.RSSI()), WiFi.localIP(), prefs.getString(ssid_key, "NOSSID")); - Serial.println(WiFi.channel()); + LOG(ELOG_LEVEL_DEBUG, "RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID")); + Serial.println(WiFi.localIP()); delay(5000); } else { - LOG(DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); + LOG(ELOG_LEVEL_DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); WiFi.mode(WIFI_STA); - WiFi.begin(prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); + + WiFi.begin(prefs.getString(ssid_key, "").c_str(), prefs.getString(wifi_password_key, "").c_str()); failed_connection_attempts++; delay(5000); } @@ -81,15 +92,14 @@ void wifi_task(void* parameter) void ethernet_task(void* parameter) { - LOG(DEBUG, "Connecting Ethernet"); - ETH.begin(0, 17, 23, 18); + LOG(ELOG_LEVEL_DEBUG, "Connecting Ethernet"); + ETH.begin(); ETH.setHostname(get_hostname(Ethernet)); while (true) { ethernet_data.link = ETH.linkUp(); ethernet_data.rssi = ETH.linkSpeed(); ethernet_data.ip_address = ETH.localIP().toString(); - Serial.println( ETH.localIP().toString()); - LOG(DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ETH.localIP().toString().c_str(), 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)); delay(60 * 1000); } } \ No newline at end of file diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index d935c44..662a8ee 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -26,7 +26,8 @@ extern SensorData shunt_data; float zero_value = 0.03; // Measured shunt voltage with nothing connected, used to fix measuring offset void init_sensor(){ - ina_sensor.begin(33, 32); + Wire.begin(33, 32); + ina_sensor.begin(); #ifdef USE_INA226 ina_sensor.setMaxCurrentShunt(0.02, 4, false); ina_sensor.setBusVoltageConversionTime(7); @@ -47,13 +48,13 @@ void read_sensor_task(void* parameter) // Get Values from sensor #ifndef USE_INA226 String chip_id = ina_sensor.get_device_model(); - LOG(DEBUG, "Chip Model: %s", chip_id.c_str()); + LOG(ELOG_LEVEL_DEBUG, "Chip Model: %s", chip_id.c_str()); #endif float bus_voltage = ina_sensor.getBusVoltage(); float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value; - LOG(DEBUG, "RAW Shunt voltage: %F mV", ina_sensor.getShuntVoltage_mV()); + LOG(ELOG_LEVEL_DEBUG, "RAW Shunt voltage: %F mV", ina_sensor.getShuntVoltage_mV()); float shunt_current = shunt_voltage / RESISTOR_VALUE; @@ -73,8 +74,8 @@ void read_sensor_task(void* parameter) float min_water_level_mA = 4 + min_water_level_mA_over_zero; float max_water_level_mA = 4 + max_water_level_mA_over_zero; - LOG(DEBUG, "max_water_level_mA: %F", max_water_level_mA); - LOG(DEBUG, "min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); + LOG(ELOG_LEVEL_DEBUG, "max_water_level_mA: %F", max_water_level_mA); + LOG(ELOG_LEVEL_DEBUG, "min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); // Current over the 0 level of the water float shunt_current_over_zero = shunt_current - min_water_level_mA; @@ -94,10 +95,10 @@ void read_sensor_task(void* parameter) active_errors.current_high = shunt_current > 20.2; active_errors.voltage_low = bus_voltage < 23; active_errors.voltage_high = bus_voltage > 25; - LOG(DEBUG, "Shunt current: %F", shunt_current); - LOG(DEBUG, "Shunt voltage: %F", shunt_voltage); - LOG(DEBUG, "Bus voltage: %F", bus_voltage); - LOG(DEBUG, "cm_over_zero: %F", cm_over_zero); + LOG(ELOG_LEVEL_DEBUG, "Shunt current: %F", shunt_current); + LOG(ELOG_LEVEL_DEBUG, "Shunt voltage: %F", shunt_voltage); + LOG(ELOG_LEVEL_DEBUG, "Bus voltage: %F", bus_voltage); + LOG(ELOG_LEVEL_DEBUG, "cm_over_zero: %F", cm_over_zero); shunt_data.bus_voltage = bus_voltage; shunt_data.shunt_voltage = shunt_voltage; diff --git a/src/tools/log.h b/src/tools/log.h index 3f46fef..ea0a3f7 100644 --- a/src/tools/log.h +++ b/src/tools/log.h @@ -1,9 +1,10 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include #define LOG(LEVEL, FMT, ...) \ do { \ - logger.log(0, LEVEL, "[Core: %i][Task: %s][%s] " FMT, \ + Logger.log(0, LEVEL, "[Core: %i][Task: %s][Fn: %s]" FMT, \ xPortGetCoreID(), \ pcTaskGetName(xTaskGetCurrentTaskHandle()), \ __FUNCTION__, \ diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index 8850a44..cf021c0 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -9,7 +9,7 @@ extern Preferences prefs; extern OTAStatus ota_status; -extern AsyncWebSocket webSocket; +// extern AsyncWebSocket webSocket; extern Version current_spiffs_version; @@ -36,28 +36,28 @@ String processor(const String& var) // OTA Callbacks void update_started() { - LOG(DEBUG, "OTA Update started"); + LOG(ELOG_LEVEL_DEBUG, "OTA Update started"); ota_status.update_progress = 0; } void update_finished() { - LOG(DEBUG, "OTA Update finished"); + LOG(ELOG_LEVEL_DEBUG, "OTA Update finished"); ota_status.update_progress = -1; - webSocket.textAll(String(-1).c_str()); + // webSocket.textAll(String(-1).c_str()); } void update_progress(int cur, int total) { ota_status.update_progress = 0; if (cur != 0 ) { ota_status.update_progress = int(float(cur)/float(total)*100); - LOG(DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress); + LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress); } - webSocket.textAll(String(ota_status.update_progress).c_str()); - LOG(DEBUG, "OTA Update progress: %d/%d", cur, total); + // webSocket.textAll(String(ota_status.update_progress).c_str()); + LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d", cur, total); } void update_error(int err) { - LOG(ERROR, "OTA Update error: %d", err); + LOG(ELOG_LEVEL_ERROR, "OTA Update error: %d", err); ota_status.update_progress = -2; } @@ -76,20 +76,20 @@ void check_update_task(void* parameter) { // If there is a SPIFSS update it will be ran automatically, as SPIFFS is necessary for the firmware to run Firmware latest_spiff_version = spiffs_fs.getLatestVersionOnServer(); - LOG(DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch); + LOG(ELOG_LEVEL_DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch); if (latest_spiff_version.valid) { if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) { // If Required SPIFFS version is newer than current version, update - LOG(DEBUG, "New SPIFFS version, running update now"); + LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now"); run_ota_spiffs_update(latest_spiff_version.url, update_started, update_finished, update_progress, update_error); // Reboot just to be safe ESP.restart(); } else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_spiff_version.version)) { // If Server has new SPIFFS version but it's not required - LOG(DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); + LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); } else { - LOG(DEBUG, "No new SPIFFS version available"); + LOG(ELOG_LEVEL_DEBUG, "No new SPIFFS version available"); } } @@ -97,11 +97,11 @@ void check_update_task(void* parameter) { while (true) { Firmware fw = ota.getLatestVersionOnServer(); if (fw.valid) { - LOG(DEBUG, "New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); + LOG(ELOG_LEVEL_DEBUG, "New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); ota_status.latest_version = fw.version; ota_status.update_url = fw.url; if (isVersionNewer(current_software_version, fw.version)) { - LOG(DEBUG, "Remote version is newer than current version"); + LOG(ELOG_LEVEL_DEBUG, "Remote version is newer than current version"); ota_status.update_available = true; } else { ota_status.update_available = false; @@ -112,7 +112,7 @@ void check_update_task(void* parameter) { ota_status.update_available = false; } - LOG(DEBUG, "No new firmware available"); + LOG(ELOG_LEVEL_DEBUG, "No new firmware available"); } delay(1000 * 60 * 1); } @@ -122,19 +122,19 @@ void check_update_task(void* parameter) { void run_ota_update_task(void* parameter) { TaskArgs_t *args = (TaskArgs_t *) parameter; - LOG(DEBUG, "Running OTA upgrade now with URL: %s", args->ota_status.update_url.c_str()); + LOG(ELOG_LEVEL_DEBUG, "Running OTA upgrade now with URL: %s", args->ota_status.update_url.c_str()); run_ota_update(args->ota_status.update_url, update_started, update_finished, update_progress, update_error); vTaskDelete(NULL); } -void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, - void *arg, uint8_t *data, size_t len) { - if (type == WS_EVT_CONNECT) { - Serial.println("WebSocket client connected"); - } else if (type == WS_EVT_DISCONNECT) { - Serial.println("WebSocket client disconnected"); - } else if (type == WS_EVT_DATA) { - // Optionally process data received from the client - } -} +// void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, +// void *arg, uint8_t *data, size_t len) { +// if (type == WS_EVT_CONNECT) { +// Serial.println("WebSocket client connected"); +// } else if (type == WS_EVT_DISCONNECT) { +// Serial.println("WebSocket client disconnected"); +// } else if (type == WS_EVT_DATA) { +// // Optionally process data received from the client +// } +// } diff --git a/src/tools/tools.h b/src/tools/tools.h index c37bd57..839a48f 100644 --- a/src/tools/tools.h +++ b/src/tools/tools.h @@ -10,7 +10,7 @@ bool is_error(ActiveErrors active_errors); String processor(const String& var); void check_update_task(void* parameter); void run_ota_update_task(void* parameter); -void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,void *arg, uint8_t *data, size_t len); +// void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,void *arg, uint8_t *data, size_t len); typedef struct { OTAStatus ota_status; diff --git a/upload.sh b/upload.sh new file mode 100755 index 0000000..3fa5574 --- /dev/null +++ b/upload.sh @@ -0,0 +1,3 @@ +pio run -t upload -e ESP32_INA226 --upload-port 192.168.6.45 +pio run -t upload -e ESP32_INA226 --upload-port 192.168.6.47 +pio run -t upload -e ESP32_INA226 --upload-port 192.168.6.49 \ No newline at end of file From 021a5c68fa13199e95ee52b0e9dad03678bcfeeb Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Sun, 2 Nov 2025 16:54:48 +0100 Subject: [PATCH 02/16] Fix --- .devcontainer/docker-compose.yaml | 2 +- platformio.ini | 15 +++++++++------ src/sensor/sensor.cpp | 5 +++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.devcontainer/docker-compose.yaml b/.devcontainer/docker-compose.yaml index d37f144..5c23772 100644 --- a/.devcontainer/docker-compose.yaml +++ b/.devcontainer/docker-compose.yaml @@ -1,7 +1,7 @@ version: '3.8' services: app: - image: gitea.maiertobi.de/tobimai/devcontainer-pio:latest + image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:latest user: tobi:tobi volumes: - ..:/workspace:cached diff --git a/platformio.ini b/platformio.ini index 0bda890..9902378 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,18 +12,20 @@ default_envs = ESP32_INA233, ESP32_INA226 [env:ESP32_INA233] -platform = espressif32 @ 6.0.0 +platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.32/platform-espressif32.zip board = esp32dev framework = arduino monitor_speed = 115200 +; monitor_filters = esp32_exception_decoder lib_deps = - ottowinter/ESPAsyncWebServer-esphome@^3.3.0 + esp32async/AsyncTCP @ ~3.4.9 + esp32async/ESPAsyncWebServer @ ~3.8.1 + robtillaart/INA226@ ~0.6.4 bblanchon/ArduinoJson@^6.21.3 - fetchOTA + x385832/Elog@~2.0.10 INA233 - ESP32Ping - x385832/Elog -board_build.partitions = default.csv + fetchOTA +board_build.partitions = min_spiffs.csv upload_protocol = espota upload_port = 192.168.5.205 build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE -DELOG_SYSLOG_ENABLE -fexceptions @@ -40,6 +42,7 @@ lib_deps = robtillaart/INA226@ ~0.6.4 bblanchon/ArduinoJson@^6.21.3 x385832/Elog@~2.0.10 + fetchOTA board_build.partitions = min_spiffs.csv upload_protocol = espota upload_port = 192.168.6.45 diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index 662a8ee..1927b14 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -26,14 +26,15 @@ extern SensorData shunt_data; float zero_value = 0.03; // Measured shunt voltage with nothing connected, used to fix measuring offset void init_sensor(){ - Wire.begin(33, 32); - ina_sensor.begin(); #ifdef USE_INA226 + Wire.begin(33, 32); + ina_sensor.begin(); ina_sensor.setMaxCurrentShunt(0.02, 4, false); ina_sensor.setBusVoltageConversionTime(7); ina_sensor.setShuntVoltageConversionTime(7); ina_sensor.setAverage(4); #else + ina_sensor.begin(33, 32); ina_sensor.reset(); ina_sensor.setShuntVoltageConversionTime(conversion_time_8244uS); ina_sensor.setBusVoltageConversionTime(conversion_time_8244uS); From 66a21928a471c4300be1082a0f45ff3fa684f8b0 Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Sun, 2 Nov 2025 16:55:37 +0100 Subject: [PATCH 03/16] fix url v2 --- .gitea/workflows/on_push.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index db06f34..b754601 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -6,7 +6,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: gitea.maiertobi.de/tobimai/devcontainer-pio:latest + image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:latest steps: - name: Checkout Code From d951d1d2be327740f65af62a57b343184d0d160c Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 20:12:57 +0100 Subject: [PATCH 04/16] Test --- .gitea/workflows/on_push.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index db06f34..2b3979e 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -18,7 +18,7 @@ jobs: - name: Run PlatformIO build for INA226 run: pio run -e ESP32_INA226 - name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary) - run: pio run --target buildfs -e ESP32_INA233 + run: pwd && pio run --target buildfs -e ESP32_INA233 - name: Upload firmware binary for INA233 run: | VERSION=$(sed -n 's/#define current_software_version Version{[[:space:]]*\([0-9]\+\),[[:space:]]*\([0-9]\+\),[[:space:]]*\([0-9]\+\)}/\1.\2.\3/p' src/global_data/defines.h) From 013356c41e8e5af72ad1a01f27a444e9a4826ee2 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 20:21:19 +0100 Subject: [PATCH 05/16] wtf --- .gitea/workflows/on_push.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index c3bbbfe..e57c85c 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -11,14 +11,14 @@ jobs: steps: - name: Checkout Code uses: actions/checkout@v2 + - name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary) + run: pio run --target buildfs -e ESP32_INA233 - name: Run PlatformIO Tests run: pio test -e native - name: Run PlatformIO build for INA233 run: pio run -e ESP32_INA233 - name: Run PlatformIO build for INA226 run: pio run -e ESP32_INA226 - - name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary) - run: pwd && pio run --target buildfs -e ESP32_INA233 - name: Upload firmware binary for INA233 run: | VERSION=$(sed -n 's/#define current_software_version Version{[[:space:]]*\([0-9]\+\),[[:space:]]*\([0-9]\+\),[[:space:]]*\([0-9]\+\)}/\1.\2.\3/p' src/global_data/defines.h) From 6ee4086716395e9bd547c35cd541ec3dd91e3327 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 20:27:13 +0100 Subject: [PATCH 06/16] fuck --- .gitea/workflows/on_push.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index e57c85c..8de6fd5 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -7,6 +7,7 @@ jobs: runs-on: ubuntu-latest container: image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:latest + options: --user 1000:1000 steps: - name: Checkout Code From aeac15aa2cd1892d8c6683eabe45ee9ce84a7283 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 20:30:03 +0100 Subject: [PATCH 07/16] test --- .gitea/workflows/on_push.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index 8de6fd5..1e0acca 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -13,7 +13,7 @@ jobs: - name: Checkout Code uses: actions/checkout@v2 - name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary) - run: pio run --target buildfs -e ESP32_INA233 + run: which pio && id && pio run --target buildfs -e ESP32_INA233 - name: Run PlatformIO Tests run: pio test -e native - name: Run PlatformIO build for INA233 From 037fc3391ce270bfb6de1b3108d91503fa4f6429 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 20:44:13 +0100 Subject: [PATCH 08/16] are you kidding me --- .gitea/workflows/on_push.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index 1e0acca..528437a 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest container: image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:latest - options: --user 1000:1000 + options: --pull always steps: - name: Checkout Code From 1d9f0f119abada693bc33f59f31d781ffd245859 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 20:48:12 +0100 Subject: [PATCH 09/16] use v2 docker image --- .gitea/workflows/on_push.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index 528437a..1fad2f2 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -6,9 +6,8 @@ jobs: test: runs-on: ubuntu-latest container: - image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:latest - options: --pull always - + image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2 + steps: - name: Checkout Code uses: actions/checkout@v2 From d2c3fc0b46b07c3c234a3232827b1ab4bf91ae79 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 20:53:23 +0100 Subject: [PATCH 10/16] lol --- .gitea/workflows/on_push.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index 1fad2f2..a11f73b 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -6,8 +6,8 @@ jobs: test: runs-on: ubuntu-latest container: - image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2 - + image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2.0.0 + steps: - name: Checkout Code uses: actions/checkout@v2 From dccdca3ad82e51357dcc9bf993a514b24e88274e Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 2 Nov 2025 21:13:38 +0100 Subject: [PATCH 11/16] fix FS image --- .gitea/workflows/on_push.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index a11f73b..e3f85cd 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -6,7 +6,7 @@ jobs: test: runs-on: ubuntu-latest container: - image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2.0.0 + image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2.1 steps: - name: Checkout Code @@ -27,7 +27,7 @@ jobs: curl -X PUT \ -H "Content-Type: application/octet-stream" \ --data-binary @.pio/build/ESP32_INA233/firmware.bin \ - https://iot.tobiasmaier.me/firmware/waterlevel/INA233/${VERSION} + https://iot.tobiasmaier.me/firmware/waterlevel/INA233REV2/${VERSION} - name: Upload firmware binary for INA226 run: | @@ -37,7 +37,7 @@ jobs: curl -X PUT \ -H "Content-Type: application/octet-stream" \ --data-binary @.pio/build/ESP32_INA226/firmware.bin \ - https://iot.tobiasmaier.me/firmware/waterlevel/INA226/${VERSION} + https://iot.tobiasmaier.me/firmware/waterlevel/INA226REV2/${VERSION} - name: Upload SPIFFS binary run: | @@ -46,5 +46,5 @@ jobs: curl -X PUT \ -H "Content-Type: application/octet-stream" \ - --data-binary @.pio/build/ESP32_INA233/spiffs.bin \ + --data-binary @.pio/build/ESP32_INA233/littlefs.bin \ https://iot.tobiasmaier.me/filesystem/waterlevel/generic/${VERSION} From 7697cb4ae99c9d86a49a1c974728e82274aa2acd Mon Sep 17 00:00:00 2001 From: tobimai Date: Mon, 3 Nov 2025 17:25:15 +0100 Subject: [PATCH 12/16] Fixed build pipelines --- .gitea/workflows/on_push.yaml | 6 ++++-- .gitea/workflows/test_build.yaml | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/test_build.yaml diff --git a/.gitea/workflows/on_push.yaml b/.gitea/workflows/on_push.yaml index e3f85cd..87d7f2d 100644 --- a/.gitea/workflows/on_push.yaml +++ b/.gitea/workflows/on_push.yaml @@ -1,6 +1,8 @@ -name: Test compiling project +name: Compilie project and upload binaries -on: [push] +on: + push: + branches: ["main"] jobs: test: diff --git a/.gitea/workflows/test_build.yaml b/.gitea/workflows/test_build.yaml new file mode 100644 index 0000000..456923d --- /dev/null +++ b/.gitea/workflows/test_build.yaml @@ -0,0 +1,24 @@ +name: Test project compilation + +on: + push: + branches-ignore: ["main"] + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + container: + image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2.1 + + steps: + - name: Checkout Code + uses: actions/checkout@v2 + - name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary) + run: which pio && id && pio run --target buildfs -e ESP32_INA233 + - name: Run PlatformIO Tests + run: pio test -e native + - name: Run PlatformIO build for INA233 + run: pio run -e ESP32_INA233 + - name: Run PlatformIO build for INA226 + run: pio run -e ESP32_INA226 From 8196ae4601bd8172474cfde4a7d018b43bbaa889 Mon Sep 17 00:00:00 2001 From: tobimai Date: Mon, 3 Nov 2025 17:53:30 +0100 Subject: [PATCH 13/16] pio config cleanup --- lib/fetchOTA/fetchOTA.cpp | 1 + platformio.ini | 41 ++++++++++++++++++--------------------- src/global_data/defines.h | 2 +- src/main.cpp | 4 ---- src/tools/tools.cpp | 4 ++-- 5 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/fetchOTA/fetchOTA.cpp b/lib/fetchOTA/fetchOTA.cpp index 8b08b54..5f14f99 100644 --- a/lib/fetchOTA/fetchOTA.cpp +++ b/lib/fetchOTA/fetchOTA.cpp @@ -5,6 +5,7 @@ #include "Arduino.h" #include #include +#include #include #include #include diff --git a/platformio.ini b/platformio.ini index 9902378..ef31b24 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,42 +11,39 @@ [platformio] default_envs = ESP32_INA233, ESP32_INA226 -[env:ESP32_INA233] +[env] platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.32/platform-espressif32.zip board = esp32dev framework = arduino monitor_speed = 115200 ; monitor_filters = esp32_exception_decoder -lib_deps = - esp32async/AsyncTCP @ ~3.4.9 - esp32async/ESPAsyncWebServer @ ~3.8.1 - robtillaart/INA226@ ~0.6.4 - bblanchon/ArduinoJson@^6.21.3 - x385832/Elog@~2.0.10 - INA233 - fetchOTA board_build.partitions = min_spiffs.csv +build_flags = -Wall -Wextra -DUSE_INA226 -fexceptions + +[common_esp32_libs] +lib_deps = + esp32async/AsyncTCP @ ~3.4.9 + esp32async/ESPAsyncWebServer @ ~3.8.1 + robtillaart/INA226@ ~0.6.4 + bblanchon/ArduinoJson@^6.21.3 + x385832/Elog@~2.0.10 + fetchOTA + +[env:ESP32_INA233] +extends = common_esp32_libs +lib_deps = + ${common_esp32_libs.lib_deps} + INA233 upload_protocol = espota upload_port = 192.168.5.205 -build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE -DELOG_SYSLOG_ENABLE -fexceptions [env:ESP32_INA226] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.32/platform-espressif32.zip -board = esp32dev -framework = arduino -monitor_speed = 115200 -; monitor_filters = esp32_exception_decoder +extends = common_esp32_libs lib_deps = - esp32async/AsyncTCP @ ~3.4.9 - esp32async/ESPAsyncWebServer @ ~3.8.1 + ${common_esp32_libs.lib_deps} robtillaart/INA226@ ~0.6.4 - bblanchon/ArduinoJson@^6.21.3 - x385832/Elog@~2.0.10 - fetchOTA -board_build.partitions = min_spiffs.csv upload_protocol = espota upload_port = 192.168.6.45 -build_flags = -Wall -Wextra -DUSE_INA226 -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE -DELOG_SYSLOG_ENABLE -fexceptions [env:native] platform = native diff --git a/src/global_data/defines.h b/src/global_data/defines.h index 4401756..308030c 100644 --- a/src/global_data/defines.h +++ b/src/global_data/defines.h @@ -7,7 +7,7 @@ #define water_level_min_key "water_level_min" #define water_level_max_key "water_level_max" #define water_volume_key "water_volume" -#define current_software_version Version{1, 3, 0} +#define current_software_version Version{2, 0, 0} #define REQUIRED_SPIFFS_VERSION Version{8, 0, 0} #define RESISTOR_VALUE 4 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0fcad25..ffa18e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -203,10 +203,6 @@ void setup() xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL); xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); - esp_netif_t* eth = esp_netif_get_handle_from_ifkey("ETH_DEF"); - esp_netif_t* wifi = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); - - if (ETH.linkUp()){ LOG(ELOG_LEVEL_DEBUG, "Ethernet connected, starting update checker"); xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index cf021c0..8df1dc5 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -65,9 +65,9 @@ void check_update_task(void* parameter) { ota_status.current_version = current_software_version; ota_status.update_progress = -1; #ifdef USE_INA226 - OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA226"); + OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA226REV2"); #else - OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA233"); + OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA233REV2"); #endif OTA spiffs_fs("https://iot.tobiasmaier.me/filesystem/waterlevel", REQUIRED_SPIFFS_VERSION, "generic"); From 4f02a8f3d0f189d853df0251d9581dc84427d11c Mon Sep 17 00:00:00 2001 From: tobimai Date: Tue, 4 Nov 2025 21:09:40 +0100 Subject: [PATCH 14/16] fixed time --- data/logic.js | 1 + data/status.html | 8 ++- lib/INA233/INA233.h | 32 ++++----- platformio.ini | 15 ++-- src/external_interfacing/leds.cpp | 1 + src/global_data/global_data.h | 1 + src/main.cpp | 112 +++++++----------------------- src/networking/json_builder.cpp | 1 + src/networking/networking.cpp | 2 +- src/sensor/sensor.cpp | 1 + src/telemetry/telemetry.cpp | 5 ++ src/tools/log.h | 2 +- src/tools/tools.cpp | 62 +++++++++++++---- src/tools/tools.h | 6 +- 14 files changed, 120 insertions(+), 129 deletions(-) diff --git a/data/logic.js b/data/logic.js index 905077a..11f863a 100644 --- a/data/logic.js +++ b/data/logic.js @@ -25,6 +25,7 @@ function fetchTelemetryData() { .then(data => { document.getElementById('uptime').textContent = data.uptime_seconds ? (roundToTwo(data.uptime_seconds) + ' s') : 'XXX'; document.getElementById('heap').textContent = data.heap_percent ? (roundToTwo(data.heap_percent) + ' %%') : 'XXX'; + document.getElementById('temperature').textContent = data.heap_percent ? (roundToTwo(data.temperature) + ' °C') : 'XXX'; }) .catch(error => { console.error("There was an error fetching data from the API", error); diff --git a/data/status.html b/data/status.html index 2620fbe..1c06fc6 100644 --- a/data/status.html +++ b/data/status.html @@ -1,4 +1,5 @@ + @@ -19,9 +20,6 @@ Data export -
@@ -121,6 +119,10 @@ Heap usage: XXX + + CPU Temperature: + XXX +
diff --git a/lib/INA233/INA233.h b/lib/INA233/INA233.h index 8e17802..4f087c9 100644 --- a/lib/INA233/INA233.h +++ b/lib/INA233/INA233.h @@ -10,25 +10,25 @@ #define REGISTER_READ_IIN 0x89 enum AveragingMode { - averages_1 = B000, - averages_4 = B001, - averages_16 = B010, - averages_64 = B011, - averages_128 = B100, - averages_256 = B101, - averages_512 = B110, - averages_1024 = B111 + averages_1 = 0b000, + averages_4 = 0b001, + averages_16 = 0b010, + averages_64 = 0b011, + averages_128 = 0b100, + averages_256 = 0b101, + averages_512 = 0b110, + averages_1024 = 0b111 }; enum ConversionTime { - conversion_time_140uS = B000, - conversion_time_204uS = B001, - conversion_time_332us = B010, - conversion_time_588uS = B011, - conversion_time_1100uS = B100, - conversion_time_2116uS = B101, - conversion_time_4156uS = B110, - conversion_time_8244uS = B111 + conversion_time_140uS = 0b000, + conversion_time_204uS = 0b001, + conversion_time_332us = 0b010, + conversion_time_588uS = 0b011, + conversion_time_1100uS = 0b100, + conversion_time_2116uS = 0b101, + conversion_time_4156uS = 0b110, + conversion_time_8244uS = 0b111 }; class INA233{ diff --git a/platformio.ini b/platformio.ini index ef31b24..65dbd05 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,11 +16,8 @@ platform = https://github.com/pioarduino/platform-espressif32/releases/download/ board = esp32dev framework = arduino monitor_speed = 115200 -; monitor_filters = esp32_exception_decoder +monitor_filters = esp32_exception_decoder board_build.partitions = min_spiffs.csv -build_flags = -Wall -Wextra -DUSE_INA226 -fexceptions - -[common_esp32_libs] lib_deps = esp32async/AsyncTCP @ ~3.4.9 esp32async/ESPAsyncWebServer @ ~3.8.1 @@ -28,22 +25,24 @@ lib_deps = bblanchon/ArduinoJson@^6.21.3 x385832/Elog@~2.0.10 fetchOTA +build_flags = -fexceptions +build_src_flags = -Wall -Wextra + [env:ESP32_INA233] -extends = common_esp32_libs lib_deps = - ${common_esp32_libs.lib_deps} + ${env.lib_deps} INA233 upload_protocol = espota upload_port = 192.168.5.205 [env:ESP32_INA226] -extends = common_esp32_libs lib_deps = - ${common_esp32_libs.lib_deps} + ${env.lib_deps} robtillaart/INA226@ ~0.6.4 upload_protocol = espota upload_port = 192.168.6.45 +build_flags = ${env.build_flags} -DUSE_INA226 [env:native] platform = native diff --git a/src/external_interfacing/leds.cpp b/src/external_interfacing/leds.cpp index f6e1e0a..3d638b5 100644 --- a/src/external_interfacing/leds.cpp +++ b/src/external_interfacing/leds.cpp @@ -9,6 +9,7 @@ extern WaterData water_data; void display_task(void* parameter) { + LOG(ELOG_LEVEL_DEBUG, "Starting display tasks"); while (true) { if (!is_error(active_errors)) { // We have no error, refresh status display and wait half a second diff --git a/src/global_data/global_data.h b/src/global_data/global_data.h index f3288d0..ea6cc09 100644 --- a/src/global_data/global_data.h +++ b/src/global_data/global_data.h @@ -29,6 +29,7 @@ struct NetworkData { struct DeviceTelemetry { float heap_used_percent; int uptime_seconds; + float temperature; }; struct ActiveErrors { diff --git a/src/main.cpp b/src/main.cpp index ffa18e2..2e35014 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,48 +39,41 @@ extern NetworkData ethernet_data; extern SensorData shunt_data; extern ActiveErrors active_errors; -extern "C" int rom_phy_get_vdd33(); - Version current_spiffs_version; AsyncWebServer server(80); -// AsyncWebSocket webSocket("/webSocket"); +AsyncWebSocket webSocket("/webSocket"); #define FORMAT_LITTLEFS_IF_FAILED true void setup() { 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"); prefs.begin("waterlevel", false); Serial.begin(115200); - delay(500); + LOG(ELOG_LEVEL_DEBUG, "Init Sensor"); init_sensor(); - LOG(ELOG_LEVEL_DEBUG, "Beginning SPIFFS"); - //SPIFFS.begin(FORMAT_LITTLEFS_IF_FAILED); + LOG(ELOG_LEVEL_DEBUG, "Beginning LittleFS"); LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED); // Read the current SPIFFS version from the versions file on the spiffs + LOG(ELOG_LEVEL_DEBUG, "Reading LittleFS version"); File file = LittleFS.open("/version", FILE_READ); if (!file) { - Serial.println("Failed to open version file for reading"); + LOG( ELOG_LEVEL_ERROR, "Failed opening LittleFS version file"); } else { String version = file.readStringUntil('\n'); - LOG(ELOG_LEVEL_DEBUG, "Version: %s", version); current_spiffs_version = parseVersion(version.c_str()); - LOG(ELOG_LEVEL_DEBUG, "Current SPIFFS 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, "SPIFFS initialized"); - - LOG(ELOG_LEVEL_DEBUG, "Begin INA"); - + LOG(ELOG_LEVEL_DEBUG, "LittleFS initialized"); /////////////////////////////// ROUTES /////////////////////////////// LOG(ELOG_LEVEL_DEBUG, "Route Setup"); @@ -98,8 +91,6 @@ void setup() // API stuff - internal server.on("/update_wifi_credentials", HTTP_POST, [](AsyncWebServerRequest* request) { - int params = request->params(); - // For settings SSID if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) { LOG(ELOG_LEVEL_DEBUG, "Updating SSID config"); @@ -134,12 +125,6 @@ void setup() float level_max_float = level_max_str.toFloat(); float liters_float = liters_str.toFloat(); - const char* paramCStr = range_str.c_str(); - char* endPtr; - - // Convert the C string to a float using strtod - float value = strtod(paramCStr, &endPtr); - LOG(ELOG_LEVEL_DEBUG, "range_float:%D:", range_float); prefs.putFloat(level_sensor_range_key, range_float); @@ -166,10 +151,8 @@ void setup() }); setup_api_endpoints(); - // webSocket.onEvent(onWsEvent); - // server.addHandler(&webSocket); - - + 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); }); @@ -178,14 +161,14 @@ void setup() LOG(ELOG_LEVEL_DEBUG, "OTA Setup"); ArduinoOTA .onStart([]() { - String type; - if (ArduinoOTA.getCommand() == U_FLASH) - type = "sketch"; - else // U_SPIFFS - type = "filesystem"; + 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); }) + // 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) { @@ -196,63 +179,20 @@ void setup() else if (error == OTA_RECEIVE_ERROR) LOG(ELOG_LEVEL_DEBUG, "Receive Failed"); else if (error == OTA_END_ERROR) LOG(ELOG_LEVEL_DEBUG, "End Failed"); }); - digitalWrite(LED_RED, 0); - - // Starting bootup sequence + LOG(ELOG_LEVEL_DEBUG, "Starting main tasks"); xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL); xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); - - if (ETH.linkUp()){ - LOG(ELOG_LEVEL_DEBUG, "Ethernet connected, starting update checker"); - xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); - } else { - LOG(ELOG_LEVEL_DEBUG, "Ethernet not connected, starting update checker and WiFi Task"); - // esp_netif_set_default_netif(esp_netif_get_handle_from_ifkey("ETH_DEF")); - xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); - delay(2000); - } - - LOG(ELOG_LEVEL_DEBUG, "Getting time now"); - // Configure time to UTC - configTime(0, 0, "pool.ntp.org"); - - // Wait until a valid time is obtained (time > 8 hours in seconds) - time_t now = time(NULL); - struct tm timeinfo; - int retry = 0; - while (now < 8 * 3600 && retry < 10) { - if(!getLocalTime(&timeinfo)){ - Serial.println("Failed to obtain time"); - } - Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); - retry++; - } - - - getLocalTime(&timeinfo); - - // Sanity check: Ensure the year is at least 2020. - int currentYear = timeinfo.tm_year + 1900; // tm_year is years since 1900 - if (currentYear < 2020) { - LOG(ELOG_LEVEL_DEBUG, "Time not set properly: "); - } else { - LOG(ELOG_LEVEL_DEBUG, "Time is valid: %s", asctime(&timeinfo)); - } - - // Setup syslog - LOG(ELOG_LEVEL_ERROR, "Here is an error message, error code: %d", 17); - - - - - LOG(ELOG_LEVEL_DEBUG, "Starting webserver"); - server.begin(); - ArduinoOTA.begin(); - + // xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, 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); + + LOG(ELOG_LEVEL_DEBUG, "Starting webserver"); + server.begin(); + LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler"); + ArduinoOTA.begin(); } diff --git a/src/networking/json_builder.cpp b/src/networking/json_builder.cpp index 309d1d0..55705ab 100644 --- a/src/networking/json_builder.cpp +++ b/src/networking/json_builder.cpp @@ -27,6 +27,7 @@ StaticJsonDocument<128> build_telemetry_json(DeviceTelemetry data) { StaticJsonDocument<128> doc; doc["uptime_seconds"] = data.uptime_seconds; doc["heap_percent"] = data.heap_used_percent; + doc["temperature"] = data.temperature; return doc; } diff --git a/src/networking/networking.cpp b/src/networking/networking.cpp index a461700..e2e8e63 100644 --- a/src/networking/networking.cpp +++ b/src/networking/networking.cpp @@ -92,7 +92,7 @@ void wifi_task(void* parameter) void ethernet_task(void* parameter) { - LOG(ELOG_LEVEL_DEBUG, "Connecting Ethernet"); + LOG(ELOG_LEVEL_DEBUG, "Starting Ethernet Task"); ETH.begin(); ETH.setHostname(get_hostname(Ethernet)); while (true) { diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index 1927b14..2f42687 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -45,6 +45,7 @@ void init_sensor(){ void read_sensor_task(void* parameter) { + LOG(ELOG_LEVEL_DEBUG, "Starting read sensor tasks"); while (true) { // Get Values from sensor #ifndef USE_INA226 diff --git a/src/telemetry/telemetry.cpp b/src/telemetry/telemetry.cpp index 1fa7922..e870a77 100644 --- a/src/telemetry/telemetry.cpp +++ b/src/telemetry/telemetry.cpp @@ -1,17 +1,22 @@ #include "telemetry.h" #include #include "../global_data/global_data.h" +#include "tools/log.h" + +extern "C" uint8_t temprature_sens_read(); extern DeviceTelemetry telemetry; void collect_internal_telemetry_task(void* parameter) { + LOG(ELOG_LEVEL_DEBUG, "Starting internal telemetry tasks"); while (true) { float heap_usage = (float(ESP.getFreeHeap()) / float(ESP.getHeapSize())) * 100; uint64_t uptime_seconds = millis() / 1000; telemetry.heap_used_percent = heap_usage; telemetry.uptime_seconds = uptime_seconds; + telemetry.temperature = (temprature_sens_read()-32) / 1.8; delay(60000); } } \ No newline at end of file diff --git a/src/tools/log.h b/src/tools/log.h index ea0a3f7..35ee4fe 100644 --- a/src/tools/log.h +++ b/src/tools/log.h @@ -4,7 +4,7 @@ #define LOG(LEVEL, FMT, ...) \ do { \ - Logger.log(0, LEVEL, "[Core: %i][Task: %s][Fn: %s]" FMT, \ + Logger.log(0, LEVEL, "[Core: %i][Task: %s][Fn: %s] " FMT, \ xPortGetCoreID(), \ pcTaskGetName(xTaskGetCurrentTaskHandle()), \ __FUNCTION__, \ diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index 8df1dc5..cb67833 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -6,10 +6,12 @@ #include #include #include "log.h" +#include "esp_sntp.h" + extern Preferences prefs; extern OTAStatus ota_status; -// extern AsyncWebSocket webSocket; +extern AsyncWebSocket webSocket; extern Version current_spiffs_version; @@ -43,7 +45,7 @@ void update_started() { void update_finished() { LOG(ELOG_LEVEL_DEBUG, "OTA Update finished"); ota_status.update_progress = -1; - // webSocket.textAll(String(-1).c_str()); + webSocket.textAll(String(-1).c_str()); } void update_progress(int cur, int total) { @@ -52,7 +54,7 @@ void update_progress(int cur, int total) { ota_status.update_progress = int(float(cur)/float(total)*100); LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress); } - // webSocket.textAll(String(ota_status.update_progress).c_str()); + webSocket.textAll(String(ota_status.update_progress).c_str()); LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d", cur, total); } @@ -62,6 +64,7 @@ void update_error(int err) { } void check_update_task(void* parameter) { + LOG(ELOG_LEVEL_DEBUG, "Starting check Update Task"); ota_status.current_version = current_software_version; ota_status.update_progress = -1; #ifdef USE_INA226 @@ -127,14 +130,47 @@ void run_ota_update_task(void* parameter) { vTaskDelete(NULL); } -// void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, -// void *arg, uint8_t *data, size_t len) { -// if (type == WS_EVT_CONNECT) { -// Serial.println("WebSocket client connected"); -// } else if (type == WS_EVT_DISCONNECT) { -// Serial.println("WebSocket client disconnected"); -// } else if (type == WS_EVT_DATA) { -// // Optionally process data received from the client -// } -// } +void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, + void *arg, uint8_t *data, size_t len) { + if (type == WS_EVT_CONNECT) { + Serial.println("WebSocket client connected"); + } else if (type == WS_EVT_DISCONNECT) { + Serial.println("WebSocket client disconnected"); + } else if (type == WS_EVT_DATA) { + // Optionally process data received from the client + } +} +void get_time_task(void* parameter) { + LOG(ELOG_LEVEL_DEBUG, "Starting GetTimeTask"); + LOG(ELOG_LEVEL_DEBUG, "Trying to get time from Internet"); + + sntp_set_time_sync_notification_cb(onTimeSync); + sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH); + sntp_set_sync_interval(1000 * 60 * 60); //re-sync every hour + configTzTime("UTC0", "pool.ntp.org", "1.europe.pool.ntp.org", " time.Windows.com"); + waitForTime(); + vTaskDelete(NULL); +} + +// Time stuff + +// Handler to handle successful time sync +void onTimeSync(struct timeval* tv) { + struct tm timeinfo; + getLocalTime(&timeinfo); + LOG(ELOG_LEVEL_DEBUG, "Time synchronized. New Time: %s", asctime(&timeinfo)); +} + +// wait for succesful time sync +bool waitForTime() { + time_t now; + do { + time(&now); + if (now > 8 * 3600) break; + delay(500); + LOG(ELOG_LEVEL_WARNING, "No valid time."); + } while (true); + LOG(ELOG_LEVEL_DEBUG, "Found valid time."); + return true; +} \ No newline at end of file diff --git a/src/tools/tools.h b/src/tools/tools.h index 839a48f..62af982 100644 --- a/src/tools/tools.h +++ b/src/tools/tools.h @@ -10,7 +10,11 @@ bool is_error(ActiveErrors active_errors); String processor(const String& var); void check_update_task(void* parameter); void run_ota_update_task(void* parameter); -// void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,void *arg, uint8_t *data, size_t len); +void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,void *arg, uint8_t *data, size_t len); +void get_time_task(void* parameter); + +void onTimeSync(struct timeval* tv); +bool waitForTime(); typedef struct { OTAStatus ota_status; From 462c5f3db7fb5b559d12937c6427d2fcc25b21b4 Mon Sep 17 00:00:00 2001 From: tobimai Date: Wed, 5 Nov 2025 18:56:53 +0100 Subject: [PATCH 15/16] Updated version --- .vscode/settings.json | 3 +- data/version | 2 +- platformio.ini | 2 +- src/global_data/defines.h | 4 +- src/main.cpp | 4 +- src/networking/networking.cpp | 26 +++++--- src/tools/tools.cpp | 119 +++++++++++++++++++++------------- src/tools/tools.h | 3 + 8 files changed, 102 insertions(+), 61 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a8b8076..2fc0cce 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,6 +21,7 @@ "span": "cpp", "text_encoding": "cpp", "thread": "cpp", - "*.inc": "cpp" + "*.inc": "cpp", + "cstddef": "cpp" } } \ No newline at end of file diff --git a/data/version b/data/version index 301160a..f11c82a 100644 --- a/data/version +++ b/data/version @@ -1 +1 @@ -8 \ No newline at end of file +9 \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 65dbd05..03cc4d3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -41,7 +41,7 @@ lib_deps = ${env.lib_deps} robtillaart/INA226@ ~0.6.4 upload_protocol = espota -upload_port = 192.168.6.45 +upload_port = 192.168.6.47 build_flags = ${env.build_flags} -DUSE_INA226 [env:native] diff --git a/src/global_data/defines.h b/src/global_data/defines.h index 308030c..c4f5cb7 100644 --- a/src/global_data/defines.h +++ b/src/global_data/defines.h @@ -7,7 +7,7 @@ #define water_level_min_key "water_level_min" #define water_level_max_key "water_level_max" #define water_volume_key "water_volume" -#define current_software_version Version{2, 0, 0} -#define REQUIRED_SPIFFS_VERSION Version{8, 0, 0} +#define current_software_version Version{2, 1, 0} +#define REQUIRED_SPIFFS_VERSION Version{9, 0, 0} #define RESISTOR_VALUE 4 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2e35014..bfd6baf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -183,12 +183,14 @@ void setup() LOG(ELOG_LEVEL_DEBUG, "Starting main tasks"); xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL); xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); - // xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, 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"); server.begin(); LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler"); diff --git a/src/networking/networking.cpp b/src/networking/networking.cpp index e2e8e63..205dd68 100644 --- a/src/networking/networking.cpp +++ b/src/networking/networking.cpp @@ -51,15 +51,15 @@ void wifi_task(void* parameter) if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) { wifi_data.link = false; if (failed_connection_attempts > 5) { - LOG(ELOG_LEVEL_DEBUG, "Failed to connect to currently saved SSID, starting SoftAP"); + LOG(ELOG_LEVEL_WARNING, "Failed to connect to currently saved SSID, starting SoftAP"); } else { - LOG(ELOG_LEVEL_DEBUG, "No SSID saved, starting SoftAP"); + LOG(ELOG_LEVEL_WARNING, "No SSID saved, starting SoftAP"); } String ap_ssid = get_hostname(Wireless); WiFi.softAP(ap_ssid.c_str(), ""); - LOG(ELOG_LEVEL_DEBUG, "[WIFI_TASK] Waiting for SSID now..."); + LOG(ELOG_LEVEL_DEBUG, "Waiting for SSID now..."); String old_ssid = prefs.getString(ssid_key, "xxx"); while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) { @@ -74,16 +74,16 @@ void wifi_task(void* parameter) wifi_data.link = true; wifi_data.network_name = WiFi.SSID(); wifi_data.ip_address = WiFi.localIP().toString(); - - LOG(ELOG_LEVEL_DEBUG, "RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID")); - Serial.println(WiFi.localIP()); - delay(5000); + 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); } else { - LOG(ELOG_LEVEL_DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); + String ssid = prefs.getString(ssid_key, ""); + String password = prefs.getString(wifi_password_key, ""); + LOG(ELOG_LEVEL_DEBUG, "Connecting to %s...", ssid); WiFi.mode(WIFI_STA); - - WiFi.begin(prefs.getString(ssid_key, "").c_str(), prefs.getString(wifi_password_key, "").c_str()); + WiFi.begin(ssid, password); failed_connection_attempts++; + LOG(ELOG_LEVEL_WARNING, "Failed to connect, retrying..."); delay(5000); } } @@ -100,6 +100,12 @@ void ethernet_task(void* parameter) ethernet_data.rssi = ETH.linkSpeed(); ethernet_data.ip_address = ETH.localIP().toString(); 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") { + LOG(ELOG_LEVEL_DEBUG, "Ethernet is up, setting to default"); + ETH.setDefault(); + } + delay(60 * 1000); } } \ No newline at end of file diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index cb67833..35eedd5 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -7,7 +7,13 @@ #include #include "log.h" #include "esp_sntp.h" +#include +#ifdef USE_INA226 + #define BOARD_VARIANT "INA226REV2" +#else + #define BOARD_VARIANT "INA233REV2" +#endif extern Preferences prefs; extern OTAStatus ota_status; @@ -67,61 +73,84 @@ void check_update_task(void* parameter) { LOG(ELOG_LEVEL_DEBUG, "Starting check Update Task"); ota_status.current_version = current_software_version; ota_status.update_progress = -1; - #ifdef USE_INA226 - OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA226REV2"); - #else - OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA233REV2"); - #endif - OTA spiffs_fs("https://iot.tobiasmaier.me/filesystem/waterlevel", REQUIRED_SPIFFS_VERSION, "generic"); - - - // Init SPIFFS update and check for update - // If there is a SPIFSS update it will be ran automatically, as SPIFFS is necessary for the firmware to run - Firmware latest_spiff_version = spiffs_fs.getLatestVersionOnServer(); - - LOG(ELOG_LEVEL_DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch); - - if (latest_spiff_version.valid) { - if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) { - // If Required SPIFFS version is newer than current version, update - LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now"); - run_ota_spiffs_update(latest_spiff_version.url, update_started, update_finished, update_progress, update_error); - // Reboot just to be safe - ESP.restart(); - } else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_spiff_version.version)) { - // If Server has new SPIFFS version but it's not required - LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); - } else { - LOG(ELOG_LEVEL_DEBUG, "No new SPIFFS version available"); - } - } + OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, BOARD_VARIANT); + OTA littlefs_ota("https://iot.tobiasmaier.me/filesystem/waterlevel", REQUIRED_SPIFFS_VERSION, "generic"); while (true) { - Firmware fw = ota.getLatestVersionOnServer(); - if (fw.valid) { - LOG(ELOG_LEVEL_DEBUG, "New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); - ota_status.latest_version = fw.version; - ota_status.update_url = fw.url; - if (isVersionNewer(current_software_version, fw.version)) { - LOG(ELOG_LEVEL_DEBUG, "Remote version is newer than current version"); - ota_status.update_available = true; - } else { - ota_status.update_available = false; - } + // Check if internet connection exists before running update, the web client seems to fail otherwise + + if (check_for_internet_connection()) { + LOG(ELOG_LEVEL_DEBUG, "Ping sucessful, starting update checks"); + check_and_update_littleFS(littlefs_ota); + check_and_update_firmware(ota); } else { - if (fw.version.major != 0 && fw.version.minor != 0 && fw.version.patch != 0) { - ota_status.latest_version = fw.version; - ota_status.update_available = false; - } - - LOG(ELOG_LEVEL_DEBUG, "No new firmware available"); + LOG(ELOG_LEVEL_WARNING, "Server did not respond to ping, waiting..."); } delay(1000 * 60 * 1); } } +void check_and_update_littleFS(OTA littlefs) { + Firmware latest_fs_version = littlefs.getLatestVersionOnServer(); + LOG(ELOG_LEVEL_DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_fs_version.version.major, latest_fs_version.version.minor, latest_fs_version.version.patch); + + if (latest_fs_version.valid) { + + // If we need new version and the server has a new 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"); + 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 + } else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_fs_version.version)) { + LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_fs_version.version.major, latest_fs_version.version.minor, latest_fs_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); + + // If we need a new version but server has no new version + } else if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) { + LOG(ELOG_LEVEL_ERROR, "New LittleFS Version is needed, but not found on server"); + + // Catch case for the rest + } else { + LOG(ELOG_LEVEL_DEBUG, "No new SPIFFS version available"); + } + } +} + +void check_and_update_firmware(OTA ota){ + Firmware fw = ota.getLatestVersionOnServer(); + if (fw.valid) { + LOG(ELOG_LEVEL_DEBUG, "Firmware available on server: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); + ota_status.latest_version = fw.version; + ota_status.update_url = fw.url; + if (isVersionNewer(current_software_version, fw.version)) { + LOG(ELOG_LEVEL_DEBUG, "Remote version is newer than current version"); + ota_status.update_available = true; + } else { + ota_status.update_available = false; + } + } else { + if (fw.version.major != 0 && fw.version.minor != 0 && fw.version.patch != 0) { + ota_status.latest_version = fw.version; + ota_status.update_available = false; + } + + LOG(ELOG_LEVEL_DEBUG, "No new firmware available"); + } +} + +bool check_for_internet_connection() { + HTTPClient http; + http.begin("https://iot.tobiasmaier.me"); + int code = http.sendRequest("HEAD"); + if (code > 0) { + return true; + } else { + return false; + } +} void run_ota_update_task(void* parameter) { TaskArgs_t *args = (TaskArgs_t *) parameter; diff --git a/src/tools/tools.h b/src/tools/tools.h index 62af982..969a7c8 100644 --- a/src/tools/tools.h +++ b/src/tools/tools.h @@ -15,6 +15,9 @@ void get_time_task(void* parameter); void onTimeSync(struct timeval* tv); bool waitForTime(); +void check_and_update_firmware(OTA fw); +void check_and_update_littleFS(OTA littlefs); +bool check_for_internet_connection(); typedef struct { OTAStatus ota_status; From 123f75dd899f61e43506c8429eccf9fcb5a1726e Mon Sep 17 00:00:00 2001 From: tobimai Date: Wed, 5 Nov 2025 19:07:19 +0100 Subject: [PATCH 16/16] fix --- platformio.ini | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index 03cc4d3..a62fb84 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ [platformio] default_envs = ESP32_INA233, ESP32_INA226 -[env] +[env:esp32_base] platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.32/platform-espressif32.zip board = esp32dev framework = arduino @@ -30,19 +30,21 @@ build_src_flags = -Wall -Wextra [env:ESP32_INA233] +extends = env:esp32_base lib_deps = - ${env.lib_deps} + ${env:esp32_base.lib_deps} INA233 upload_protocol = espota upload_port = 192.168.5.205 [env:ESP32_INA226] +extends = env:esp32_base lib_deps = - ${env.lib_deps} + ${env:esp32_base.lib_deps} robtillaart/INA226@ ~0.6.4 upload_protocol = espota upload_port = 192.168.6.47 -build_flags = ${env.build_flags} -DUSE_INA226 +build_flags = ${env:esp32_base.build_flags} -DUSE_INA226 [env:native] platform = native @@ -50,3 +52,4 @@ build_flags = -DUNIT_TEST -Ilib/fetchOTA/ lib_deps = fetchOTA arduino-libraries/ArduinoHttpClient@^0.6.1 +