Move WebOTA to separate module

This commit is contained in:
Philip Johansson 2020-06-12 10:40:10 +02:00
parent d0c47848f3
commit c0e46d2dcb
6 changed files with 8 additions and 98 deletions

3
.gitmodules vendored
View File

@ -13,3 +13,6 @@
[submodule "lib/pubsubclient"]
path = lib/pubsubclient
url = https://github.com/knolleary/pubsubclient.git
[submodule "lib/webota"]
path = lib/webota
url = https://gitea.philsson.com/philip/webota.git

View File

@ -1,7 +1,8 @@
{
"files.associations": {
"array": "cpp",
"*.tcc": "cpp"
"*.tcc": "cpp",
"functional": "cpp"
},
"editor.formatOnSave": true,
}

View File

@ -1,13 +0,0 @@
#include <Arduino.h>
#include <ESP8266WebServer.h>
class WebOTA {
public:
WebOTA(int port);
void setup(ESP8266WiFiClass* pWiFiClient);
void run();
private:
ESP8266WebServer m_server;
ESP8266WiFiClass* m_pWiFiClient;
};

1
lib/webota Submodule

@ -0,0 +1 @@
Subproject commit 6f9dc31b1ee4b2652a27bcaf9a2290a952fbdbd3

View File

@ -1,83 +0,0 @@
#include "WebOTA.h"
#include "WiFiUdp.h"
const char* m_serverIndex =
"<form method='POST' action='/update' enctype='multipart/form-data'><input "
"type='file' name='update'><input type='submit' value='Update'></form>";
ESP8266WebServer* pServer;
WebOTA::WebOTA(int port)
: m_server(port)
, m_pWiFiClient(nullptr)
{
pServer = &m_server;
}
void WebOTA::setup(ESP8266WiFiClass* pWiFiClient)
{
m_pWiFiClient = pWiFiClient;
/*** Most here is borrowed from Arduino example sketch "WebUpdate" ***/
// wait for WiFi connection
if (m_pWiFiClient->status() == WL_CONNECTED)
{
pServer->on("/", HTTP_GET, []() {
pServer->sendHeader("Connection", "close");
pServer->send(200, "text/html", m_serverIndex);
});
pServer->on(
"/update",
HTTP_POST,
[]() {
pServer->sendHeader("Connection", "close");
pServer->send(
200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
},
[]() {
HTTPUpload& upload = pServer->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();
});
pServer->begin();
} else
{
Serial.println("WiFi Failed");
}
}
void WebOTA::run()
{
pServer->handleClient();
}

View File

@ -7,9 +7,10 @@
#include <Ticker.h>
// My own includes
#include "WebOTA.h"
#include "config.h"
#include "myled.h"
#include "webota.h"
// Onboard button and LED
const int buttonPin = 0;