SD-Card --> Filesystem gets corrupted aftr random time
Posted: Sat Jun 08, 2024 4:28 pm
Hi,
i use an ESP32 with a 3.3Volt SD-Card-Modul for data logging of my power meter.
One of my main functions is this, which refreshes a day-sum-file.
File is saved to SD card.
My curent problem is, that after random time (sometimes weeks, sometimes month) the file system seems to get destroyed/corrupted. Files on SD Card then all have strange names like "!")$/ยง)" and are not readable anymore.
Here is the current code (please stay friendly... i am a beginner
Is this function maybe cause of the SD-Card defect?
Another question:
Is it better / required to check if SD-Card-Reader is ready with SD.begin(pinNum) prior to saving data each time.
Or is it ok to just call SD.begin() in void Setup()
Thank you for your support.
i use an ESP32 with a 3.3Volt SD-Card-Modul for data logging of my power meter.
One of my main functions is this, which refreshes a day-sum-file.
File is saved to SD card.
My curent problem is, that after random time (sometimes weeks, sometimes month) the file system seems to get destroyed/corrupted. Files on SD Card then all have strange names like "!")$/ยง)" and are not readable anymore.
Here is the current code (please stay friendly... i am a beginner
Code: Select all
// Struct at the moment has just one value
// Will be more in final script, so don't wonder why i use a struct here please ;-)
struct data_s {
double kwh;
};
//Parameters
//filename_tmp : e.g. "2024-06-08.bin"
//measuring_kwh: e.g. "0.45"
void refresh_daysum (const char* filename_tmp, double measuring_kwh) {
struct data_s sum;
File sumFile;
if (SD.exists(filename_tmp)) {
// Read out current value
sumFile= SD.open(filename_tmp, FILE_READ);
while (sumFile.available()) {
sumFile.read((uint8_t *)&summe, sizeof(summe));
}
sumFile.close();
// add new measuring to current value
sum.kwh = sum.kwh + measuring_kwh;
} else {
// First measuring of the day
sum.kwh = measuring_kwh;
}
// Question is, if this part of the code is bad.
// Is it bad to delete the file and create it anew every time?
// Would it be better for SD-life to keep the file and just refresh the value?
SD.remove(filename_tmp);
sumFile= SD.open(filename_tmp, FILE_WRITE);
sumFile.write((uint8_t *)&summe, sizeof(summe));
sumFile.close();
}
Another question:
Is it better / required to check if SD-Card-Reader is ready with SD.begin(pinNum) prior to saving data each time.
Or is it ok to just call SD.begin() in void Setup()
Thank you for your support.