Hi @Hansen, I’m having problems writing two files consecutively on the Flash, If I run the sequences one at a time everything goes well, if I execute them consecutively the second writing corrupts the first file. The sequence consists of deleting the file and writing the new file. Do you have any idea where the problem might be? I’ll post the code part.
I will be grateful if you can help me
void deleteFile(fs::FS &fs, const char *pathD)
{
DPRINT("Deleting file: ");
DPRINTLN(pathD);
if (fs.remove(pathD))
{
DPRINTLN(“File deleted”);
}
else
{
DPRINTLN(“Delete failed”);
}
delay(500);
}
void writeFile(fs::FS &fs, const char *pathF, const char *message)
{
DPRINT("Writing file: ");
DPRINTLN(pathF);
File fileF = fs.open(pathF, FILE_WRITE);
if (!fileF)
{
DPRINTLN(“Failed to open file for writing”);
return;
}
const int capacity = 1024;
StaticJsonDocument docW;
// Set the values in the document
docW[“IdDevice”] = config.IdDevice;
docW[“DeviceName”] = config.DeviceName;
docW[“tftTimeout”] = config.tftTimeout;
docW[“ssidSD”] = config.ssidSD;
docW[“passSD”] = config.passSD;
docW[“local_IP”] = config.local_IP;
docW[“gateway”] = config.gateway;
docW[“subnet”] = config.subnet;
docW[“primaryDNS”] = config.primaryDNS;
docW[“secondaryDNS”] = config.secondaryDNS;
docW[“LowWiFiTreshold”] = config.LowWiFiTreshold;
docW[“SensorCalibrationTime”] = config.SensorCalibrationTime;
docW[“DBAddressParam”] = config.DBAddressParam;
docW[“DBPortParam”] = config.DBPortParam;
docW[“DBUpTimeParam”] = config.DBUpTimeParam;
docW[“DBUserParam”] = config.DBUserParam;
docW[“DBPswParam”] = config.DBPswParam;
docW[“HumAlaParam”] = config.HumAlaParam;
docW[“TempAlaParam”] = config.TempAlaParam;
docW[“DewAlaParam”] = config.DewAlaParam;
docW[“HumCalParam”] = config.HumCalParam;
docW[“TempCalParam”] = config.TempCalParam;
docW[“SyncDB”] = config.SyncDB;
// Serialize JSON to file
if (serializeJson(docW, fileF) == 0)
{
DPRINTLN(F(“Failed to write to file”));
}
fileF.close();
}
void WriteCfgNew()
{
DPRINTLN(“Delete file”);
deleteFile(SPIFLASH, FileCfgtest);
char bufcfgTime[30];
const char *fineriga = “\n”;
writeFilecfg(SPIFLASH, FileCfgtest, strcat(itoa(cfgTime, bufcfgTime, 10), fineriga));
readFile(SPIFLASH,FileCfgtest);
delay(500);
}
void writeFilecfg(fs::FS &fs, const char *pathcfg, const char *message)
{
DPRINT("Writing file cfg: ");
DPRINTLN(pathcfg);
File filecfg = fs.open(pathcfg, FILE_WRITE);
if (!filecfg)
{
DPRINTLN(“Failed to open file for writing”);
return;
}
if (filecfg.print(message))
{
DPRINTLN(“File written”);
}
else
{
DPRINTLN(“Write failed”);
}
filecfg.close();
}