Web.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. uint8_t newMACAddress[] = {0x06, 0x07, 0x0D, 0x09, 0x0E, 0x0D};
  36. display_obj.tft.setTextWrap(false);
  37. display_obj.tft.setFreeFont(NULL);
  38. display_obj.tft.setCursor(0, 100);
  39. display_obj.tft.setTextSize(1);
  40. display_obj.tft.setTextColor(TFT_WHITE);
  41. Serial.println(wifi_scan_obj.freeRAM());
  42. display_obj.tft.print("Configuring update server...\n\n");
  43. Serial.println("Configuring update server...");
  44. display_obj.tft.setTextColor(TFT_YELLOW);
  45. // Start WiFi AP
  46. Serial.println("Initializing WiFi...");
  47. //wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  48. esp_wifi_init(&wifi_scan_obj.cfg);
  49. //esp_wifi_set_storage(WIFI_STORAGE_RAM);
  50. if (esp_wifi_set_storage(WIFI_STORAGE_FLASH) != ESP_OK)
  51. Serial.println("Could not set WiFi Storage!");
  52. esp_wifi_set_mode(WIFI_MODE_NULL);
  53. esp_wifi_start();
  54. Serial.println(wifi_scan_obj.freeRAM());
  55. Serial.println("Starting softAP...");
  56. esp_wifi_set_mac(WIFI_IF_AP, &newMACAddress[0]);
  57. WiFi.softAP(ssid, password);
  58. Serial.println("");
  59. Serial.println(wifi_scan_obj.freeRAM());
  60. Serial.println("Displaying settings to TFT...");
  61. display_obj.tft.print("SSID: ");
  62. display_obj.tft.println(ssid);
  63. display_obj.tft.print("IP address: ");
  64. display_obj.tft.print(WiFi.softAPIP());
  65. display_obj.tft.print("\n");
  66. Serial.print("IP address: ");
  67. Serial.println(WiFi.softAPIP());
  68. /*use mdns for host name resolution*/
  69. /*
  70. if (!MDNS.begin(host)) { //http://esp32.local
  71. Serial.println("Error setting up MDNS responder!");
  72. while (1) {
  73. delay(1000);
  74. }
  75. }
  76. Serial.println("mDNS responder started");
  77. */
  78. // return javascript jquery
  79. Serial.println("Setting server behavior...");
  80. Serial.println(wifi_scan_obj.freeRAM());
  81. server.on("/jquery.min.js", HTTP_GET, onJavaScript);
  82. /*return index page which is stored in serverIndex */
  83. server.on("/", HTTP_GET, [this]() {
  84. server.sendHeader("Connection", "close");
  85. server.send(200, "text/html", loginIndex);
  86. });
  87. server.on("/serverIndex", HTTP_GET, [this]() {
  88. server.sendHeader("Connection", "close");
  89. server.send(200, "text/html", serverIndex);
  90. });
  91. /*handling uploading firmware file */
  92. server.on("/update", HTTP_POST, [this]() {
  93. server.sendHeader("Connection", "close");
  94. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  95. ESP.restart();
  96. }, [this]() {
  97. HTTPUpload& upload = server.upload();
  98. if (upload.status == UPLOAD_FILE_START) {
  99. display_obj.tft.setTextColor(TFT_YELLOW);
  100. display_obj.tft.print("Update: ");
  101. display_obj.tft.print(upload.filename.c_str());
  102. display_obj.tft.print("\n");
  103. //display_obj.updateBanner(menu_function_obj.current_menu->name);
  104. Serial.printf("Update: %s\n", upload.filename.c_str());
  105. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  106. Update.printError(Serial);
  107. }
  108. } else if (upload.status == UPLOAD_FILE_WRITE) {
  109. /* flashing firmware to ESP*/
  110. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  111. Update.printError(Serial);
  112. }
  113. //display_obj.tft.println(upload.totalSize);
  114. /*
  115. String display_string = "";
  116. display_obj.tft.setCursor(0, 164);
  117. for (int i = 0; i < 40; i++) {
  118. display_string.concat(" ");
  119. }
  120. */
  121. display_obj.tft.setTextColor(TFT_CYAN);
  122. display_obj.tft.fillRect(0, 164, 240, 8, TFT_BLACK);
  123. //delay(1);
  124. //display_obj.tft.print(display_string);
  125. display_obj.tft.setCursor(0, 164);
  126. display_obj.tft.print("Bytes complete: ");
  127. display_obj.tft.print(upload.totalSize);
  128. display_obj.tft.print("\n");
  129. //Serial.println(upload.totalSize);
  130. } else if (upload.status == UPLOAD_FILE_END) {
  131. if (Update.end(true)) { //true to set the size to the current progress
  132. display_obj.tft.setTextColor(TFT_GREEN);
  133. display_obj.tft.print("Update Success: ");
  134. display_obj.tft.print(upload.totalSize);
  135. display_obj.tft.print("\nRebooting...\n");
  136. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  137. delay(1000);
  138. } else {
  139. Update.printError(Serial);
  140. }
  141. }
  142. });
  143. Serial.println("Finished setting server behavior");
  144. Serial.println(wifi_scan_obj.freeRAM());
  145. Serial.println("Starting server...");
  146. server.begin();
  147. display_obj.tft.setTextColor(TFT_GREEN);
  148. display_obj.tft.println("\nCompleted update server setup");
  149. Serial.println("Completed update server setup");
  150. Serial.println(wifi_scan_obj.freeRAM());
  151. }
  152. void Web::shutdownServer() {
  153. Serial.println("Closing Update Server...");
  154. server.stop();
  155. WiFi.mode(WIFI_OFF);
  156. esp_wifi_set_mode(WIFI_MODE_NULL);
  157. esp_wifi_stop();
  158. esp_wifi_deinit();
  159. Serial.println(wifi_scan_obj.freeRAM());
  160. }