File Management on ESP32: A Guide to LittleFS with PlatformIO

Ibrahim Bin Mansur
3 min readOct 26, 2024

--

In the world of IoT and embedded systems, efficient file management is crucial. Today, we’ll explore how to leverage LittleFS (Little File System) on the ESP32 using PlatformIO. We’ll cover everything from setting up your project to uploading files and reading them from your ESP32. Let’s dive in!

What is LittleFS?

LittleFS is a lightweight file system designed for microcontrollers with a small amount of flash memory. It’s perfect for devices like the ESP32, offering a balance between features and resource usage.

Setting Up Your Project

First, let’s set up a PlatformIO project for the ESP32 with LittleFS support.

Open PlatformIO in your preferred IDE (e.g., VS Code).

Create a new project:

  • Select ESP32 as your board
  • Choose Arduino as your framework

Open your platformio.ini file and add the following lines:

[env:ESP32_S3_DEV_4MB_QD_No_PSRAM]
platform = espressif32
board = ESP32_S3_DEV_4MB_QD_No_PSRAM
framework = arduino
monitor_speed = 115200

board_build.partitions = min_littlefs.csv
board_build.filesystem = littlefs

This configuration sets up LittleFS and adjusts the partition scheme to allocate space for our file system.

Creating and Uploading Files

Now, let’s add some files to our ESP32:

In your project root, create a folder named data.

Add files you want to upload to this folder. For example:

  • cert.pem: A sample certificate
  • hello.txt: A simple text file

To upload these files, use PlatformIO’s “Upload Filesystem Image” task. You can find this in the PlatformIO sidebar under “PROJECT TASKS” > “esp32dev” > “Platform” > “Upload Filesystem Image”.

Select Upload File System Image
Build Image
Uploading the Image

Reading Files from LittleFS

With our files uploaded, let’s write some code to read them:

#include <Arduino.h>
#include <FS.h>
#include <LittleFS.h>

void readFile(const char* path) {
File file = LittleFS.open(path, "r");
if (!file) {
Serial.printf("Failed to open file %s for reading\n", path);
return;
}
Serial.printf("\nContents of %s:\n", path);
while (file.available()) {
Serial.write(file.read());
}
file.close();
}

void setup() {
Serial.begin(115200);
if (!LittleFS.begin()) {
Serial.println("LittleFS Mount Failed");
return;
}
readFile("/cert.pem");
readFile("/hello.txt");
LittleFS.end();
}

void loop() {
// Your main code here
}

This code does the following:

  1. Initializes the serial communication and mounts LittleFS.
  2. Defines a readFile function that opens a file, reads its contents, and prints them to the serial monitor.
  3. In the setup function, it reads and prints the contents of our uploaded files.
Contents

Putting It All Together

  1. Upload your code to the ESP32.
  2. Open the Serial Monitor (baud rate: 115200).
  3. You should see the contents of your files printed!

If you do not upload the image and run code you should see this:

Conclusion

Congratulations! You’ve successfully set up LittleFS on your ESP32, uploaded files, and read them using PlatformIO. This powerful combination allows you to manage configuration files, store data, and even handle web assets directly on your ESP32.

Remember, LittleFS is just one of many tools in your IoT toolkit. As you continue to explore, you’ll find even more ways to optimize your ESP32 projects.

References

--

--

No responses yet