mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-04 10:19:29 +02:00
add ElegantOTA to webserver
This commit is contained in:
parent
e5586e9339
commit
ce47ffd452
3 changed files with 82 additions and 2 deletions
|
@ -127,6 +127,9 @@ void setup() {
|
||||||
|
|
||||||
// Perform main program functions
|
// Perform main program functions
|
||||||
void loop() {
|
void loop() {
|
||||||
|
// Over-the-air updates by ElegantOTA
|
||||||
|
ElegantOTA.loop();
|
||||||
|
|
||||||
// Input
|
// Input
|
||||||
receive_can(); // Receive CAN messages. Runs as fast as possible
|
receive_can(); // Receive CAN messages. Runs as fast as possible
|
||||||
#ifdef DUAL_CAN
|
#ifdef DUAL_CAN
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
// Create AsyncWebServer object on port 80
|
// Create AsyncWebServer object on port 80
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
|
// Measure OTA progress
|
||||||
|
unsigned long ota_progress_millis = 0;
|
||||||
|
|
||||||
const char index_html[] PROGMEM = R"rawliteral(
|
const char index_html[] PROGMEM = R"rawliteral(
|
||||||
<!DOCTYPE HTML><html>
|
<!DOCTYPE HTML><html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -49,7 +52,11 @@ void init_webserver() {
|
||||||
[](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, processor); });
|
[](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, processor); });
|
||||||
|
|
||||||
// Send a GET request to <ESP_IP>/update
|
// 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
|
// Start server
|
||||||
server.begin();
|
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) {
|
String processor(const String& var) {
|
||||||
if (var == "PLACEHOLDER") {
|
if (var == "PLACEHOLDER") {
|
||||||
String content = "";
|
String content = "";
|
||||||
|
@ -136,3 +151,27 @@ String processor(const String& var) {
|
||||||
}
|
}
|
||||||
return String();
|
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>
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// Load Wi-Fi library
|
// Load Wi-Fi library
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include "../../../USER_SETTINGS.h" // Needed for WiFi ssid and password
|
#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-AsyncTCP/src/AsyncTCP.h"
|
||||||
#include "../../lib/me-no-dev-ESPAsyncWebServer/src/ESPAsyncWebServer.h"
|
#include "../../lib/me-no-dev-ESPAsyncWebServer/src/ESPAsyncWebServer.h"
|
||||||
#include "../config.h" // Needed for LED defines
|
#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);
|
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
|
* @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);
|
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
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue