From e8b8e95c13e09694b71d3b28295af9e8830c3373 Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Sat, 15 Feb 2025 16:57:21 +0100 Subject: [PATCH] Inital version to support OTA --- lib/fetchOTA/fetchOTA.cpp | 46 +++++++++++++++++++-------------- lib/fetchOTA/fetchOTA.h | 5 ++++ lib/fetchOTA/utils.h | 2 ++ src/global_data/defines.h | 4 ++- src/global_data/global_data.cpp | 2 ++ src/main.cpp | 16 +----------- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/lib/fetchOTA/fetchOTA.cpp b/lib/fetchOTA/fetchOTA.cpp index b23d8fa..0b52acf 100644 --- a/lib/fetchOTA/fetchOTA.cpp +++ b/lib/fetchOTA/fetchOTA.cpp @@ -5,12 +5,10 @@ #include "Arduino.h" #include #include - #include #include #include - OTA::OTA(String server_url, String currentVersion, String currentDeviceConfiguration) { _serverUrl = server_url; _currentVersion = parseVersion(currentVersion.c_str()); @@ -73,32 +71,42 @@ Firmware OTA::createErrorResponse(const String& errorMsg) { }; } -void OTA::run_ota_update(String url) { +void OTA::run_ota_update(String url, std::function callback_started, std::function callback_finished, std::function callback_progress, std::function callback_error) { Log.verbose("Starting OTA upgrade"); HTTPUpdate httpUpdate; - httpUpdate.onStart(update_started); - httpUpdate.onEnd(update_finished); - httpUpdate.onProgress(update_progress); - httpUpdate.onError(update_error); - Serial.println("RUNNING OTA"); - - WiFiClientSecure client; - client.setInsecure(); - - t_httpUpdate_return ret = httpUpdate.update(client, "https://iot.tobiasmaier.me/firmware/waterlevel/INA233/1_1_1.bin"); - // t_httpUpdate_return ret = httpUpdate.update(client, "https://iot.tobiasmaier.me", 443, "/firmware/waterlevel/INA233/1_0_1.bin"); + httpUpdate.onStart(callback_started); + httpUpdate.onEnd(callback_finished); + httpUpdate.onProgress(callback_progress); + httpUpdate.onError(callback_error); + Log.verbose("Defined callbacks, Starting update now"); + + t_httpUpdate_return ret; + + if (url.startsWith("https")) { + Log.verbose("HTTPS URL"); + WiFiClientSecure client; + client.setInsecure(); + ret = httpUpdate.update(client, url); + } else if (url.startsWith("http")) { + Log.verbose("HTTP URL"); + WiFiClient client; + ret = httpUpdate.update(client, url); + } else { + Log.error("URL is not valid"); + } + switch (ret) { case HTTP_UPDATE_FAILED: - Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); - break; + Log.error("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); + break; case HTTP_UPDATE_NO_UPDATES: - Serial.println("HTTP_UPDATE_NO_UPDATES"); - break; + Log.error("HTTP_UPDATE_NO_UPDATES"); + break; case HTTP_UPDATE_OK: - Serial.println("HTTP_UPDATE_OK"); + Log.verbose("Update done"); break; } } diff --git a/lib/fetchOTA/fetchOTA.h b/lib/fetchOTA/fetchOTA.h index db6f7c1..1d635c3 100644 --- a/lib/fetchOTA/fetchOTA.h +++ b/lib/fetchOTA/fetchOTA.h @@ -22,6 +22,11 @@ class OTA { Version _currentVersion; String _current_device_configuration; Firmware 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); + void update_started(); + void update_finished(); + void update_progress(int cur, int total); + void update_error(int err); }; #endif \ No newline at end of file diff --git a/lib/fetchOTA/utils.h b/lib/fetchOTA/utils.h index 5b2416c..801c143 100644 --- a/lib/fetchOTA/utils.h +++ b/lib/fetchOTA/utils.h @@ -5,6 +5,8 @@ #include #endif +#pragma once + struct Version { int major; int minor; diff --git a/src/global_data/defines.h b/src/global_data/defines.h index c576e37..6ef85db 100644 --- a/src/global_data/defines.h +++ b/src/global_data/defines.h @@ -1,10 +1,12 @@ // Define keys to prevent typos +#include + #define ssid_key "ssid" #define wifi_password_key "wifi_password" #define level_sensor_range_key "sensor_range" #define water_level_min_key "water_level_min" #define water_level_max_key "water_level_max" #define water_volume_key "water_volume" -#define current_version Version{0, 0, 2} +#define current_software_version Version{0, 0, 3} #define RESISTOR_VALUE 4 \ No newline at end of file diff --git a/src/global_data/global_data.cpp b/src/global_data/global_data.cpp index aee4177..81ed409 100644 --- a/src/global_data/global_data.cpp +++ b/src/global_data/global_data.cpp @@ -7,4 +7,6 @@ DeviceTelemetry telemetry; SensorData shunt_data; WaterData water_data; +OTAStatus ota_status; + ActiveErrors active_errors = { false, false, false, false, false, false }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4aba952..7028fd9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,21 +38,7 @@ extern "C" int rom_phy_get_vdd33(); AsyncWebServer server(80); #define FORMAT_LITTLEFS_IF_FAILED true -void update_started() { - Serial.println("CALLBACK: HTTP update process started"); - } - - void update_finished() { - Serial.println("CALLBACK: HTTP update process finished"); - } - - void update_progress(int cur, int total) { - Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total); - } - - void update_error(int err) { - Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err); - } + void run_ota(void* parameter) { OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", "1.1.1", "INA233");