Web.cpp 5.3 KB

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