Web.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "Web.h"
  2. WebServer server(80);
  3. Web::Web()
  4. {
  5. }
  6. void Web::main()
  7. {
  8. //Serial.println("Running the shits");
  9. // Notify if client has connected to the update server
  10. int current_sta = WiFi.softAPgetStationNum();
  11. if (current_sta < this->num_sta)
  12. {
  13. this->num_sta = current_sta;
  14. Serial.print("Update server: Client disconnected -> ");
  15. Serial.println(this->num_sta);
  16. }
  17. else if (current_sta > this->num_sta)
  18. {
  19. this->num_sta = current_sta;
  20. Serial.print("Update server: Client connected -> ");
  21. Serial.println(this->num_sta);
  22. }
  23. server.handleClient();
  24. delay(1);
  25. }
  26. void Web::setupOTAupdate()
  27. {
  28. display_obj.tft.setTextWrap(false);
  29. display_obj.tft.setFreeFont(NULL);
  30. display_obj.tft.setCursor(0, 100);
  31. display_obj.tft.setTextSize(1);
  32. display_obj.tft.setTextColor(TFT_WHITE);
  33. display_obj.tft.print("Configuring update server...\n\n");
  34. Serial.println("Configuring update server...");
  35. display_obj.tft.setTextColor(TFT_YELLOW);
  36. // Start WiFi AP
  37. WiFi.softAP(ssid, password);
  38. Serial.println("");
  39. display_obj.tft.print("SSID: ");
  40. display_obj.tft.println(ssid);
  41. display_obj.tft.print("IP address: ");
  42. display_obj.tft.print(WiFi.softAPIP());
  43. display_obj.tft.print("\n");
  44. Serial.print("IP address: ");
  45. Serial.println(WiFi.softAPIP());
  46. /*use mdns for host name resolution*/
  47. /*
  48. if (!MDNS.begin(host)) { //http://esp32.local
  49. Serial.println("Error setting up MDNS responder!");
  50. while (1) {
  51. delay(1000);
  52. }
  53. }
  54. Serial.println("mDNS responder started");
  55. */
  56. /*return index page which is stored in serverIndex */
  57. server.on("/", HTTP_GET, [this]() {
  58. server.sendHeader("Connection", "close");
  59. server.send(200, "text/html", loginIndex);
  60. });
  61. server.on("/serverIndex", HTTP_GET, [this]() {
  62. server.sendHeader("Connection", "close");
  63. server.send(200, "text/html", serverIndex);
  64. });
  65. /*handling uploading firmware file */
  66. server.on("/update", HTTP_POST, [this]() {
  67. server.sendHeader("Connection", "close");
  68. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  69. ESP.restart();
  70. }, [this]() {
  71. HTTPUpload& upload = server.upload();
  72. if (upload.status == UPLOAD_FILE_START) {
  73. display_obj.tft.setTextColor(TFT_YELLOW);
  74. display_obj.tft.print("Update: ");
  75. display_obj.tft.print(upload.filename.c_str());
  76. display_obj.tft.print("\n");
  77. //display_obj.updateBanner(menu_function_obj.current_menu->name);
  78. Serial.printf("Update: %s\n", upload.filename.c_str());
  79. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  80. Update.printError(Serial);
  81. }
  82. } else if (upload.status == UPLOAD_FILE_WRITE) {
  83. /* flashing firmware to ESP*/
  84. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  85. Update.printError(Serial);
  86. }
  87. //display_obj.tft.println(upload.totalSize);
  88. /*
  89. String display_string = "";
  90. display_obj.tft.setCursor(0, 164);
  91. for (int i = 0; i < 40; i++) {
  92. display_string.concat(" ");
  93. }
  94. */
  95. display_obj.tft.setTextColor(TFT_CYAN);
  96. display_obj.tft.fillRect(0, 164, 240, 8, TFT_BLACK);
  97. //delay(1);
  98. //display_obj.tft.print(display_string);
  99. display_obj.tft.setCursor(0, 164);
  100. display_obj.tft.print("Bytes complete: ");
  101. display_obj.tft.print(upload.totalSize);
  102. display_obj.tft.print("\n");
  103. //Serial.println(upload.totalSize);
  104. } else if (upload.status == UPLOAD_FILE_END) {
  105. if (Update.end(true)) { //true to set the size to the current progress
  106. display_obj.tft.setTextColor(TFT_GREEN);
  107. display_obj.tft.print("Update Success: ");
  108. display_obj.tft.print(upload.totalSize);
  109. display_obj.tft.print("\nRebooting...\n");
  110. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  111. delay(1000);
  112. } else {
  113. Update.printError(Serial);
  114. }
  115. }
  116. });
  117. server.begin();
  118. display_obj.tft.setTextColor(TFT_GREEN);
  119. display_obj.tft.println("\nCompleted update server setup");
  120. Serial.println("Completed update server setup");
  121. }