Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPIFFS write not persisting completely on REBOOT #2853

Closed
chakri1vr1 opened this issue Jan 12, 2017 · 2 comments
Closed

SPIFFS write not persisting completely on REBOOT #2853

chakri1vr1 opened this issue Jan 12, 2017 · 2 comments

Comments

@chakri1vr1
Copy link

chakri1vr1 commented Jan 12, 2017

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

#include <Arduino.h>
#include <stdlib.h> 
#include <FS.h>

#define SERIAL_BAUDRATE                 115200
#define FORMAT_SPIFF_ON_START                 false
#define PRINT_PENDING_DATA_ON_START                 true

void printFile(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--------------");
}

void updataToServer(postData){
    dataFile.println(postData);
    Serial.print("Appended--");Serial.print(postData);Serial.println("--to dataFile");
}

void setup() {
    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");
    }
}

void loop() {
    static unsigned long lastDataUpdateLoop = millis();
    if((millis() - lastDataUpdateLoop) > DATA_UPDATE_LOOP_TIME){
        lastDataUpdateLoop = millis();
        updateDataToServer("Some random data to save to file");
    }
}
@chakri1vr1
Copy link
Author

Newbie alert !

A simple flush() seems to have solved the issue.

dataFile.println(postData);
dataFile.flush();

@frank240889
Copy link

Thanks!!!! I had the same problem!!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants