Problem when reading file line by line to feed it in to char

I tried to upload the code on a ESP32 (changing LittleFS to SPIFFS) but now it doesnt read the file.

Finally i got it right!
I think the problem of restarts was where the declarations for the line read an parse were made. I put them inside the while(chart.available()) and suddenly it loads even 20 lines without resetting.
The working MRE is this:

#include <LittleFS.h>

#include <CircularBuffer.h>
#include <string.h>


unsigned long log_prevTime = 0;
unsigned int log_time = 10000;
unsigned long tPrevioSensor = 0;
unsigned long sensor_delay = 1000;
double tempD1;
double rHum;

namespace data {
  typedef struct {
    String time;
    double t;
    double h;
  } record;
  void print(record r) {
   File file = LittleFS.open("log.csv", "a");
      file.print(r.time);
      file.print(",");
      file.print(r.t);
      file.print(",");
      file.println(r.h);
      file.close();
    }

  }


CircularBuffer<data::record, 1008> structs;

void getSensorData(){
  tempD1 = random(20,30);
  rHum = random(50, 60);
}
void wrBuff(){
  LittleFS.remove("log.csv");
  
  String ft = "JUE 00:05";
  
  structs.push(data::record{ft, tempD1, rHum});
  
  Serial.println("data saved to buffer");
  for (int j=0; j < structs.size(); j++){
    data::print(structs[j]);
  }
  Serial.println("data saved to file");
  
}
void load_chart(){
  File chart = LittleFS.open("log.csv","r");
  Serial.println("log open");
  while (chart.available()){
    char linea[23];
    byte index = 0;
    char *ptr = NULL;
    char *str[4];
    String H;
    float t;
    float h;
    
    chart.readStringUntil('\n').toCharArray(linea, 23);
    Serial.println(linea);
    ptr = strtok(linea,",");
    while(ptr != NULL){
      str[index] = ptr;
      index++;
      ptr = strtok(NULL,",");
    }
    
    H = str[0];
    t = atof(str[1]);
    h = atof(str[2]);
    structs.push(data::record{H,t,h});
    
  }
  chart.close();
  Serial.println("Buffer Loaded");
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
    // Initialize LittleFS
    if (!LittleFS.begin()) {
      Serial.println("Ha ocurrido un error al montar los archivos del LFS.");
      return;
    }
    else{
      Serial.println("Sistema de Archivos Inciado correctamente.");
    }
    load_chart();
    Serial.println("setup completed");
}

void loop() {
  // put your main code here, to run repeatedly:
  if(millis() >= tPrevioSensor + sensor_delay){
      getSensorData();
      tPrevioSensor = millis();
    }
    if (millis() >= log_prevTime + log_time){
    
    wrBuff();
    log_prevTime = millis();
  }
}

Thanks all for the help given!

To anyone that comes across this post, this code creates a circular log file. Works ok in ESP32 and ESP8266. Feel free to use it.
Didn't test yet whats the max size for the circular buffer. I'll try to post it when i test it!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.