diff --git a/include/WebOTA.h b/include/WebOTA.h new file mode 100644 index 0000000..0578e8e --- /dev/null +++ b/include/WebOTA.h @@ -0,0 +1,6 @@ +class WebOTA { +public: + WebOTA(int port); + void setup(); + void loop(); +}; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 36239a0..70bbbeb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,12 @@ platform = espressif32 board = esp32dev framework = arduino +[env:featheresp32 OTA] +platform = espressif32 +board = esp32dev +framework = arduino + ; OTA ; https://docs.platformio.org/en/latest/platforms/espressif8266.html#over-the-air-ota-update upload_protocol = espota -upload_port = Hexapod +upload_port = hexapod diff --git a/src/WebOTA.cpp b/src/WebOTA.cpp new file mode 100644 index 0000000..c18a6f0 --- /dev/null +++ b/src/WebOTA.cpp @@ -0,0 +1,69 @@ +/* --------------------------------------------- * + * Code from http://esp32-server.de/webupdate/ * + * --------------------------------------------- */ + +#include "WebOTA.h" +#include +#include +#include + +WebServer* server; + +const char* serverIndex = "\ +
\ + \ + \ +
"; + +WebOTA::WebOTA(int port) { + server = new WebServer(port); +} + +void WebOTA::setup() { + + auto rootHandlerFunc = []() { + server->sendHeader("Connection", "close"); + server->send(200, "text/html", serverIndex); + }; + auto updateHandlerFunc = []() { + server->sendHeader("Connection", "close"); + server->send(200, "text/plain", (Update.hasError()) ? "NOK" : "OK"); + delay(1000); + ESP.restart(); + }; + auto updateHandlerUFunc = [&]() { + HTTPUpload& upload = server->upload(); + if (upload.status == UPLOAD_FILE_START) { + Serial.setDebugOutput(true); + Serial.printf("Update: %s\n", upload.filename.c_str()); + uint32_t maxSketchSpace = (1048576 - 0x1000) & 0xFFFFF000; + if (!Update.begin(maxSketchSpace)) { // start with max available size + Update.printError(Serial); + } + } + else if (upload.status == UPLOAD_FILE_WRITE) { + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { + Update.printError(Serial); + } + } + else if (upload.status == UPLOAD_FILE_END) { + if (Update.end(true)) { // true to set the size to the current progress + Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); + } + else { + Update.printError(Serial); + } + Serial.setDebugOutput(false); + } + yield(); + }; + + server->on("/", HTTP_GET, rootHandlerFunc); + server->on("/update", HTTP_POST, updateHandlerFunc, updateHandlerUFunc); + server->begin(); + Serial.println("HTTP Server started"); +}; + +void WebOTA::loop() { + server->handleClient(); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5876527..89c36a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "FlashConfig.h" #include "OSCRemote.h" #include "Vbat.h" +#include "WebOTA.h" #include "body.h" #include "settings.h" #include @@ -11,6 +12,7 @@ Body body = Body::instance(); FlashConfig config; std::unique_ptr remote; Vbat vbat; +WebOTA webOTA(80); void servoTest() { for (uint8_t legnum = 0; legnum < body.legs.size(); legnum++) { @@ -116,6 +118,7 @@ void setup() { Serial.println("End Failed"); }); + webOTA.setup(); ArduinoOTA.begin(); } @@ -125,5 +128,6 @@ void loop() { remote->loop(); body.healthCheck(); + webOTA.loop(); ArduinoOTA.handle(); } \ No newline at end of file