You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am saving some data to a file on SPIFFS in a loop, approximately a line each 25 seconds. When I restart the device, some 3-4 lines at the end which had written are gone on start. As I have read, it seems the op buffers aren't flushed sometimes and sudden reboot might cause similar issue, but my question is that is there a efficient way to handle it? Is it advisable to use fclose() on every write operation (once 25 seconds)
Hardware
Hardware: ESP-12
Core Version: 2.1.0-rc2
Settings in IDE
Module: NodeMCU 1.0 (ESP-12E) Module
Flash Size: 4M( 3M SPIFFS)
CPU Frequency: 80Mhz
Upload Using: SERIAL
Sketch
#include<Arduino.h>
#include<stdlib.h>
#include<FS.h>
#defineSERIAL_BAUDRATE115200
#defineFORMAT_SPIFF_ON_STARTfalse
#definePRINT_PENDING_DATA_ON_STARTtruevoidprintFile(File f){
f.seek(0,SeekSet);
Serial.println("--------------Starting read of dataFile--------------");
while(f.available()) {
//Lets read line by line from the file
String line = f.readStringUntil('\n');
Serial.println(line);
}
Serial.println("--------------Ended read of dataFile--------------");
}
voidupdataToServer(postData){
dataFile.println(postData);
Serial.print("Appended--");Serial.print(postData);Serial.println("--to dataFile");
}
voidsetup() {
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
Serial.println();
SPIFFS.begin();
if(FORMAT_SPIFF_ON_START)
if(SPIFFS.format())
Serial.println("Formatting succesfull");
else
Serial.println("Formatting failed");
dataFile= SPIFFS.open("data.txt", "a+");
if (!dataFile) {
Serial.println("Opening dataFile failed");
handleFatalError();
}else{
Serial.println("energyData.txt file opened for appending");
if(PRINT_PENDING_DATA_ON_START){
printFile(dataFile);
}
}
if(SPIFFS.info(fs_info)){
Serial.println("Filesystem information :");
Serial.print("Total bytes : ");Serial.println(fs_info.totalBytes);
Serial.print("Used bytes : ");Serial.println(fs_info.usedBytes);
}else{
Serial.println("Failed to retrieve FSInfo");
}
}
voidloop() {
staticunsignedlong lastDataUpdateLoop = millis();
if((millis() - lastDataUpdateLoop) > DATA_UPDATE_LOOP_TIME){
lastDataUpdateLoop = millis();
updateDataToServer("Some random data to save to file");
}
}
The text was updated successfully, but these errors were encountered:
Basic Infos
I am saving some data to a file on SPIFFS in a loop, approximately a line each 25 seconds. When I restart the device, some 3-4 lines at the end which had written are gone on start. As I have read, it seems the op buffers aren't flushed sometimes and sudden reboot might cause similar issue, but my question is that is there a efficient way to handle it? Is it advisable to use fclose() on every write operation (once 25 seconds)
Hardware
Hardware: ESP-12
Core Version: 2.1.0-rc2
Settings in IDE
Module: NodeMCU 1.0 (ESP-12E) Module
Flash Size: 4M( 3M SPIFFS)
CPU Frequency: 80Mhz
Upload Using: SERIAL
Sketch
The text was updated successfully, but these errors were encountered: