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:

}
Peter Feerick
  • 300
  • 1
  • 7
guyd
  • 898
  • 1
  • 13
  • 39
  • 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. https://gist.github.com/pfeerick/7230ce3b93c35c8b8feb4e97979ac3f7 – Peter Feerick Aug 08 '20 at 06:04
  • Will edit my question soon – guyd Aug 08 '20 at 06:27
  • @PeterFeerick - edited – guyd Aug 08 '20 at 07:56
  • 1
    Thanks for that. Other than a missing `#include ` 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 09 '20 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 09 '20 at 11:01
  • @PeterFeerick -class is implemneted in another.h file - so you don't see that include you've mentioned ( but it is there ). about the format, I'll try again, but doesn't it when I erase all ( third option in Arduino IDE ), doesn't it also format as needed ? how come it shows file size and read properly but "loses" data only after reboot ?? – guyd Aug 09 '20 at 11:07
  • @PeterFeerick - BTW why did you add another `#include – guyd Aug 09 '20 at 11:22
  • 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. https://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 09 '20 at 11:25
  • Actually, I'm not sure now... because of how the Arduino IDE preprocessor works, it isn't needed in the sketch... as you can see from the second gist... I didn't add it at all, since it will be included via the flashLOG.h. In reality, since I used it in the sketch main, it should be there as well. For your project, it shouldn't need to be included in the main sketch - just in the class that uses it. – Peter Feerick Aug 09 '20 at 11:32
  • I'm UTC+10, so unfortunately it looks like your work is my late evening. Happy to try and work through this more, just need to find a time that works for both of us ;) Then we can also take it over to the chat thingy (only clicked on it before to see what it was about and because SE was being pushy). – Peter Feerick Aug 10 '20 at 00:02
  • 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! – guyd Aug 16 '20 at 10:46
  • Fantastic. Glad you got it working! It's those seemingly little gremlins that are the most annoying - hard to pin down! – Peter Feerick Aug 16 '20 at 11:58
  • @PeterFeerick - sooooooo annoying. I was so troubled about how without calling any class but the one I need, it failed while in a new library it didn't....URRRGGHH. cheers – guyd Aug 16 '20 at 12:00

0 Answers0