Added OTA Web Server

This commit is contained in:
Philip Johansson 2020-04-12 21:12:09 +02:00
parent 2506afdf28
commit eaf8b94bfa
4 changed files with 85 additions and 1 deletions

6
include/WebOTA.h Normal file
View File

@ -0,0 +1,6 @@
class WebOTA {
public:
WebOTA(int port);
void setup();
void loop();
};

View File

@ -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

69
src/WebOTA.cpp Normal file
View File

@ -0,0 +1,69 @@
/* --------------------------------------------- *
* Code from http://esp32-server.de/webupdate/ *
* --------------------------------------------- */
#include "WebOTA.h"
#include <Arduino.h>
#include <Update.h>
#include <WebServer.h>
WebServer* server;
const char* serverIndex = "\
<form method='POST' action='/update' enctype='multipart/form-data'>\
<input type='file' name='update'>\
<input type='submit' value='Update'>\
</form>";
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();
}

View File

@ -1,6 +1,7 @@
#include "FlashConfig.h"
#include "OSCRemote.h"
#include "Vbat.h"
#include "WebOTA.h"
#include "body.h"
#include "settings.h"
#include <ArduinoOTA.h>
@ -11,6 +12,7 @@ Body body = Body::instance();
FlashConfig config;
std::unique_ptr<IRemote> 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();
}