Web.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // Callback for the embedded jquery.min.js page
  27. void Web::onJavaScript(void) {
  28. Serial.println("onJavaScript(void)");
  29. server.setContentLength(jquery_min_js_v3_2_1_gz_len);
  30. server.sendHeader(F("Content-Encoding"), F("gzip"));
  31. server.send_P(200, "text/javascript", jquery_min_js_v3_2_1_gz, jquery_min_js_v3_2_1_gz_len);
  32. }
  33. void Web::setupOTAupdate()
  34. {
  35. display_obj.tft.setTextWrap(false);
  36. display_obj.tft.setFreeFont(NULL);
  37. display_obj.tft.setCursor(0, 100);
  38. display_obj.tft.setTextSize(1);
  39. display_obj.tft.setTextColor(TFT_WHITE);
  40. display_obj.tft.print("Configuring update server...\n\n");
  41. Serial.println("Configuring update server...");
  42. display_obj.tft.setTextColor(TFT_YELLOW);
  43. // Start WiFi AP
  44. WiFi.softAP(ssid, password);
  45. Serial.println("");
  46. display_obj.tft.print("SSID: ");
  47. display_obj.tft.println(ssid);
  48. display_obj.tft.print("IP address: ");
  49. display_obj.tft.print(WiFi.softAPIP());
  50. display_obj.tft.print("\n");
  51. Serial.print("IP address: ");
  52. Serial.println(WiFi.softAPIP());
  53. /*use mdns for host name resolution*/
  54. /*
  55. if (!MDNS.begin(host)) { //http://esp32.local
  56. Serial.println("Error setting up MDNS responder!");
  57. while (1) {
  58. delay(1000);
  59. }
  60. }
  61. Serial.println("mDNS responder started");
  62. */
  63. // return javascript jquery
  64. server.on("/jquery.min.js", HTTP_GET, onJavaScript);
  65. /*return index page which is stored in serverIndex */
  66. server.on("/", HTTP_GET, [this]() {
  67. server.sendHeader("Connection", "close");
  68. server.send(200, "text/html", loginIndex);
  69. });
  70. server.on("/serverIndex", HTTP_GET, [this]() {
  71. server.sendHeader("Connection", "close");
  72. server.send(200, "text/html", serverIndex);
  73. });
  74. /*handling uploading firmware file */
  75. server.on("/update", HTTP_POST, [this]() {
  76. server.sendHeader("Connection", "close");
  77. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  78. ESP.restart();
  79. }, [this]() {
  80. HTTPUpload& upload = server.upload();
  81. if (upload.status == UPLOAD_FILE_START) {
  82. display_obj.tft.setTextColor(TFT_YELLOW);
  83. display_obj.tft.print("Update: ");
  84. display_obj.tft.print(upload.filename.c_str());
  85. display_obj.tft.print("\n");
  86. //display_obj.updateBanner(menu_function_obj.current_menu->name);
  87. Serial.printf("Update: %s\n", upload.filename.c_str());
  88. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  89. Update.printError(Serial);
  90. }
  91. } else if (upload.status == UPLOAD_FILE_WRITE) {
  92. /* flashing firmware to ESP*/
  93. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  94. Update.printError(Serial);
  95. }
  96. //display_obj.tft.println(upload.totalSize);
  97. /*
  98. String display_string = "";
  99. display_obj.tft.setCursor(0, 164);
  100. for (int i = 0; i < 40; i++) {
  101. display_string.concat(" ");
  102. }
  103. */
  104. display_obj.tft.setTextColor(TFT_CYAN);
  105. display_obj.tft.fillRect(0, 164, 240, 8, TFT_BLACK);
  106. //delay(1);
  107. //display_obj.tft.print(display_string);
  108. display_obj.tft.setCursor(0, 164);
  109. display_obj.tft.print("Bytes complete: ");
  110. display_obj.tft.print(upload.totalSize);
  111. display_obj.tft.print("\n");
  112. //Serial.println(upload.totalSize);
  113. } else if (upload.status == UPLOAD_FILE_END) {
  114. if (Update.end(true)) { //true to set the size to the current progress
  115. display_obj.tft.setTextColor(TFT_GREEN);
  116. display_obj.tft.print("Update Success: ");
  117. display_obj.tft.print(upload.totalSize);
  118. display_obj.tft.print("\nRebooting...\n");
  119. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  120. delay(1000);
  121. } else {
  122. Update.printError(Serial);
  123. }
  124. }
  125. });
  126. server.begin();
  127. display_obj.tft.setTextColor(TFT_GREEN);
  128. display_obj.tft.println("\nCompleted update server setup");
  129. Serial.println("Completed update server setup");
  130. }