Hello i have a problem with downloading a file from the internet to an sd card via an ESP32. The gettting the data from the stream works fine, but the writing to a sdcard doesnt work so well. It fails to write to the sd card at random moments and wont continue to write. Sometimes it succes to write all of it to the card with the same code. Could anyone help me, how to write streams continously to an sdcard?
`
download_sd(SD, "https://data-ssl.atudo.net/output/mobile.php?v=4&lat=52.51678333333333&lon=13.416493333333335&session=3da5ce70a1a17597@@@bd9a18ba-d33d-41d7-b44e-a071bf812d10&ver=3.7.6&cspeed=0&direction=0&key=d83nv9dj38FQ", "/test.sqlite");
void download_sd(fs::FS &fs, String url, String path) {
if ((wifiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
int len = http.getSize();
uint8_t buff[512] = { 0 };
int c; //How many Bytes are read
WiFiClient * stream = http.getStreamPtr();
fs::File f = SD.open(path, FILE_WRITE);
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
// read up to 128 byte
c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
Serial.write(buff, c);
if (!f) {
Serial.println("Failed to open file for appending");
return;
}
while(!f.write(buff,c)){
Serial.println();
Serial.println("SD-Card write failed");
delay(100);
Serial.println();
}
if (len > 0) {
len -= c;
}
}
delay(1);
}
f.close();
Serial.println();
Serial.print("[HTTP] connection closed or file end.\n");
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(10000000);
}`