Page 1 of 1

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

Posted: Tue Dec 12, 2023 5:31 pm
by antonins
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

Code: Select all

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

Code: Select all

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
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

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

Posted: Sun Dec 17, 2023 4:08 pm
by antonins
Hello,

I try several thinks but can't resolve my issue :(

I just see that about 15 seconds, the ESP32 block on

Code: Select all

client->write(";",1);
or

Code: Select all

client->readBytes(..)
Even if I test client available & connected and http connected just before.

Do you know where the issue is coming ?

Thanks for your help,

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

Posted: Sun Dec 17, 2023 4:38 pm
by lbernstone
Does it work if that's the only thing you have running?
Try putting a delay(1) in between the read and write. It won't fix it if you have extremely long reads, but at least it breaks the blocking gap in half.
Unless you really don't have 4k to spare, your buffer size should be 4096. This is the sector size, and will minimize the amount of writes to flash.

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

Posted: Sun Dec 17, 2023 8:02 pm
by antonins
I block everything except this function.
I also end server (server.end()) to test if there is something coming from this side.

I change buffer to 4096 and add a delay(1) between read and write but I still have the same problem :(