| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #include "webinterface.h"
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <WiFiAP.h>
- #include <ESPmDNS.h>
- #include <Update.h>
- #include "parameters.h"
- #include "romfs.h"
- static WebServer server(80);
- /*
- init web server
- */
- void WebInterface::init(void)
- {
- WiFi.softAP(g.wifi_ssid, g.wifi_password);
- IPAddress myIP = WiFi.softAPIP();
- /*return index page which is stored in serverIndex */
- server.on("/", HTTP_GET, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/html", ROMFS::find_string("web/login.html"));
- });
- server.on("/serverIndex", HTTP_GET, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/html", ROMFS::find_string("web/uploader.html"));
- });
- /*handling uploading firmware file */
- 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.printf("Update: %s\n", upload.filename.c_str());
- if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
- 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) {
- 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);
- }
- }
- });
- server.begin();
- }
- void WebInterface::update()
- {
- if (!initialised) {
- init();
- initialised = true;
- }
- server.handleClient();
- }
|