ESP32S3 OTA

tqh_vn
Posts: 8
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:

Code: Untitled.txt Select all

 
Begin OTA. Wait updating ...
OTA: 1%
OTA: 2%
OTA: 3%
OTA: 4%
OTA: 5%
OTA: 6%
OTA: 7%
OTA: 8%
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x8 (TG1WDT_SYS_RST),boot:0x28 (SPI_FAST_FLASH_BOOT)
Saved PC:0x403743c0
SPIWP:0xee
mode:DIO, clock div:1
Here is my source code:

Code: Untitled.cpp Select all


#define CPR_ESP32OTA_HTTP_TIMEOUT     10000 //millis
#define CPR_ESP32OTA_BIN_BUF_SIZE 4096
bool CprESP32OTA::execOTA(UpdateClass::THandlerFunction_Progress fnProgress)
{
String url = "http://" + _host + _bin;
HTTPClient http;
http.setConnectTimeout(CPR_ESP32OTA_HTTP_TIMEOUT);
http.setTimeout(CPR_ESP32OTA_HTTP_TIMEOUT);
if(!http.begin(url))
{
Serial.printf("execOTA (%s): HTTP begin FAILED\n", url.c_str());
return false;
}
if (http.GET() == HTTP_CODE_OK) //200
{
int totalLength = http.getSize();
Serial.printf("%s: SIZE = %d\n", url.c_str(), totalLength);
if(!Update.begin(totalLength))
{
Serial.printf("Update.begin ERR: %d (%s)\n", Update.getError(), Update.errorString());
http.end();
return false;
}
Serial.println("Begin OTA. Wait updating ...");
// create buffer for read
//uint8_t buff[CPR_ESP32OTA_BIN_BUF_SIZE] = { 0 };
uint8_t *bin_buf = NULL;
bin_buf = (uint8_t*)malloc(CPR_ESP32OTA_BIN_BUF_SIZE);
memset(bin_buf, 0, CPR_ESP32OTA_BIN_BUF_SIZE);

WiFiClient* stream = http.getStreamPtr(); // get tcp stream
// read all data from server
int dataWritten = 0;
while (http.connected() && dataWritten < totalLength)
{
size_t sizeAvail = stream->available();
if (sizeAvail > 0)
{
size_t bytes_to_read = min(sizeAvail, size_t(CPR_ESP32OTA_BIN_BUF_SIZE));
//Serial.printf("bytes_to_read = %d\n", bytes_to_read);
size_t bytes_read = stream->readBytes((char*)bin_buf, bytes_to_read);
//Serial.printf("bytes_to_read = %d, bytes_read = %d\n", bytes_to_read, bytes_read);
size_t bytes_written = Update.write(bin_buf, bytes_read);
//Serial.printf("bytes_to_read = %d, bytes_read = %d, bytes_written = %d\n", bytes_to_read, bytes_read, bytes_written);
if (bytes_read != bytes_written)
{
Serial.printf("Update.write ERR: %d %d %d\n", bytes_to_read, bytes_read, bytes_written);
break;
}
dataWritten += bytes_written;
if (fnProgress != NULL) fnProgress(dataWritten, totalLength);
}
//esp_task_wdt_reset();
}
free(bin_buf);
bin_buf = NULL;
if (dataWritten == totalLength)
Serial.println("Written : " + String(dataWritten) + " successfully");
else
Serial.println("Written only : " + String(dataWritten) + "/" + String(totalLength));
if (Update.end())
{
Serial.println("OTA done!");
if (Update.isFinished())
{
Serial.println("Update completed. execOTA END");
http.end();
return true;
}
else
Serial.println("Update not finished? Something went wrong!");
}
else
Serial.printf("Update.end ERR: %d (%s)\n", Update.getError(), Update.errorString());
}
http.end();
return false;
}
Please help me overcome this difficulty.

lbernstone
Posts: 1131
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: 8
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: ChatGPT-User and 7 guests