Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Cabooman committed Feb 12, 2024
1 parent 8f61883 commit db1e679
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Software/src/devboard/utils/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void init_events(void) {
events.entries[EVENT_SERIAL_RX_FAILURE].level = EVENT_LEVEL_ERROR;
events.entries[EVENT_SERIAL_TX_FAILURE].level = EVENT_LEVEL_ERROR;
events.entries[EVENT_SERIAL_TRANSMITTER_FAILURE].level = EVENT_LEVEL_ERROR;
events.entries[EVENT_OTA_UPDATE_TIMEOUT].level = EVENT_LEVEL_INFO;

events.entries[EVENT_DUMMY_INFO].log = true;
events.entries[EVENT_DUMMY_DEBUG].log = true;
Expand Down Expand Up @@ -213,6 +214,8 @@ const char* get_event_message_string(EVENTS_ENUM_TYPE event) {
return "Error in serial function: Some ERROR level fault in transmitter, received by receiver";
case EVENT_OTA_UPDATE:
return "OTA update started!";
case EVENT_OTA_UPDATE_TIMEOUT:
return "OTA update timed out!";
default:
return "";
}
Expand Down
1 change: 1 addition & 0 deletions Software/src/devboard/utils/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
XX(EVENT_CELL_DEVIATION_HIGH) \
XX(EVENT_UNKNOWN_EVENT_SET) \
XX(EVENT_OTA_UPDATE) \
XX(EVENT_OTA_UPDATE_TIMEOUT) \
XX(EVENT_DUMMY_INFO) \
XX(EVENT_DUMMY_DEBUG) \
XX(EVENT_DUMMY_WARNING) \
Expand Down
23 changes: 23 additions & 0 deletions Software/src/devboard/webserver/webserver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "webserver.h"
#include <Preferences.h>
#include "../utils/events.h"
#include "../utils/timer.h"

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
Expand All @@ -21,6 +22,9 @@ enum WifiState {

WifiState wifi_state = INIT;

MyTimer ota_timeout_timer = MyTimer(5000);
bool ota_active = false;

unsigned const long WIFI_MONITOR_INTERVAL_TIME = 15000;
unsigned const long INIT_WIFI_CONNECT_TIMEOUT = 8000; // Timeout for initial WiFi connect in milliseconds
unsigned const long DEFAULT_WIFI_RECONNECT_INTERVAL = 1000; // Default WiFi reconnect interval in ms
Expand Down Expand Up @@ -299,6 +303,13 @@ void wifi_monitor() {
Serial.println(" Hostname: " + String(WiFi.getHostname()));
}
}

if (ota_active && ota_timeout_timer.elapsed()) {
// OTA timeout, try to restore can and clear the update event
ESP32Can.CANInit();
clear_event(EVENT_OTA_UPDATE);
set_event(EVENT_OTA_UPDATE_TIMEOUT, 0);
}
}

void init_WiFi_STA(const char* ssid, const char* password, const uint8_t wifi_channel) {
Expand Down Expand Up @@ -635,13 +646,21 @@ void onOTAStart() {
// Log when OTA has started
ESP32Can.CANStop();
set_event(EVENT_OTA_UPDATE, 0);

// If already set, make a new attempt
clear_event(EVENT_OTA_UPDATE_TIMEOUT);
ota_active = true;
ota_timeout_timer.reset();
}

void onOTAProgress(size_t current, size_t final) {
// Log every 1 second
if (millis() - ota_progress_millis > 1000) {
ota_progress_millis = millis();
Serial.printf("OTA Progress Current: %u bytes, Final: %u bytes\n", current, final);

// Reset the "watchdog"
ota_timeout_timer.reset();
}
}

Expand All @@ -651,7 +670,11 @@ void onOTAEnd(bool success) {
Serial.println("OTA update finished successfully!");
} else {
Serial.println("There was an error during OTA update!");

// If we fail without a timeout, try to restore CAN
ESP32Can.CANInit();
}
ota_active = false;
clear_event(EVENT_OTA_UPDATE);
}

Expand Down

0 comments on commit db1e679

Please sign in to comment.