@@ -469,6 +469,7 @@ void WiFiManager::setupConfigPortal() {
469
469
}
470
470
471
471
/* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */
472
+
472
473
server->on (String (FPSTR (R_root)).c_str (), std::bind (&WiFiManager::handleRoot, this ));
473
474
server->on (String (FPSTR (R_wifi)).c_str (), std::bind (&WiFiManager::handleWifi, this , true ));
474
475
server->on (String (FPSTR (R_wifinoscan)).c_str (), std::bind (&WiFiManager::handleWifi, this , false ));
@@ -483,6 +484,9 @@ void WiFiManager::setupConfigPortal() {
483
484
server->on (String (FPSTR (R_status)).c_str (), std::bind (&WiFiManager::handleWiFiStatus, this ));
484
485
server->onNotFound (std::bind (&WiFiManager::handleNotFound, this ));
485
486
487
+ server->on ((String)FPSTR (R_update), std::bind (&WiFiManager::handleUpdate, this ));
488
+ server->on ((String)FPSTR (R_updatedone), HTTP_POST, std::bind (&WiFiManager::handleUpdateDone, this ), std::bind (&WiFiManager::handleUpdating, this ));
489
+
486
490
server->begin (); // Web server start
487
491
DEBUG_WM (DEBUG_VERBOSE,F (" HTTP server started" ));
488
492
@@ -2693,3 +2697,92 @@ void WiFiManager::WiFi_autoReconnect(){
2693
2697
// }
2694
2698
#endif
2695
2699
}
2700
+
2701
+ // Called when /u is requested
2702
+ void WiFiManager::handleUpdate () {
2703
+ DEBUG_WM (DEBUG_VERBOSE,F (" <- Handle update" ));
2704
+ if (captivePortal ()) return ; // If captive portal redirect instead of displaying the page
2705
+ String page = getHTTPHead (FPSTR (S_options)); // @token options
2706
+ String str = FPSTR (HTTP_ROOT_MAIN);
2707
+ str.replace (FPSTR (T_v), configPortalActive ? _apName : WiFi.localIP ().toString ()); // use ip if ap is not active for heading
2708
+ page += str;
2709
+
2710
+ page += FPSTR (HTTP_UPDATE);
2711
+ page += FPSTR (HTTP_END);
2712
+
2713
+ server->sendHeader (FPSTR (HTTP_HEAD_CL), String (page.length ()));
2714
+ server->send (200 , FPSTR (HTTP_HEAD_CT), page);
2715
+
2716
+ }
2717
+
2718
+ void WiFiManager::handleUpdating (){
2719
+ if (captivePortal ()) return ; // If captive portal redirect instead of displaying the page
2720
+ // handler for the file upload, get's the sketch bytes, and writes
2721
+ // them through the Update object
2722
+ HTTPUpload& upload = server->upload ();
2723
+ if (upload.status == UPLOAD_FILE_START) {
2724
+ Serial.setDebugOutput (true );
2725
+
2726
+ #ifdef ESP8266
2727
+ WiFiUDP::stopAll ();
2728
+ #elif defined(ESP32)
2729
+ // Think we do not need to stop WiFIUDP because we haven't started a listener
2730
+ #endif
2731
+ Serial.printf (" Update: %s\r\n " , upload.filename .c_str ());
2732
+ #ifdef ESP8266
2733
+ uint32_t maxSketchSpace = (ESP.getFreeSketchSpace () - 0x1000 ) & 0xFFFFF000 ;
2734
+ #elif defined(ESP32)
2735
+ uint32_t maxSketchSpace = (ESP.getFlashChipSize () - 0x1000 ) & 0xFFFFF000 ;
2736
+
2737
+ #endif
2738
+
2739
+ if (!Update.begin (maxSketchSpace)) { // start with max available size
2740
+ Update.printError (Serial);
2741
+ }
2742
+ } else if (upload.status == UPLOAD_FILE_WRITE) {
2743
+ Serial.print (" ." );
2744
+ if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ) {
2745
+ Update.printError (Serial);
2746
+ }
2747
+ } else if (upload.status == UPLOAD_FILE_END) {
2748
+ if (Update.end (true )) { // true to set the size to the current progress
2749
+ Serial.printf (" Updated: %u bytes\r\n Rebooting...\r\n " , upload.totalSize );
2750
+ } else {
2751
+ Update.printError (Serial);
2752
+ }
2753
+ Serial.setDebugOutput (false );
2754
+ } else if (upload.status == UPLOAD_FILE_ABORTED) {
2755
+ Update.end ();
2756
+ DEBUG_WM (F (" <- Update was aborted" ));
2757
+ }
2758
+ delay (0 );
2759
+ } // handleUpdating
2760
+
2761
+ void WiFiManager::handleUpdateDone () {
2762
+ DEBUG_WM (DEBUG_VERBOSE, F (" <- Handle update done" ));
2763
+ if (captivePortal ()) return ; // If captive portal redirect instead of displaying the page
2764
+
2765
+ String page = getHTTPHead (FPSTR (S_options)); // @token options
2766
+ String str = FPSTR (HTTP_ROOT_MAIN);
2767
+ str.replace (FPSTR (T_v), configPortalActive ? _apName : WiFi.localIP ().toString ()); // use ip if ap is not active for heading
2768
+ page += str;
2769
+
2770
+ if (Update.hasError ()) {
2771
+ page += FPSTR (HTTP_UPDATE_FAIL);
2772
+ DEBUG_WM (F (" update failed" ));
2773
+ }
2774
+ else {
2775
+ page += FPSTR (HTTP_UPDATE_OK);
2776
+ DEBUG_WM (F (" update ok" ));
2777
+
2778
+ }
2779
+ page += FPSTR (HTTP_END);
2780
+
2781
+ server->sendHeader (FPSTR (HTTP_HEAD_CL), String (page.length ()));
2782
+ server->send (200 , FPSTR (HTTP_HEAD_CT), page);
2783
+
2784
+ delay (1000 ); // send page
2785
+ if (!Update.hasError ()) {
2786
+ ESP.restart ();
2787
+ }
2788
+ }
0 commit comments