Inital version to support OTA
This commit is contained in:
@@ -5,12 +5,10 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ArduinoLog.h>
|
#include <ArduinoLog.h>
|
||||||
#include <HTTPUpdate.h>
|
#include <HTTPUpdate.h>
|
||||||
|
|
||||||
|
|
||||||
OTA::OTA(String server_url, String currentVersion, String currentDeviceConfiguration) {
|
OTA::OTA(String server_url, String currentVersion, String currentDeviceConfiguration) {
|
||||||
_serverUrl = server_url;
|
_serverUrl = server_url;
|
||||||
_currentVersion = parseVersion(currentVersion.c_str());
|
_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");
|
Log.verbose("Starting OTA upgrade");
|
||||||
HTTPUpdate httpUpdate;
|
HTTPUpdate httpUpdate;
|
||||||
httpUpdate.onStart(update_started);
|
httpUpdate.onStart(callback_started);
|
||||||
httpUpdate.onEnd(update_finished);
|
httpUpdate.onEnd(callback_finished);
|
||||||
httpUpdate.onProgress(update_progress);
|
httpUpdate.onProgress(callback_progress);
|
||||||
httpUpdate.onError(update_error);
|
httpUpdate.onError(callback_error);
|
||||||
Serial.println("RUNNING OTA");
|
Log.verbose("Defined callbacks, Starting update now");
|
||||||
|
|
||||||
WiFiClientSecure client;
|
t_httpUpdate_return ret;
|
||||||
client.setInsecure();
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case HTTP_UPDATE_FAILED:
|
case HTTP_UPDATE_FAILED:
|
||||||
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
|
Log.error("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case HTTP_UPDATE_NO_UPDATES:
|
case HTTP_UPDATE_NO_UPDATES:
|
||||||
Serial.println("HTTP_UPDATE_NO_UPDATES");
|
Log.error("HTTP_UPDATE_NO_UPDATES");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case HTTP_UPDATE_OK:
|
case HTTP_UPDATE_OK:
|
||||||
Serial.println("HTTP_UPDATE_OK");
|
Log.verbose("Update done");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ class OTA {
|
|||||||
Version _currentVersion;
|
Version _currentVersion;
|
||||||
String _current_device_configuration;
|
String _current_device_configuration;
|
||||||
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);
|
||||||
|
void update_started();
|
||||||
|
void update_finished();
|
||||||
|
void update_progress(int cur, int total);
|
||||||
|
void update_error(int err);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <WString.h>
|
#include <WString.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
struct Version {
|
struct Version {
|
||||||
int major;
|
int major;
|
||||||
int minor;
|
int minor;
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
// Define keys to prevent typos
|
// Define keys to prevent typos
|
||||||
|
#include <utils.h>
|
||||||
|
|
||||||
#define ssid_key "ssid"
|
#define ssid_key "ssid"
|
||||||
#define wifi_password_key "wifi_password"
|
#define wifi_password_key "wifi_password"
|
||||||
#define level_sensor_range_key "sensor_range"
|
#define level_sensor_range_key "sensor_range"
|
||||||
#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_version Version{0, 0, 2}
|
#define current_software_version Version{0, 0, 3}
|
||||||
|
|
||||||
#define RESISTOR_VALUE 4
|
#define RESISTOR_VALUE 4
|
||||||
@@ -7,4 +7,6 @@ DeviceTelemetry telemetry;
|
|||||||
SensorData shunt_data;
|
SensorData shunt_data;
|
||||||
WaterData water_data;
|
WaterData water_data;
|
||||||
|
|
||||||
|
OTAStatus ota_status;
|
||||||
|
|
||||||
ActiveErrors active_errors = { false, false, false, false, false, false };
|
ActiveErrors active_errors = { false, false, false, false, false, false };
|
||||||
14
src/main.cpp
14
src/main.cpp
@@ -38,21 +38,7 @@ extern "C" int rom_phy_get_vdd33();
|
|||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
#define FORMAT_LITTLEFS_IF_FAILED true
|
#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) {
|
void run_ota(void* parameter) {
|
||||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", "1.1.1", "INA233");
|
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", "1.1.1", "INA233");
|
||||||
|
|||||||
Reference in New Issue
Block a user