@@ -495,12 +495,18 @@ void AsyncFsWebServer::doWifiConnection(AsyncWebServerRequest *request) {
495495 request->send (401 , " text/plain" , " Wrong credentials provided" );
496496}
497497
498+ // the request handler is triggered after the upload has finished...
499+ // create the response, add header, and send response
498500void AsyncFsWebServer::update_second (AsyncWebServerRequest *request) {
499- // the request handler is triggered after the upload has finished...
500- // create the response, add header, and send response
501+
501502 String txt;
502503 if (Update.hasError ()) {
503- txt = " Error! Formware update not performed" ;
504+ txt = " Error! " ;
505+ #if defined(ESP8266)
506+ txt += Update.getErrorString ();
507+ #elif defined(ESP32)
508+ txt += Update.errorString ();
509+ #endif
504510 }
505511 else {
506512 txt = " Update completed successfully. The ESP32 will restart" ;
@@ -517,15 +523,13 @@ void AsyncFsWebServer::update_second(AsyncWebServerRequest *request) {
517523}
518524
519525void AsyncFsWebServer::update_first (AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final ) {
526+ static uint8_t otaDone = 0 ;
520527
521528 if (!m_contentLen) {
522- int headers = request->headers ();
523- for (int i=0 ;i<headers;i++){
524- AsyncWebHeader* h = request->getHeader (i);
525- if (h->name ().indexOf (" Content-Length" ) > -1 ) {
526- m_contentLen = h->value ().toInt ();
527- DebugPrintln (h->value ());
528- }
529+ AsyncWebHeader* h = request->getHeader (" Content-Length" );
530+ if (h->value ().length ()) {
531+ m_contentLen = h->value ().toInt ();
532+ DebugPrintln (h->value ());
529533 }
530534 }
531535
@@ -557,23 +561,30 @@ void AsyncFsWebServer::update_first(AsyncWebServerRequest *request, String file
557561 if (Update.write (data, len) != len) {
558562 return request->send (400 , " text/plain" , " OTA could not begin" );
559563 }
560- static uint32_t sendTime;
561- if (millis () - sendTime > 1000 ) {
562- sendTime = millis ();
564+ static uint32_t pTime = millis ();
565+ if (millis () - pTime > 500 ) {
566+ pTime = millis ();
567+ otaDone = 100 * Update.progress () / Update.size ();
563568 char buffer[100 ];
564- snprintf (buffer, sizeof (buffer)," Update... Get %d of %d bytes " , index, m_contentLen );
565- m_ws->textAll (buffer);
569+ snprintf (buffer, sizeof (buffer)," OTA progress: %d%% \n " , otaDone );
570+ // m_ws->textAll(buffer);
566571 DebugPrintln (buffer);
567572 }
568573 }
569574
570575 if (final ) { // if the final flag is set then this is the last frame of data
571576 if (!Update.end (true )) { // true to set the size to the current progress
572- Update.printError (Serial);
577+ #if defined(ESP8266)
578+ Serial.printf (" %s\n " , Update.getErrorString ());
579+ #elif defined(ESP32)
580+ Serial.printf (" %s\n " , Update.errorString ());
581+ #endif
582+ otaDone = 0 ;
573583 return request->send (400 , " text/plain" , " Could not end OTA" );
574584 }
575- m_ws->textAll (" Update done! ESP will be restarted" );
576- DebugPrintln (" Update done! ESP will be restarted" );
585+ // m_ws->textAll("Update done! ESP will be restarted");
586+ DebugPrintln (" Update Success.\n Rebooting...\n " );
587+
577588 delay (100 );
578589 // restore task WDT timeout
579590 setTaskWdt (8000 );
@@ -586,18 +597,16 @@ IPAddress AsyncFsWebServer::startWiFi(uint32_t timeout, const char *apSSID, cons
586597 IPAddress ip;
587598 m_timeout = timeout;
588599 WiFi.mode (WIFI_STA);
589- const char *_ssid;
590- const char *_pass;
591600#if defined(ESP8266)
592601 struct station_config conf;
593602 wifi_station_get_config_default (&conf);
594- _ssid = reinterpret_cast <const char *>(conf.ssid );
595- _pass = reinterpret_cast <const char *>(conf.password );
603+ const char * _ssid = reinterpret_cast <const char *>(conf.ssid );
604+ const char * _pass = reinterpret_cast <const char *>(conf.password );
596605#elif defined(ESP32)
597606 wifi_config_t conf;
598607 esp_wifi_get_config (WIFI_IF_STA, &conf);
599- _ssid = reinterpret_cast <const char *>(conf.sta .ssid );
600- _pass = reinterpret_cast <const char *>(conf.sta .password );
608+ const char * _ssid = reinterpret_cast <const char *>(conf.sta .ssid );
609+ const char * _pass = reinterpret_cast <const char *>(conf.sta .password );
601610#endif
602611
603612 if (strlen (_ssid) && strlen (_pass)) {
0 commit comments