Page 1 of 1

LittleFs and PlatformIO ... how to use both?

Posted: Tue Feb 15, 2022 6:45 pm
by alex_esp32
Hi, I am very confused by the many different tutorials how to use LittleFS on PlatformIO for ESP32.
I have tried the instruction on Github: https://github.com/lorol/LITTLEFS/tree/ ... latformIO however I had problems to build the code. I had some build errors.

Than I opened a new project and tested this instruction: https://techoverflow.net/2021/12/16/how ... s-library/



By simply adding this line to platform.io
lib_deps =
lorol/LittleFS_esp32 @ ^1.0.6


I could run this simple test code:
Is it really so simple? Is this right now the best way to use LittleFS on PlatformIO? I have also added mklittlefs.exe and partitions_custom.csv to my root project folder. But how can I use it from PlatformIO?

Code: Select all

#include <Arduino.h>
#include <LITTLEFS.h>
#define SPIFFS LITTLEFS

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;
    }

    Serial.println("- read from file:");
    while(file.available()){
        Serial.write(file.read());
    }
    file.close();
}


void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending 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(file.print(message)){
        Serial.println("- message appended");
    } else {
        Serial.println("- append failed");
    }
    file.close();
}


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(file.print(message)){
        Serial.println("- file written");
    } else {
        Serial.println("- write failed");
    }
    file.close();
}

void deleteFile(fs::FS &fs, const char * path){
    Serial.printf("Deleting file: %s\r\n", path);
    if(fs.remove(path)){
        Serial.println("- file deleted");
    } else {
        Serial.println("- delete failed");
    }
}


void setup() {
    Serial.begin(115200);
  // put your setup code here, to run once:
  if (!LITTLEFS.begin(false /* false: Do not format if mount failed */)) {
  Serial.println("Failed to mount LittleFS");
  if (!LITTLEFS.begin(true /* true: format */)) {
    Serial.println("Failed to format LittleFS");
  } else {
    Serial.println("LittleFS formatted successfully");
  }
} else { // Initial mount success
 Serial.println("Initial mount success");
}

 deleteFile(LITTLEFS, "/hello.txt");
 writeFile(LITTLEFS, "/hello.txt", "Hello ");
 
 appendFile(LITTLEFS, "/hello.txt", "World!\r\n");
 readFile(LITTLEFS, "/hello.txt");
}

void loop() {
  // put your main code here, to run repeatedly:
}