Hi all!
I making a program that uses a circular buffer and saves it to a file.
The thing is i wan to load the file and feed it to the circular buffer.
I need to parse each line to load the buffer. So i use strtok.
The log file is a csv file with this form:
LUN 00:00,20.00,50.00
LUN 00:01,21.00,51.00
I think two lines is enough for testing purposes.
But i have a problem.
The code is this:
#include <CircularBuffer.h>
#include <string.h>
#include <LittleFS.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, 128> structs;
void getSensorData(){
tempD1 = random(20,30);
rHum = random(50, 60);
}
void wrBuff(){
LittleFS.remove("log.csv");
String ft = "MAR 00:00";
structs.push(data::record{ft, tempD1, rHum});
for (int j=0; j < structs.size(); j++){
data::print(structs[j]);
}
}
void load_chart(){
File chart = LittleFS.open("log.csv","r");
String ln;
byte index = 0;
char *str[4];
char *ptr = NULL;
String H;
float t;
float h;
char *linea;
while (chart.available()){
chart.readStringUntil('\n').toCharArray(linea, 25);
//linea[23] = '\n';
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});
Serial.println(H);
Serial.println(t);
Serial.println(h);
}
chart.close();
}
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();
}
void loop() {
// put your main code here, to run repeatedly:
if(millis() >= tPrevioSensor + sensor_delay){
getSensorData();
tPrevioSensor = millis();
}
if (millis() >= log_prevTime + log_time){
/*log_file(String(tempD1)+ "," + String(rHum));*/
wrBuff();
log_prevTime = millis();
}
}
The part that's not working is the linea variable. it doesnt get printed nor filled by the chart.readStringUntil("\n").toCharArray(linea, 25);
is esp8266.
I noticed the problem starts when load_chart() has to load 6 lines.
Before that it runs ok.
Im thinking esp8266 isnt big enough to load more than those six lines.
or i have a memmory leak somewhere