0

EDIT1 - This phenomena occurs only after reboot using button or ESP.reboot()

I'll try to make my question more focused:

  1. I have library that saved log events to SPIFFS.

  2. From 3rd entry (in the following example - epoch clock entries), it saves wrongly somehow (jump to Take-4 below).

  3. Following code is an example using only relevant functions from that library.

Quick explanation:

T is holding epoch clock, that stores it right after boot.

write(const char *message, bool NOW) function - gets a message to store, and a boolean NOW if it has to be saved immediately rather than delayed save (delayed saved is implemented only in library, while here it gets saved instantly.)

void _write2file() writes the buffer to SPIFFS.

int getnumlines() returns number of entries saved (seeking for _EOL chars). In code below it acts as information only that serves displaying the problem, while when used in real library code, it limits the number of entries of the log.

void rawPrintfile() prints in human readable manner log's contents.

And last, the only function which does not belong to the library, void insert_log_entry(), simply enter a log entry (always the same one, after reboot).

Take 1-After uploading the sketch, you get this result on:

19:58:00.125 -> add buffer #0: 1234567890

19:58:00.191 -> total Lines before: 0
19:58:00.191 -> entries in buffer: 1
19:58:00.257 -> ~~~ Saved in /logfile.txt ~~~
19:58:00.257 -> row #0 {1234567890}
19:58:00.257 -> ~~~ EOF ~~~

Explanation: there was 1 entry to buffer, no- entries before, and a printout of file contents. So far- OK!

Take 2 - Pressing reboot button, and results are:

19:58:07.747 -> Start!
19:58:07.814 -> add buffer #0: 1234567890

19:58:07.814 -> total Lines before: 1
19:58:07.814 -> entries in buffer: 1
19:58:07.814 -> ~~~ Saved in /logfile.txt ~~~
19:58:07.814 -> row #0 {1234567890}
19:58:07.814 -> row #1 {1234567890}
19:58:07.814 -> ~~~ EOF ~~~

Explanation:again 1 entry in buffer, 1 entry in log, and a printout of 2 entries.

Take 3 Pressing reboot button again, and results are:

19:58:10.167 -> Start!
19:58:10.200 -> add buffer #0: 1234567890

19:58:10.200 -> total Lines before: 2
19:58:10.233 -> entries in buffer: 1
19:58:10.233 -> ~~~ Saved in /logfile.txt ~~~
19:58:10.233 -> row #0 {1234567890}
19:58:10.233 -> row #1 {1234567890}
19:58:10.233 -> row #2 {1234567890}
19:58:10.233 -> ~~~ EOF ~~~

Explanation: same, but now 3 entries are saved.

Take 4!!!

19:58:11.194 -> Start!
19:58:11.227 -> add buffer #0: 1234567890

19:58:11.227 -> total Lines before: 2
19:58:11.260 -> entries in buffer: 1
19:58:11.260 -> ~~~ Saved in /logfile.txt ~~~
19:58:11.260 -> row #0 {1234567890}
19:58:11.260 -> row #1 {1234567890}
19:58:11.260 -> row #2 {12345678⸮⸮⸮1234567890}
19:58:11.260 -> ~~~ EOF ~~~

Explanation: Now here is the unsolved error- 4th entry gets mixed up with 3rd, causing it to count only 2 entries (where it had to be 3), and row #2 is now mixed.

In purpose - I simplified the code to focus on the problem itself, which I can't find it root cause for this error (rather that showing entire library , consisting delayed save and delete line function, when log gets full).

`

#include <FS.h>
unsigned long T = 1234567890;

char *_logfilename = "/logfile.txt";
int _buff_i = 0;
#define TEMP_LOG_SIZE 10
#define TEMP_LOG_LEN 150
int _logSize = TEMP_LOG_SIZE;                 // entries
int _logLength = TEMP_LOG_LEN;                // chars in each entry
char _logBuffer[TEMP_LOG_SIZE][TEMP_LOG_LEN]; // Temp buffer for delayed write
const char _EOL = '\r';
unsigned long lastUpdate = 0;

void insert_log_entry()
{
  char a[25];
  sprintf(a, "%d", T);
  write(a, true);
}
void rawPrintfile()
{
  int row_counter = 0;
  bool new_line = true;

  File file = SPIFFS.open(_logfilename, "r");
  if (!file)
  {

    Serial.println("Failed to open file for reading");
  }
  Serial.print("~~~ Saved in ");
  Serial.print(_logfilename);
  Serial.println(" ~~~");
  while (file.available())
  {
    if (new_line)
    {
      Serial.print("row #");
      Serial.print(row_counter++);
      Serial.print(" {");
      new_line = false;
    }
    char tt = file.read();
    if (tt == _EOL)
    {
      new_line = true;
      Serial.println("}");
    }
    else
    {
      Serial.print(tt);
    }
  }
  file.close();
  Serial.println("~~~ EOF ~~~");
}
int getnumlines()
{
  int row_counter = 0;

  File file = SPIFFS.open(_logfilename, "r");
  if (file)
  {
    while (file.available())
    {
      char tt = file.read();
      if (tt == _EOL)
      {
        row_counter++;
      }
    }
  }
  file.close();
  return row_counter;
}
void write(const char *message, bool NOW)
{
  sprintf(_logBuffer[_buff_i], "%s%c", message, _EOL);

  Serial.print("add buffer #");
  Serial.print(_buff_i);
  Serial.print(": ");
  Serial.println(_logBuffer[_buff_i]);
  _buff_i++;
  lastUpdate = millis();

  if (NOW == true)
  {
    _write2file();
  }
}
void _write2file()
{
  int num_lines = getnumlines();
  Serial.print("total Lines before: ");
  Serial.println(num_lines);
  Serial.print("entries in buffer: ");
  Serial.println(_buff_i);

  File file1 = SPIFFS.open(_logfilename, "a");
  if (!file1)
  {
    Serial.println("Failed to open file for appending");
  }
  else
  {
    for (int x = 0; x < _buff_i; x++)
    {
      file1.print(_logBuffer[x]);
    }
    _buff_i = 0;
    lastUpdate = 0;
  }
  file1.close();
}

void setup()
{
  Serial.begin(115200);
  Serial.println("\n\nStart!");
  SPIFFS.begin();
  insert_log_entry();
  rawPrintfile();
}

void loop()
{
  delay(50);
}
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Juraj
    Aug 11 at 13:43
0

Well , getting to answer was complicated.

  1. It happens only after reboot ( button, or ESP.reset() ), somehow after 2nd attempt, every time - getting this ( see THIS):
19:58:11.194 -> Start!
19:58:11.227 -> add buffer #0: 1234567890

19:58:11.227 -> total Lines before: 2
19:58:11.260 -> entries in buffer: 1
19:58:11.260 -> ~~~ Saved in /logfile.txt ~~~
19:58:11.260 -> row #0 {1234567890}
19:58:11.260 -> row #1 {1234567890}
19:58:11.260 -> row #2 {12345678⸮⸮⸮1234567890}   <----- THIS ----
19:58:11.260 -> ~~~ EOF ~~~
  1. @Peter, in comments referred for github issue , that pointed out that there is a known problem regarding reboots and validity of data stored. Issue was solved by using file.flush() - but this solution did not solve the problem

  2. When searching ESP8266 core, it pointed out an erros SPIFFS- regarding data saved and it validity

  3. only after updating LittleFS for all instances in code (somehow when checked as part of Peter's suggestion, only few instances were changed from SPIFFS ), solved that problem. no file.flush() or delay() is required.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.