From cfc843804279bbc8e3d28dc7e2b8af49ca3830f1 Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Sun, 29 Mar 2026 17:46:41 +0200 Subject: [PATCH] Cleanup + Bugfix --- data/version | 2 +- lib/fetchOTA/fetchOTA.cpp | 21 ++++++++++++++++----- platformio.ini | 6 +++--- src/global_data/defines.h | 4 ++-- src/networking/responses.cpp | 2 +- src/sensor/sensor.cpp | 3 ++- src/tools/tools.cpp | 5 +++-- 7 files changed, 28 insertions(+), 15 deletions(-) diff --git a/data/version b/data/version index f11c82a..9a03714 100644 --- a/data/version +++ b/data/version @@ -1 +1 @@ -9 \ No newline at end of file +10 \ No newline at end of file diff --git a/lib/fetchOTA/fetchOTA.cpp b/lib/fetchOTA/fetchOTA.cpp index 5f14f99..c9c89bb 100644 --- a/lib/fetchOTA/fetchOTA.cpp +++ b/lib/fetchOTA/fetchOTA.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -97,11 +98,11 @@ void run_ota_update(String url, std::function callback_started, std::fu httpUpdate.onError(callback_error); 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")) { Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL"); - WiFiClient client; + WiFiClientSecure client; // client.setInsecure(); ret = httpUpdate.update(client, url); } else if (url.startsWith("http")) { @@ -110,15 +111,20 @@ void run_ota_update(String url, std::function callback_started, std::fu ret = httpUpdate.update(client, url); } else { Logger.log(0, ELOG_LEVEL_ERROR, "URL is not valid: \n%s", url.c_str()); + if (callback_error) callback_error(-1); + return; } switch (ret) { case HTTP_UPDATE_FAILED: 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; case HTTP_UPDATE_NO_UPDATES: Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES"); + if (callback_error) callback_error(-2); break; case HTTP_UPDATE_OK: @@ -136,12 +142,12 @@ void run_ota_spiffs_update(String url, std::function callback_started, httpUpdate.onError(callback_error); 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")) { Logger.log(0, ELOG_LEVEL_DEBUG, "HTTPS URL"); - WiFiClient client; - // client.setInsecure(); + WiFiClientSecure client; + client.setInsecure(); ret = httpUpdate.updateSpiffs(client, url); } else if (url.startsWith("http")) { Logger.log(0, ELOG_LEVEL_DEBUG, "HTTP URL"); @@ -149,15 +155,20 @@ void run_ota_spiffs_update(String url, std::function callback_started, ret = httpUpdate.updateSpiffs(client, url); } else { Logger.log(0, ELOG_LEVEL_ERROR, "URL is not valid: \n%s", url.c_str()); + if (callback_error) callback_error(-1); + return; } switch (ret) { case HTTP_UPDATE_FAILED: 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; case HTTP_UPDATE_NO_UPDATES: Logger.log(0, ELOG_LEVEL_ERROR, "HTTP_UPDATE_NO_UPDATES"); + if (callback_error) callback_error(-2); break; case HTTP_UPDATE_OK: diff --git a/platformio.ini b/platformio.ini index 3bc6ca7..3a83530 100644 --- a/platformio.ini +++ b/platformio.ini @@ -34,8 +34,8 @@ extends = env:esp32_base lib_deps = ${env:esp32_base.lib_deps} INA233 -upload_protocol = espota -upload_port = 192.168.5.205 +; upload_protocol = espota +; upload_port = 192.168.18.18 [env:ESP32_INA226] extends = env:esp32_base @@ -43,7 +43,7 @@ lib_deps = ${env:esp32_base.lib_deps} robtillaart/INA226@ ~0.6.4 upload_protocol = espota -upload_port = 192.168.6.45 +upload_port = 192.168.18.18 build_flags = ${env:esp32_base.build_flags} -DUSE_INA226 [env:native] diff --git a/src/global_data/defines.h b/src/global_data/defines.h index cb6eee8..e4365b2 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, 2, 0} -#define REQUIRED_SPIFFS_VERSION Version{9, 0, 0} +#define current_software_version Version{2, 5, 0} +#define REQUIRED_SPIFFS_VERSION Version{10, 0, 0} #define RESISTOR_VALUE 4 \ No newline at end of file diff --git a/src/networking/responses.cpp b/src/networking/responses.cpp index d9b0278..cf44ede 100644 --- a/src/networking/responses.cpp +++ b/src/networking/responses.cpp @@ -51,7 +51,7 @@ void setup_api_endpoints(){ request->send(200, "application/json", output); }); - server.on("/ota_udpate_status", HTTP_GET, [](AsyncWebServerRequest* request) { + server.on("/ota_update_status", HTTP_GET, [](AsyncWebServerRequest* request) { String output; serializeJson(build_ota_json(ota_status), output); request->send(200, "application/json", output); diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index 2f42687..6c2abca 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -33,6 +33,8 @@ void init_sensor(){ ina_sensor.setBusVoltageConversionTime(7); ina_sensor.setShuntVoltageConversionTime(7); ina_sensor.setAverage(4); + // 111 - Shunt + Bus continous + ina_sensor.setMode(7); #else ina_sensor.begin(33, 32); ina_sensor.reset(); @@ -52,7 +54,6 @@ void read_sensor_task(void* parameter) String chip_id = ina_sensor.get_device_model(); 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; diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index 35eedd5..5900441 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -49,9 +49,11 @@ void update_started() { } 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; webSocket.textAll(String(-1).c_str()); + delay(2000); + ESP.restart(); } void update_progress(int cur, int total) { @@ -102,7 +104,6 @@ void check_and_update_littleFS(OTA littlefs) { if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION) && isVersionNewer(current_spiffs_version, latest_fs_version.version)) { 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)) {