ESP32S3 OTA

tqh_vn
Posts: 5
Joined: Thu Oct 26, 2023 8:13 am

ESP32S3 OTA

Postby tqh_vn » Thu Feb 01, 2024 4:06 am

Hi everyone. Have a good day for all. I use Arduino IDE with ESP32 board V3.0.0-alpha3. When updating the firmware (OTA) my ESP32S3 rebooted. The restart occurs when running to the "Update.write" function.
This is the debugging section:
  1.  
  2. Begin OTA. Wait updating ...
  3. OTA: 1%
  4. OTA: 2%
  5. OTA: 3%
  6. OTA: 4%
  7. OTA: 5%
  8. OTA: 6%
  9. OTA: 7%
  10. OTA: 8%
  11. ESP-ROM:esp32s3-20210327
  12. Build:Mar 27 2021
  13. rst:0x8 (TG1WDT_SYS_RST),boot:0x28 (SPI_FAST_FLASH_BOOT)
  14. Saved PC:0x403743c0
  15. SPIWP:0xee
  16. mode:DIO, clock div:1
Here is my source code:
  1. #define CPR_ESP32OTA_HTTP_TIMEOUT     10000 //millis
  2. #define CPR_ESP32OTA_BIN_BUF_SIZE     4096
  3. bool CprESP32OTA::execOTA(UpdateClass::THandlerFunction_Progress fnProgress)
  4. {
  5.   String url = "http://" + _host + _bin;
  6.   HTTPClient http;
  7.   http.setConnectTimeout(CPR_ESP32OTA_HTTP_TIMEOUT);
  8.   http.setTimeout(CPR_ESP32OTA_HTTP_TIMEOUT);
  9.   if(!http.begin(url))
  10.   {
  11.     Serial.printf("execOTA (%s): HTTP begin FAILED\n", url.c_str());
  12.     return false;
  13.   }
  14.   if (http.GET() == HTTP_CODE_OK) //200
  15.   {
  16.     int totalLength = http.getSize();
  17.     Serial.printf("%s: SIZE = %d\n", url.c_str(), totalLength);
  18.     if(!Update.begin(totalLength))
  19.     {
  20.       Serial.printf("Update.begin ERR: %d (%s)\n", Update.getError(), Update.errorString());
  21.       http.end();
  22.       return false;
  23.     }
  24.     Serial.println("Begin OTA. Wait updating ...");
  25.     // create buffer for read
  26.     //uint8_t buff[CPR_ESP32OTA_BIN_BUF_SIZE] = { 0 };
  27.     uint8_t *bin_buf = NULL;
  28.     bin_buf = (uint8_t*)malloc(CPR_ESP32OTA_BIN_BUF_SIZE);
  29.     memset(bin_buf, 0, CPR_ESP32OTA_BIN_BUF_SIZE);
  30.  
  31.     WiFiClient* stream = http.getStreamPtr(); // get tcp stream
  32.     // read all data from server
  33.     int dataWritten = 0;
  34.     while (http.connected() && dataWritten < totalLength)
  35.     {
  36.       size_t sizeAvail = stream->available();
  37.       if (sizeAvail > 0)
  38.       {
  39.         size_t bytes_to_read = min(sizeAvail, size_t(CPR_ESP32OTA_BIN_BUF_SIZE));
  40.         //Serial.printf("bytes_to_read = %d\n", bytes_to_read);
  41.         size_t bytes_read = stream->readBytes((char*)bin_buf, bytes_to_read);
  42.         //Serial.printf("bytes_to_read = %d, bytes_read = %d\n", bytes_to_read, bytes_read);
  43.         size_t bytes_written = Update.write(bin_buf, bytes_read);
  44.         //Serial.printf("bytes_to_read = %d, bytes_read = %d, bytes_written = %d\n", bytes_to_read, bytes_read, bytes_written);
  45.         if (bytes_read != bytes_written)
  46.         {
  47.           Serial.printf("Update.write ERR: %d %d %d\n", bytes_to_read, bytes_read, bytes_written);
  48.           break;
  49.         }
  50.         dataWritten += bytes_written;
  51.         if (fnProgress != NULL) fnProgress(dataWritten, totalLength);
  52.       }
  53.       //esp_task_wdt_reset();
  54.     }
  55.     free(bin_buf);
  56.     bin_buf = NULL;
  57.     if (dataWritten == totalLength)
  58.       Serial.println("Written : " + String(dataWritten) + " successfully");
  59.     else
  60.       Serial.println("Written only : " + String(dataWritten) + "/" + String(totalLength));
  61.     if (Update.end())
  62.     {
  63.       Serial.println("OTA done!");
  64.       if (Update.isFinished())
  65.       {
  66.         Serial.println("Update completed. execOTA END");
  67.         http.end();
  68.         return true;
  69.       }
  70.       else
  71.         Serial.println("Update not finished? Something went wrong!");
  72.     }
  73.     else
  74.       Serial.printf("Update.end ERR: %d (%s)\n", Update.getError(), Update.errorString());
  75.   }
  76.   http.end();
  77.   return false;
  78. }
Please help me overcome this difficulty.

lbernstone
Posts: 673
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32S3 OTA

Postby lbernstone » Thu Feb 01, 2024 3:07 pm

Unremark line 44 (prints bytes written), and see if you get a final bytes written without a corresponding OTA percentage. Calling fnProgress is the most likely source of the error.

tqh_vn
Posts: 5
Joined: Thu Oct 26, 2023 8:13 am

Re: ESP32S3 OTA

Postby tqh_vn » Fri Feb 02, 2024 9:01 am

Thanks for reply.
Unremark line 44 (prints bytes written), and see if you get a final bytes written without a corresponding OTA percentage
The ESP32S3 was restarted while running inside the Update.write function
Calling fnProgress is the most likely source of the error.
I checked again and this function is not the cause
Luckily, I just found a solution. Turn off the camera (esp_Camera_deinit()) before performing OTA. I will continue to test on many other boards.

Who is online

Users browsing this forum: No registered users and 147 guests