ESP32 : Download from server doesn't work for big file

Bonjour,

I tried to download file from a server, it works perfectly for file until around 30ko but when I try for more bigger file (around 1000ko), system crash.

I use this code

int HandleWriteFileFromServer()
{
  HTTPClient http;
  http.setTimeout(30000);
  http.begin("http://boisurel.com/wp-content/uploads/2023/04/Capteur-solaire-en-facade-2604x1800.jpg");
  int httpCode = http.GET();
    if (httpCode <= 0) {
    //Error HTTP failed
    printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    http.end();
    WifiFirmwareUpdate = false;
    return 0;
  }
  int contentLen = http.getSize();
  int len = http.getSize();
  printf("Downloading: file should be %i\n", len);
 
  WiFiClient* client = http.getStreamPtr();
  client->setTimeout(2); // Définis timeout client
  Serial.print("Start");
 
//size_t written = Update.writeStream(*client);
const size_t bufferSize = 1024;  // Taille du tampon de lecture
uint8_t buffer[bufferSize];      // Tampon de lecture
size_t written = 0;
size_t bytesRead = 0;
size_t bytesWritten =0;
size_t size = 0;
unsigned long sortir = millis();
File fileToCreate = LittleFS.open("/Chart1.json", "w"); // Créer le nouveau fichier
while (written != contentLen && client->available()) {
  if(!http.connected()){Serial.println("Déconnceté");}
  size = client->available();
  Serial.print("Size : ");
  Serial.print(size);
  bytesRead = client->readBytes(buffer, min(bufferSize, size));
  Serial.print(".");
  bytesWritten = fileToCreate.write(buffer, bytesRead);
  Serial.print(". W : ");
  written += bytesWritten;
  Serial.println(written);
  if(millis() - sortir >= 30000)
  {
    Serial.println("Temps dépassé, échec du téléchargement");
    break;
  }
  if (bytesRead != bytesWritten) {
    // Erreur d'écriture
    Serial.print("Erreur d'écriture du firmware : ");
    Serial.print(bytesRead);
    Serial.print(" / ");
    Serial.print(bytesWritten);
    http.end();
    WifiFirmwareUpdate = false;
    return 0;
  }
  delay(1);
}
Serial.print("fichier créé");
fileToCreate.close();
http.end();
return 1;
}

On serial monitor, i've got this

Size : 3503.. W : 610304
Size : 5351.. W : 611328
Size : 5763.. W : 612352
Size : 4739.. W : 613376
Size : 3715.. W : 614400
Size : 5563.. W : 615424
Size : 4539
E (115742) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (115742) task_wdt:  - async_tcp (CPU 0/1)
E (115742) task_wdt: Tasks currently running:
E (115742) task_wdt: CPU 0: IDLE
E (115742) task_wdt: CPU 1: IDLE

It seems that program stop on ' client->readBytes' but I don't understand where is the issue ...
Sometime it stop at 200000, sometimes at 500000 without reproductible number...

Is someone have any idea of what happens ?

Thanks for your help,
Antonin

looks like the WDT is timing out probably in a long loop
in the loop try calling esp_task_wdt_reset() to reset the WDT

also consider using FTP?

Thanks for your feedback !

Watchdog reset is the result of readBytes crash.
The program is block on this line. WDT reset the CPU as it is done for.

Do you have example of FTP transfer program ?

Antonin

do a web search for esp32 ftp you will get plenty of links
if you program now works and data is transferred OK no need for going to FTP

Program still doesn't work.

My surprise is there many example of this code in internet. But nobody seems to have any issue with it ...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.