#include "LittleFS.h"
const String FILENAME="/test.txt";
int readData() {
File f = LittleFS.open(FILENAME, "r");
if (!f) {
Serial.println("File open failed on read.");
} else {
while(f.available()) {
//Lets read line by line from the file
String line = f.readStringUntil('\n');
return line.toInt();
break; //if left in, we'll just read the first line then break out of the while.
}
f.close();
}
return -1;
}
void writeData(int data) {
//opening "w" will truncate the file, so we know we only store our 1 value.
File f = LittleFS.open(FILENAME, "w");
if (!f) {
Serial.println("File open failed on update.");
delay(10000);
} else {
f.println(data);
f.close();
Serial.println("File updated");
}
}
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
int loadedData = readData();
Serial.print("Stored data is ");
Serial.print(loadedData);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
writeData(millis());
}