First Version to be updateable over API
All checks were successful
Test compiling project / test (push) Successful in 2m34s

This commit is contained in:
2025-02-17 21:46:20 +01:00
parent 6c4dc144d8
commit 6cec8f0a11
7 changed files with 57 additions and 7 deletions

View File

@@ -83,7 +83,7 @@ Firmware OTA::createErrorResponse(const String& errorMsg) {
}; };
} }
void OTA::run_ota_update(String url, std::function<void()> callback_started, std::function<void()> callback_finished, std::function<void(int, int)> callback_progress, std::function<void(int)> callback_error) { void run_ota_update(String url, std::function<void()> callback_started, std::function<void()> callback_finished, std::function<void(int, int)> callback_progress, std::function<void(int)> callback_error) {
Log.verbose("Starting OTA upgrade"); Log.verbose("Starting OTA upgrade");
HTTPUpdate httpUpdate; HTTPUpdate httpUpdate;
httpUpdate.onStart(callback_started); httpUpdate.onStart(callback_started);

View File

@@ -7,6 +7,7 @@
#include <Arduino.h> #include <Arduino.h>
#endif #endif
#pragma once
class OTA { class OTA {
@@ -15,7 +16,7 @@ class OTA {
Firmware getLatestVersionOnServer(); Firmware getLatestVersionOnServer();
bool checkForUpdate(); bool checkForUpdate();
void run_ota_update(String url, std::function<void()> callback_started, std::function<void()> callback_finished, std::function<void(int, int)> callback_progress, std::function<void(int)> callback_error);
private: private:
bool _isHTTPS = false; bool _isHTTPS = false;
@@ -25,4 +26,6 @@ class OTA {
Firmware createErrorResponse(const String& errorMsg); Firmware createErrorResponse(const String& errorMsg);
}; };
void run_ota_update(String url, std::function<void()> callback_started, std::function<void()> callback_finished, std::function<void(int, int)> callback_progress, std::function<void(int)> callback_error);
#endif #endif

View File

@@ -7,6 +7,6 @@
#define water_level_min_key "water_level_min" #define water_level_min_key "water_level_min"
#define water_level_max_key "water_level_max" #define water_level_max_key "water_level_max"
#define water_volume_key "water_volume" #define water_volume_key "water_volume"
#define current_software_version Version{0, 0, 9} #define current_software_version Version{0, 0, 11}
#define RESISTOR_VALUE 4 #define RESISTOR_VALUE 4

View File

@@ -45,4 +45,5 @@ struct OTAStatus {
Version current_version; Version current_version;
Version latest_version; Version latest_version;
int update_progress; int update_progress;
String update_url;
}; };

View File

@@ -6,6 +6,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <ArduinoLog.h> #include <ArduinoLog.h>
#include "json_builder.h" #include "json_builder.h"
#include "../tools/tools.h"
extern WaterData water_data; extern WaterData water_data;
extern DeviceTelemetry telemetry; extern DeviceTelemetry telemetry;
@@ -42,5 +43,22 @@ void setup_api_endpoints(){
String output; String output;
serializeJson(build_ota_json(ota_status), output); serializeJson(build_ota_json(ota_status), output);
request->send(200, "application/json", output); request->send(200, "application/json", output);
}); });
server.on("/run_ota_update", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ota_status.update_progress > -1) {
request->send(200, "text/plain", "OTA Update already in progress");
return;
} else if (!ota_status.update_available) {
request->send(200, "text/plain", "No update available");
return;
}
static TaskArgs_t args = {
.ota_status = ota_status
};
xTaskCreate(run_ota_update_task, "RunOTAUpdate", 1024 * 8, (void *)&args, 1, NULL);
request->send(200, "text/plain", "OTA Update started");
});
} }

View File

@@ -53,7 +53,11 @@ void update_finished() {
} }
void update_progress(int cur, int total) { void update_progress(int cur, int total) {
ota_status.update_progress = total/cur; ota_status.update_progress = 0;
if (cur != 0 ) {
ota_status.update_progress = total/cur;
}
Log.verbose("OTA Update progress: %d/%d", cur, total); Log.verbose("OTA Update progress: %d/%d", cur, total);
} }
@@ -63,6 +67,8 @@ void update_error(int err) {
} }
void check_update_task(void* parameter) { void check_update_task(void* parameter) {
ota_status.current_version = current_software_version;
ota_status.update_progress = -1;
#ifdef USE_INA226 #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, "INA226");
#else #else
@@ -71,12 +77,27 @@ void check_update_task(void* parameter) {
while (true) { while (true) {
Firmware fw = ota.getLatestVersionOnServer(); Firmware fw = ota.getLatestVersionOnServer();
if (fw.valid) { if (fw.valid) {
Log.verbose("New firmware available: %d.%d.%d, current versio: %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.verbose("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.update_available = true; ota_status.update_available = true;
ota_status.latest_version = fw.version; ota_status.latest_version = fw.version;
ota_status.update_url = fw.url;
} else { } 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.verbose("No new firmware available"); Log.verbose("No new firmware available");
} }
delay(1000 * 60 * 1); delay(1000 * 60 * 1);
} }
} }
void run_ota_update_task(void* parameter) {
TaskArgs_t *args = (TaskArgs_t *) parameter;
Log.verbose("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);
}

View File

@@ -1,8 +1,15 @@
#include <Arduino.h> #include <Arduino.h>
#include "../global_data/global_data.h" #include "../global_data/global_data.h"
#include <fetchOTA.h>
void printSuffix(Print* _logOutput, int logLevel); void printSuffix(Print* _logOutput, int logLevel);
void print_prefix(Print* _logOutput, int logLevel); void print_prefix(Print* _logOutput, int logLevel);
bool is_error(ActiveErrors active_errors); bool is_error(ActiveErrors active_errors);
String processor(const String& var); String processor(const String& var);
void check_update_task(void* parameter); void check_update_task(void* parameter);
void run_ota_update_task(void* parameter);
typedef struct {
OTAStatus ota_status;
} TaskArgs_t;