/* * SimpleUpdater.cpp * * ESP8266 / ESP32 Environmental Sensor * * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice * you can do whatever you want with this stuff. If we meet some day, and you * think this stuff is worth it, you can buy me a beer in return. Thomas Buck * ---------------------------------------------------------------------------- */ #if defined(ARDUINO_ARCH_ESP8266) #include #elif defined(ARDUINO_ARCH_ESP32) #include #endif #include "SimpleUpdater.h" #if defined(ARDUINO_ARCH_ESP32) void SimpleUpdater::get(void) { String uploadPage = F( "\n" "\n" "\n" "\n" "\n" "SimpleUpdater ESP32\n" "\n" "

SimpleUpdater

\n" "

Select the update file. If you have built this project with PlatformIO, you can find a firmware.bin in the .pio folder.

\n" "
\n" "\n" "\n" "

\n" "
progress: 0%
\n" "

After the update is finished, you will automatically be redirected to the main page.

\n" "Back to Main Page\n" "\n" "" ); server->send(200, "text/html", uploadPage); } void SimpleUpdater::postResult(void) { server->sendHeader("Connection", "close"); server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); ESP.restart(); } void SimpleUpdater::postUpload(void) { HTTPUpload& upload = server->upload(); if (upload.status == UPLOAD_FILE_START) { Serial.printf("Update: %s\n", upload.filename.c_str()); // start with max available size if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_WRITE) { // flashing firmware to ESP if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_END) { // true to set the size to the current progress if (Update.end(true)) { Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); } else { Update.printError(Serial); } } } #endif #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) void SimpleUpdater::setup(UPDATE_WEB_SERVER *_server) { if (_server == NULL) { return; } server = _server; #if defined(ARDUINO_ARCH_ESP8266) updateServer.setup(server); #elif defined(ARDUINO_ARCH_ESP32) server->on(uri.c_str(), HTTP_POST, [this]() { postResult(); }, [this]() { postUpload(); }); server->on(uri.c_str(), HTTP_GET, [this]() { get(); }); #endif } SimpleUpdater::SimpleUpdater(String _uri) { uri = _uri; server = NULL; } #endif