add ElegantOTA to webserver

This commit is contained in:
lenvm 2023-11-22 23:12:38 +01:00
parent e5586e9339
commit ce47ffd452
3 changed files with 82 additions and 2 deletions

View file

@ -127,6 +127,9 @@ void setup() {
// Perform main program functions
void loop() {
// Over-the-air updates by ElegantOTA
ElegantOTA.loop();
// Input
receive_can(); // Receive CAN messages. Runs as fast as possible
#ifdef DUAL_CAN

View file

@ -3,6 +3,9 @@
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Measure OTA progress
unsigned long ota_progress_millis = 0;
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
@ -49,7 +52,11 @@ void init_webserver() {
[](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, processor); });
// Send a GET request to <ESP_IP>/update
server.on("/update", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(200, "text/plain", "OK"); });
server.on("/debug", HTTP_GET,
[](AsyncWebServerRequest* request) { request->send(200, "text/plain", "Debug: all OK."); });
// Initialize ElegantOTA
init_ElegantOTA();
// Start server
server.begin();
@ -102,6 +109,14 @@ void init_WiFi_STA(const char* ssid, const char* password) {
}
}
void init_ElegantOTA() {
ElegantOTA.begin(&server); // Start ElegantOTA
// ElegantOTA callbacks
ElegantOTA.onStart(onOTAStart);
ElegantOTA.onProgress(onOTAProgress);
ElegantOTA.onEnd(onOTAEnd);
}
String processor(const String& var) {
if (var == "PLACEHOLDER") {
String content = "";
@ -136,3 +151,27 @@ String processor(const String& var) {
}
return String();
}
void onOTAStart() {
// Log when OTA has started
Serial.println("OTA update started!");
// <Add your own code here>
}
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);
}
}
void onOTAEnd(bool success) {
// Log when OTA has finished
if (success) {
Serial.println("OTA update finished successfully!");
} else {
Serial.println("There was an error during OTA update!");
}
// <Add your own code here>
}

View file

@ -4,6 +4,7 @@
// Load Wi-Fi library
#include <WiFi.h>
#include "../../../USER_SETTINGS.h" // Needed for WiFi ssid and password
#include "../../lib/ayushsharma82-ElegantOTA/src/ElegantOTA.h"
#include "../../lib/me-no-dev-AsyncTCP/src/AsyncTCP.h"
#include "../../lib/me-no-dev-ESPAsyncWebServer/src/ESPAsyncWebServer.h"
#include "../config.h" // Needed for LED defines
@ -42,6 +43,15 @@ void init_WiFi_AP();
*/
void init_WiFi_STA(const char* ssid, const char* password);
/**
* @brief Initialization function for ElegantOTA.
*
* @param[in] void
*
* @return void
*/
void init_ElegantOTA();
/**
* @brief Replaces placeholder with content section in web page
*
@ -51,4 +61,32 @@ void init_WiFi_STA(const char* ssid, const char* password);
*/
String processor(const String& var);
/**
* @brief Executes on OTA start
*
* @param[in] void
*
* @return void
*/
void onOTAStart();
/**
* @brief Executes on OTA progress
*
* @param[in] current Current bytes
* @param[in] final Final bytes
*
* @return void
*/
void onOTAProgress(size_t current, size_t final);
/**
* @brief Executes on OTA end
*
* @param[in] void
*
* @return bool success: success = true, failed = false
*/
void onOTAEnd(bool success);
#endif