webinterface.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "webinterface.h"
  2. #include <WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <WebServer.h>
  5. #include <WiFiAP.h>
  6. #include <ESPmDNS.h>
  7. #include <Update.h>
  8. #include "parameters.h"
  9. #include "romfs.h"
  10. static WebServer server(80);
  11. /*
  12. init web server
  13. */
  14. void WebInterface::init(void)
  15. {
  16. WiFi.softAP(g.wifi_ssid, g.wifi_password);
  17. IPAddress myIP = WiFi.softAPIP();
  18. /*return index page which is stored in serverIndex */
  19. server.on("/", HTTP_GET, []() {
  20. server.sendHeader("Connection", "close");
  21. server.send(200, "text/html", ROMFS::find_string("web/login.html"));
  22. });
  23. server.on("/serverIndex", HTTP_GET, []() {
  24. server.sendHeader("Connection", "close");
  25. server.send(200, "text/html", ROMFS::find_string("web/uploader.html"));
  26. });
  27. /*handling uploading firmware file */
  28. server.on("/update", HTTP_POST, []() {
  29. server.sendHeader("Connection", "close");
  30. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  31. ESP.restart();
  32. }, []() {
  33. HTTPUpload& upload = server.upload();
  34. if (upload.status == UPLOAD_FILE_START) {
  35. Serial.printf("Update: %s\n", upload.filename.c_str());
  36. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  37. Update.printError(Serial);
  38. }
  39. } else if (upload.status == UPLOAD_FILE_WRITE) {
  40. /* flashing firmware to ESP*/
  41. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  42. Update.printError(Serial);
  43. }
  44. } else if (upload.status == UPLOAD_FILE_END) {
  45. if (Update.end(true)) { //true to set the size to the current progress
  46. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  47. } else {
  48. Update.printError(Serial);
  49. }
  50. }
  51. });
  52. server.begin();
  53. }
  54. void WebInterface::update()
  55. {
  56. if (!initialised) {
  57. init();
  58. initialised = true;
  59. }
  60. server.handleClient();
  61. }