This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -21,6 +21,7 @@
|
||||
"span": "cpp",
|
||||
"text_encoding": "cpp",
|
||||
"thread": "cpp",
|
||||
"*.inc": "cpp"
|
||||
"*.inc": "cpp",
|
||||
"cstddef": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
8
|
||||
9
|
||||
@@ -41,7 +41,7 @@ lib_deps =
|
||||
${env.lib_deps}
|
||||
robtillaart/INA226@ ~0.6.4
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.6.45
|
||||
upload_port = 192.168.6.47
|
||||
build_flags = ${env.build_flags} -DUSE_INA226
|
||||
|
||||
[env:native]
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#define water_level_min_key "water_level_min"
|
||||
#define water_level_max_key "water_level_max"
|
||||
#define water_volume_key "water_volume"
|
||||
#define current_software_version Version{2, 0, 0}
|
||||
#define REQUIRED_SPIFFS_VERSION Version{8, 0, 0}
|
||||
#define current_software_version Version{2, 1, 0}
|
||||
#define REQUIRED_SPIFFS_VERSION Version{9, 0, 0}
|
||||
|
||||
#define RESISTOR_VALUE 4
|
||||
@@ -183,12 +183,14 @@ void setup()
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting main tasks");
|
||||
xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL);
|
||||
xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL);
|
||||
// xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, NULL, 1, NULL);
|
||||
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL);
|
||||
xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL);
|
||||
xTaskCreate(get_time_task, "GetTimeTask", 1024 * 4, NULL, 1, NULL);
|
||||
|
||||
delay(5000);
|
||||
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
||||
server.begin();
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler");
|
||||
|
||||
@@ -51,15 +51,15 @@ void wifi_task(void* parameter)
|
||||
if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) {
|
||||
wifi_data.link = false;
|
||||
if (failed_connection_attempts > 5) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Failed to connect to currently saved SSID, starting SoftAP");
|
||||
LOG(ELOG_LEVEL_WARNING, "Failed to connect to currently saved SSID, starting SoftAP");
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "No SSID saved, starting SoftAP");
|
||||
LOG(ELOG_LEVEL_WARNING, "No SSID saved, starting SoftAP");
|
||||
}
|
||||
|
||||
String ap_ssid = get_hostname(Wireless);
|
||||
WiFi.softAP(ap_ssid.c_str(), "");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "[WIFI_TASK] Waiting for SSID now...");
|
||||
LOG(ELOG_LEVEL_DEBUG, "Waiting for SSID now...");
|
||||
|
||||
String old_ssid = prefs.getString(ssid_key, "xxx");
|
||||
while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) {
|
||||
@@ -74,16 +74,16 @@ void wifi_task(void* parameter)
|
||||
wifi_data.link = true;
|
||||
wifi_data.network_name = WiFi.SSID();
|
||||
wifi_data.ip_address = WiFi.localIP().toString();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID"));
|
||||
Serial.println(WiFi.localIP());
|
||||
delay(5000);
|
||||
LOG(ELOG_LEVEL_DEBUG, "WIFI connected; RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID"));
|
||||
delay(1000 * 60);
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, ""));
|
||||
String ssid = prefs.getString(ssid_key, "");
|
||||
String password = prefs.getString(wifi_password_key, "");
|
||||
LOG(ELOG_LEVEL_DEBUG, "Connecting to %s...", ssid);
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
WiFi.begin(prefs.getString(ssid_key, "").c_str(), prefs.getString(wifi_password_key, "").c_str());
|
||||
WiFi.begin(ssid, password);
|
||||
failed_connection_attempts++;
|
||||
LOG(ELOG_LEVEL_WARNING, "Failed to connect, retrying...");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
@@ -100,6 +100,12 @@ void ethernet_task(void* parameter)
|
||||
ethernet_data.rssi = ETH.linkSpeed();
|
||||
ethernet_data.ip_address = ETH.localIP().toString();
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ETH.localIP().toString(), String(ethernet_data.link));
|
||||
|
||||
if (ETH.linkUp() && !ETH.isDefault() && ETH.localIP().toString() != "0.0.0.0") {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet is up, setting to default");
|
||||
ETH.setDefault();
|
||||
}
|
||||
|
||||
delay(60 * 1000);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,13 @@
|
||||
#include <AsyncWebSocket.h>
|
||||
#include "log.h"
|
||||
#include "esp_sntp.h"
|
||||
#include <HTTPClient.h>
|
||||
|
||||
#ifdef USE_INA226
|
||||
#define BOARD_VARIANT "INA226REV2"
|
||||
#else
|
||||
#define BOARD_VARIANT "INA233REV2"
|
||||
#endif
|
||||
|
||||
extern Preferences prefs;
|
||||
extern OTAStatus ota_status;
|
||||
@@ -67,61 +73,84 @@ void check_update_task(void* parameter) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting check Update Task");
|
||||
ota_status.current_version = current_software_version;
|
||||
ota_status.update_progress = -1;
|
||||
#ifdef USE_INA226
|
||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA226REV2");
|
||||
#else
|
||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA233REV2");
|
||||
#endif
|
||||
OTA spiffs_fs("https://iot.tobiasmaier.me/filesystem/waterlevel", REQUIRED_SPIFFS_VERSION, "generic");
|
||||
|
||||
|
||||
// Init SPIFFS update and check for update
|
||||
// If there is a SPIFSS update it will be ran automatically, as SPIFFS is necessary for the firmware to run
|
||||
Firmware latest_spiff_version = spiffs_fs.getLatestVersionOnServer();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch);
|
||||
|
||||
if (latest_spiff_version.valid) {
|
||||
if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) {
|
||||
// If Required SPIFFS version is newer than current version, update
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now");
|
||||
run_ota_spiffs_update(latest_spiff_version.url, update_started, update_finished, update_progress, update_error);
|
||||
// Reboot just to be safe
|
||||
ESP.restart();
|
||||
} else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_spiff_version.version)) {
|
||||
// If Server has new SPIFFS version but it's not required
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch);
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "No new SPIFFS version available");
|
||||
}
|
||||
}
|
||||
|
||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, BOARD_VARIANT);
|
||||
OTA littlefs_ota("https://iot.tobiasmaier.me/filesystem/waterlevel", REQUIRED_SPIFFS_VERSION, "generic");
|
||||
|
||||
while (true) {
|
||||
Firmware fw = ota.getLatestVersionOnServer();
|
||||
if (fw.valid) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "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.latest_version = fw.version;
|
||||
ota_status.update_url = fw.url;
|
||||
if (isVersionNewer(current_software_version, fw.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Remote version is newer than current version");
|
||||
ota_status.update_available = true;
|
||||
} else {
|
||||
ota_status.update_available = false;
|
||||
}
|
||||
// Check if internet connection exists before running update, the web client seems to fail otherwise
|
||||
|
||||
if (check_for_internet_connection()) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ping sucessful, starting update checks");
|
||||
check_and_update_littleFS(littlefs_ota);
|
||||
check_and_update_firmware(ota);
|
||||
} 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(ELOG_LEVEL_DEBUG, "No new firmware available");
|
||||
LOG(ELOG_LEVEL_WARNING, "Server did not respond to ping, waiting...");
|
||||
}
|
||||
delay(1000 * 60 * 1);
|
||||
}
|
||||
}
|
||||
|
||||
void check_and_update_littleFS(OTA littlefs) {
|
||||
Firmware latest_fs_version = littlefs.getLatestVersionOnServer();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_fs_version.version.major, latest_fs_version.version.minor, latest_fs_version.version.patch);
|
||||
|
||||
if (latest_fs_version.valid) {
|
||||
|
||||
// If we need new version and the server has a new version
|
||||
if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION) && isVersionNewer(current_spiffs_version, latest_fs_version.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now");
|
||||
run_ota_spiffs_update(latest_fs_version.url, update_started, update_finished, update_progress, update_error);
|
||||
ESP.restart();
|
||||
|
||||
// If we do not need a new version but one is available on the server
|
||||
} else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_fs_version.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_fs_version.version.major, latest_fs_version.version.minor, latest_fs_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch);
|
||||
|
||||
// If we need a new version but server has no new version
|
||||
} else if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) {
|
||||
LOG(ELOG_LEVEL_ERROR, "New LittleFS Version is needed, but not found on server");
|
||||
|
||||
// Catch case for the rest
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "No new SPIFFS version available");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void check_and_update_firmware(OTA ota){
|
||||
Firmware fw = ota.getLatestVersionOnServer();
|
||||
if (fw.valid) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Firmware available on server: %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.latest_version = fw.version;
|
||||
ota_status.update_url = fw.url;
|
||||
if (isVersionNewer(current_software_version, fw.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Remote version is newer than current version");
|
||||
ota_status.update_available = true;
|
||||
} else {
|
||||
ota_status.update_available = false;
|
||||
}
|
||||
} 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(ELOG_LEVEL_DEBUG, "No new firmware available");
|
||||
}
|
||||
}
|
||||
|
||||
bool check_for_internet_connection() {
|
||||
HTTPClient http;
|
||||
http.begin("https://iot.tobiasmaier.me");
|
||||
int code = http.sendRequest("HEAD");
|
||||
if (code > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void run_ota_update_task(void* parameter) {
|
||||
TaskArgs_t *args = (TaskArgs_t *) parameter;
|
||||
|
||||
@@ -15,6 +15,9 @@ void get_time_task(void* parameter);
|
||||
|
||||
void onTimeSync(struct timeval* tv);
|
||||
bool waitForTime();
|
||||
void check_and_update_firmware(OTA fw);
|
||||
void check_and_update_littleFS(OTA littlefs);
|
||||
bool check_for_internet_connection();
|
||||
|
||||
typedef struct {
|
||||
OTAStatus ota_status;
|
||||
|
||||
Reference in New Issue
Block a user