From 6f9dc31b1ee4b2652a27bcaf9a2290a952fbdbd3 Mon Sep 17 00:00:00 2001 From: Philip Johansson Date: Fri, 12 Jun 2020 10:39:20 +0200 Subject: [PATCH] initial commit --- README.md | 1 + src/webota.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/webota.h | 20 +++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 README.md create mode 100644 src/webota.cpp create mode 100644 src/webota.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..be1d391 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +This lib is based on the Arduino example sketch "WebUpdate" \ No newline at end of file diff --git a/src/webota.cpp b/src/webota.cpp new file mode 100644 index 0000000..fec4aa2 --- /dev/null +++ b/src/webota.cpp @@ -0,0 +1,79 @@ +#include "webota.h" +#include "WiFiUdp.h" + +const char* serverIndex = + "
"; + +WebOTA::WebOTA(int port) +: _server(port) +, _wiFiClient(nullptr) +{} + +bool WebOTA::setup(ESP8266WiFiClass* wiFiClient) +{ + _wiFiClient = wiFiClient; + + if (_wiFiClient->status() == WL_CONNECTED) + { + _server.on("/", HTTP_GET, [&]() { + _server.sendHeader("Connection", "close"); + _server.send(200, "text/html", serverIndex); + }); + _server.on( + "/update", + HTTP_POST, + [&]() { + _server.sendHeader("Connection", "close"); + _server.send( + 200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); + ESP.restart(); + }, + [&]() { + HTTPUpload& upload = _server.upload(); + if (upload.status == UPLOAD_FILE_START) + { + Serial.setDebugOutput(true); + WiFiUDP::stopAll(); + 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 + 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.begin(); + return true; + + } else + { + Serial.println("WiFi Failed"); + } + return false; +} + +void WebOTA::run() +{ + _server.handleClient(); +} \ No newline at end of file diff --git a/src/webota.h b/src/webota.h new file mode 100644 index 0000000..4e0c6eb --- /dev/null +++ b/src/webota.h @@ -0,0 +1,20 @@ +#include +#include + +class WebOTA { +public: + //! @brief Create OTA server on default port 80 + WebOTA(); + WebOTA(int port); + + //! @brief Needs to be ran in "setup" stage + //! @param wiFiClient used to check for connection before initializing. A + //! WiFi connections needs to be established for the setup to succeed + //! @return true if successful + bool setup(ESP8266WiFiClass* wiFiClient); + void run(); + +private: + ESP8266WebServer _server; + ESP8266WiFiClass* _wiFiClient; +}; \ No newline at end of file