0

I'm trying to create an even log saved on flash. I try to make it a class, called flashLOG as shown. Code behaves as expceted, reads, writes and posts it size and entries.

The problem is that after reboot - it seems that the file "disappears".

Code is written based on the Arduino IDE example. First I thought it is a flash problem, but after flashing Arduino's example and tried to run my code on an other MCU - it seems there no physical problem with flash.

I tried to reduce most of my code only to write "events" in order to see what survives reboot - so please ignore commented out lines and some logic in reading text, in code attached.

The only thing came in mind- perhaps it has something to do with being in a class or inside a library.

Appreciate any help finding this bug **EDIT 1: ** Update .h file and .ino fil

.h file

class flashLOG {
private:
    char *_logfilename = "/logfile.txt";
    const static int _logsize = 5;     // entries
    const static int _log_length = 20; // chars in each entry
    char _log_array[_logsize][_log_length];
public:
    flashLOG(char *filename = "/logfile.txt");
    void start();
    void read();
    void write(const char *message);
    void append(const char *message);
    void postlog(int x);
    int sizelog();
};

.cpp file:

flashLOG::flashLOG(char *filename) 
{
    _logfilename = filename;
}
void flashLOG::start() {
    if (!LittleFS.begin())
    {
        Serial.println("LittleFS mount failed");
    }
    else {
        Serial.println("MOUNTED OK");
    }
}
void flashLOG::read() {
    int r = 0;
    int c = 0;

    File file = LittleFS.open(_logfilename, "r");
    if (!file)
    {
        Serial.println("Failed to open file for reading");
    }
    else {
        while (file.available())
        {
            char tt = file.read();
            //     if (tt != '\n')
            //     {
            //         _log_array[r][c] = tt;
            //         c++;
            //     }
            //     else
            //     {
            //         _log_array[r][c] = '\n';
            //         r++;
            //         c = 0;
            //         // Serial.println(_log_array[r-1]);
            //     }
            Serial.write(tt);
        }
    }
    file.close();
    //     return r;

}
void flashLOG::write(const char *message)
{
    char a[_log_length];
    //     sprintf(a, "%s\n", message);

    File file = LittleFS.open(_logfilename, "a");
    if (!file)
    {
        Serial.println("Failed to open file for appending");
    }
    else {
        if (file.print(message)) {
            Serial.println("Append OK");
        }
        else {
            Serial.println("Append fail");
        }
    }
    Serial.printf("file size: %d\n", file.size());
    delay(2000);
    file.close();
}
void flashLOG::append(const char *message)
{
    char a[_log_length];
    //     sprintf(a, "%s\n", message);

    File file = LittleFS.open(_logfilename, "a");
    if (!file)
    {
        Serial.println("Failed to open file for appending");
    }
    else {
        if (file.print(message)) {
            Serial.println("Append OK");
        }
        else {
            Serial.println("Append fail");
        }
    }
    Serial.printf("file size: %d\n", file.size());
    //     delay(2000);
    file.close();
}
void flashLOG::postlog(int x)
{
    int num_lines = 0;//read();
    int y = min(x, num_lines);
    for (int a = 0; a < y; a++)
    {
        if (y > 1)
        {
            char t[_log_length];
            sprintf(t, "[#%d] %s", a, _log_array[a]);
            Serial.println(t);
        }
        else
        {
            Serial.println(_log_array[a]);
        }
    }
}
int flashLOG::sizelog()
{
    File file = LittleFS.open(_logfilename, "r");
    int f=file.size();
    file.close();
    return f;
}

.ino file

#include <myIOT.h>
#include <Arduino.h>

flashLOG flog;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("\nBEGIN!");
  flog.start();
//  flog.write("HITHERE");
  flog.read();
  flog.append("HITHERE2");
//  flog.read();

}

void loop() {
  // put your main code here, to run repeatedly:

}
  • Without the class header, and preferably some skeleton code that demonstrates how you are using it, it's hard to tell exactly what is happening. For example, are you working from example code that formats the LittleFS file system every time setup() runs? In trying to reverse-engineer the code, I came up with this, and it is working correctly in that there is data after every restart, and it is valid. the cpp is what you gave above, with my own .h and .ino. If you give more info, I'll do a proper answer. gist.github.com/pfeerick/7230ce3b93c35c8b8feb4e97979ac3f7 – Peter Feerick Aug 8 at 6:04
  • 1
    Thanks for that. Other than a missing #include <LittleFS.h> it worked - i.e. it was appending. I would suggest that if it is not working for you at all, you've not formatted the LittleFS partition first - look at the sketch at the bottom of the gist linked above. There is also an underlying issue in the code, as after several appends, the code causes an exception, but to be expected if you can't get the initial bits to work! ``` BEGIN! MOUNTED OK HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2HITHERE2Append OK file size: 104 ``` – Peter Feerick Aug 9 at 10:59
  • 1
    I added this to the loop... do you get anything after the ESP has restarted with this? void loop() { delay(2000); ESP.restart(); delay(100); } – Peter Feerick Aug 9 at 11:01
  • 1
    I can't tell you why it shows file size and read properly but "loses" data only after reboot - perhaps it's only buffered in memory, not saved? Have a look at the output here. gist.github.com/pfeerick/33791f2ddbc70f30a89a590eab71d9a0 hm... begin() is supposed to format if a file system isn't detected... i just did an 'erase all' and re-loaded the sketch minus the format routine, and it got to the append after file size: 104 again and then continuous exceptions again. Very strange. I added the include because I also format when flashLOG b0rks after several appends – Peter Feerick Aug 9 at 11:25
  • 1
    @PeterFeerick - As I told, class was placed in another library ( encapsulate my entire IOT class ). Somehow - I had an instance of a file saving JSON, outside to all classes ( I dont know why I did it ), running FS.h - which caused some collision with LittleFS.h which probaby can't be ran together :). Thank you very much! – Guy . D Aug 16 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.