/*
*
* LittleFS_FILE-Test01.ino
*
* https://wokwi.com/projects/419678453022640129
*
*
*
* Source code: https://randomnerdtutorials.com/esp32-write-data-littlefs-arduino/
*
*
* ::: WARNING ::: WARNING ::: WARNING ::: WARNING ::: WARNING ::: WARNING :::
*
* ONLY on Wokwi simulator:
* From the tab -Library Manager- install << LittleFS_esp32.h >>
* [[-LittleFS.h- is not listed yet]]
*
* ::: WARNING ::: WARNING ::: WARNING ::: WARNING ::: WARNING ::: WARNING :::
*
*
*/
#include <Arduino.h>
#include "FS.h"
#include <LittleFS.h>
#define FORMAT_LITTLEFS_IF_FAILED true
// *** functions prototypes declaration *************************************
void writeFile(fs::FS &fs, const char *path, const char *message);
void appendFile(fs::FS &fs, const char *path, const char *message);
void readFile(fs::FS &fs, const char *path);
// **************************************************************************
// **************************************************************************
void writeFile(fs::FS &fs, const char * path, const char * message){
//
Serial.printf("Writing file: %s\r\n", path);
//
File file = fs.open(path, FILE_WRITE);
//
if (!file) {
//
Serial.println("- failed to open file for writing");
return;
//
} // if
//
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
} // if-else
//
file.close();
//
} // writeFile()
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("\nAppending to file: %s\r\n", path);
//
File file = fs.open(path, FILE_APPEND);
//
if (!file) {
//
Serial.println("- failed to open file for appending");
return;
//
} // if
//
if(file.print(message)){
Serial.println("- message appended");
} else {
Serial.println("- append failed");
} // if-else
//
file.close();
//
} // appendFile()
void readFile(fs::FS &fs, const char * path) {
//
Serial.printf("Reading file: %s\r\n", path);
//
File file = fs.open(path);
//
if (!file || file.isDirectory()) {
//
Serial.println("- failed to open file for reading");
return;
//
} // if
//
Serial.println("- read from file:");
//
while(file.available()) Serial.write(file.read());
//
file.close();
//
} // readFile()
// **************************************************************************
// **************************************************************************
void setup() {
//
Serial.begin(115200);
//
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
//
Serial.print(F("\nLittleFS Mount Failed\n"));
return;
//
} else {
Serial.print(F("\nLittle FS Mounted Successfully\n"));
} // if-else
//
// Check if the file already exists to prevent overwritting existing data
bool fileexists = LittleFS.exists("/data.txt");
//
if (!fileexists) {
//
Serial.print(F("File doesn’t exist\nCreating file...\n"));
// Create File and add header
writeFile(LittleFS, "/data.txt", "MY ESP32 DATA \r\n");
//
} else {
Serial.print(F("File already exists.\n"));
} // if-else
//
readFile(LittleFS, "/data.txt");
//
} // setup()
void loop() {
//
static int mydata;
//
mydata = random (0, 1000);
//
String s_Mydata = "\r\n";
s_Mydata += String(mydata);
//Append data to the file
appendFile(LittleFS, "/data.txt", s_Mydata.c_str());
// Read the contents of the file
readFile(LittleFS, "/data.txt");
//
delay(30000);
//
} // loop()
/*
Serial Monitor Output:
E (548) esp_core_dump_flash: No core dump partition found!
E (549) esp_core_dump_flash: No core dump partition found!
E (1) esp_littlefs: ./managed_components/joltwallet__littlefs/src/littlefs/lfs.c:1384:error: Corrupted dir pair at {0x0, 0x1}
E (1) esp_littlefs: mount failed, (-84)
E (1) esp_littlefs: Failed to initialize LittleFS
E (589) task_wdt: esp_task_wdt_reset(705): task not found
E (2451) task_wdt: esp_task_wdt_reset(705): task not found
E (2452) task_wdt: esp_task_wdt_reset(705): task not found
E (2453) task_wdt: esp_task_wdt_reset(705): task not found
E (2456) task_wdt: esp_task_wdt_reset(705): task not found
E (2461) task_wdt: esp_task_wdt_reset(705): task not found
E (2479) task_wdt: esp_task_wdt_reset(705): task not found
E (2480) task_wdt: esp_task_wdt_reset(705): task not found
E (2481) task_wdt: esp_task_wdt_reset(705): task not found
E (2484) task_wdt: esp_task_wdt_reset(705): task not found
E (2489) task_wdt: esp_task_wdt_reset(705): task not found
Little FS Mounted Successfully
File doesn’t exist
Creating file...
Writing file: /data.txt
- file written
Appending to file: /data.txt
- message appended
Reading file: /data.txt
- read from file:
MY ESP32 DATA
911
Appending to file: /data.txt
- message appended
Reading file: /data.txt
- read from file:
MY ESP32 DATA
911
390
943
...
*/