From e589297db315cb6764ca530f609bf830f711e082 Mon Sep 17 00:00:00 2001 From: Alan <60433566+alanesq@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:46:18 +0100 Subject: [PATCH] this file is required if you wish to activate OTA updates in the sketch put it in the same folder as the main sketch and enable in settings (#define ENABLE_OTA 1) --- ota.h | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 ota.h diff --git a/ota.h b/ota.h new file mode 100644 index 0000000..a64e305 --- /dev/null +++ b/ota.h @@ -0,0 +1,204 @@ +/************************************************************************************************** + * + * Over The Air updates (OTA) - 02Aug23 + * MODIFIED FOR USE WITH ESP32CamDemo (no log, different header) + * + * part of the BasicWebserver sketch - https://github.com/alanesq/BasicWebserver + * + * If using an esp32cam module In Arduino IDE Select "ESP32 dev module" not "ESP32-cam" with PSRAM enabled + * + ************************************************************************************************** + + Make sure partition with OTA enabled is selected + + To enable/disable OTA updates see setting at top of main sketch (#define ENABLE_OTA 1) + + Then access with http:///ota + + + **************************************************************************************************/ + + +#if defined ESP32 + #include +#endif + + +// forward declarations (i.e. details of all functions in this file) + void otaSetup(); + void handleOTA(); + + +// some useful html/css + const char colRed[] = ""; // red text + const char colGreen[] = ""; // green text + const char colBlue[] = ""; // blue text + const char colEnd[] = ""; // end coloured text + const char htmlSpace[] = " "; // leave a space (see 'HTML entity') + + +// ---------------------------------------------------------------- +// -enable OTA +// ---------------------------------------------------------------- +// Enable OTA updates, called when correct password has been entered + +void otaSetup() { + + OTAEnabled = 1; // flag that OTA has been enabled + + // esp32 version (using webserver.h) + #if defined ESP32 + server.on("/update", HTTP_POST, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", (Update.hasError()) ? "Update Failed!, rebooting..." : "Update complete, rebooting..."); + delay(2000); + ESP.restart(); + delay(2000); + }, []() { + HTTPUpload& upload = server.upload(); + if (upload.status == UPLOAD_FILE_START) { + if (serialDebug) Serial.setDebugOutput(true); + if (serialDebug) Serial.printf("Update: %s\n", upload.filename.c_str()); + if (!Update.begin()) { //start with max available size + if (serialDebug) Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_WRITE) { + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { + if (serialDebug) Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_END) { + if (Update.end(true)) { //true to set the size to the current progress + if (serialDebug) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); + } else { + if (serialDebug) Update.printError(Serial); + } + if (serialDebug) Serial.setDebugOutput(false); + } else { + if (serialDebug) Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status); + } + }); + #endif + + // esp8266 version (using ESP8266WebServer.h) + #if defined ESP8266 + server.on("/update", HTTP_POST, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", (Update.hasError()) ? "Update Failed!, rebooting..." : "Update complete, rebooting..."); + delay(2000); + ESP.restart(); + delay(2000); + }, []() { + HTTPUpload& upload = server.upload(); + if (upload.status == UPLOAD_FILE_START) { + if (serialDebug) Serial.setDebugOutput(true); + WiFiUDP::stopAll(); + if (serialDebug) Serial.printf("Update: %s\n", upload.filename.c_str()); + uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; + if (!Update.begin(maxSketchSpace)) { //start with max available size + if (serialDebug) Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_WRITE) { + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { + if (serialDebug) Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_END) { + if (Update.end(true)) { //true to set the size to the current progress + if (serialDebug) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); + } else { + if (serialDebug) Update.printError(Serial); + } + if (serialDebug) Serial.setDebugOutput(false); + } + yield(); + }); + #endif + +} + + +// ---------------------------------------------------------------- +// -OTA web page requested i.e. http://x.x.x.x/ota +// ---------------------------------------------------------------- +// Request OTA password or implement OTA update if already entered + +void handleOTA(){ + + WiFiClient client = server.client(); // open link with client + + // check if valid password supplied + if (server.hasArg("pwd")) { + if (server.arg("pwd") == OTAPassword) otaSetup(); // Enable over The Air updates (OTA) + } + + + // ----------------------------------------- + + if (OTAEnabled == 0) { + + // OTA is not enabled so request password to enable it + + sendHeader(client, stitle); + + // This is the below javascript/html compacted to save flash memory via https://www.textfixer.com/html/compress-html-compression.php + client.print (R"=====(



Enter OTA password

Password:
)====="); + /* + client.print (R"=====( +
+ + +
+ + +

+ + + +
+
Enter OTA password

+
Password:
+
+ + )====="); + */ + + + sendFooter(client); // close web page + + } + + // ----------------------------------------- + + + if (OTAEnabled == 1) { // if OTA is enabled implement it + + sendHeader(client, stitle); + + client.write("

Update firmware


\n"); + client.printf("Current version = %s, %s \n\n", stitle, sversion); + + client.write("
\n"); + client.write("\n"); + client.write("


\n"); + + client.write("

Device will reboot when upload complete"); + client.printf("%s
To disable OTA restart device
%s \n", colRed, colEnd); + + sendFooter(client); // close web page + } + + // ----------------------------------------- + + + // close html page + delay(3); + client.stop(); + +} + + +// ---------------------------------------------- end ----------------------------------------------