Inital version to support OTA
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <WString.h>
|
||||
#endif
|
||||
|
||||
#pragma once
|
||||
|
||||
struct Version {
|
||||
int major;
|
||||
int minor;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
// Define keys to prevent typos
|
||||
#include <utils.h>
|
||||
|
||||
#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
|
||||
@@ -7,4 +7,6 @@ DeviceTelemetry telemetry;
|
||||
SensorData shunt_data;
|
||||
WaterData water_data;
|
||||
|
||||
OTAStatus ota_status;
|
||||
|
||||
ActiveErrors active_errors = { false, false, false, false, false, false };
|
||||
16
src/main.cpp
16
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");
|
||||
|
||||
Reference in New Issue
Block a user