Inital version to support OTA

This commit is contained in:
2025-02-15 16:57:21 +01:00
parent 33fdae3fb2
commit e8b8e95c13
6 changed files with 40 additions and 35 deletions

View File

@@ -5,12 +5,10 @@
#include "Arduino.h"
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <vector>
#include <ArduinoLog.h>
#include <HTTPUpdate.h>
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<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");
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;
}
}

View File

@@ -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<void()> callback_started, std::function<void()> callback_finished, std::function<void(int, int)> callback_progress, std::function<void(int)> callback_error);
void update_started();
void update_finished();
void update_progress(int cur, int total);
void update_error(int err);
};
#endif

View File

@@ -5,6 +5,8 @@
#include <WString.h>
#endif
#pragma once
struct Version {
int major;
int minor;