SPIFFS (SPI Flash File System) enables developers working with microcontrollers like the ESP32'to store files directly on the device's onboard flash memory.
This is incredibly useful for storing static assets such as configuration files, credentials, images, or text that the microcontroller can read at runtime.
In this tutorial, we'll show how to upload a file to the ESP32 using Visual Studio Code with the PlatformIO plugin. The same approach can also be used in the Arduino IDE, which offers a similar SPIFFS file upload feature.
Why Use SPIFFS?
Storing data directly on your microcontroller via SPIFFS avoids hardcoding values or relying on external memory modules. You can upload:
- Text configuration files (e.g., Wi-Fi credentials)
- Images
- MP3/MP4 files
- Or any file that your microcontroller logic can interpret
Steps to Upload a SPIFFS File Using PlatformIO
- Create a folder named
data
in the root directory of your PlatformIO project. - Place the file(s) you wish to upload into the
data
folder. This could be a text file likeconfig.txt
or an image. - Use the PlatformIO upload task to flash the data:
PlatformIO > Upload File System image
This will copy the contents of thedata
folder into the ESP32's internal flash memory.
Reading the SPIFFS File in Code
After uploading, your ESP32 sketch can interact with the file system:
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS Mount Failed");
return;
}
File file = SPIFFS.open("/config.txt");
if (!file) {
Serial.println("Failed to open file");
return;
}
while (file.available()) {
Serial.write(file.read());
}
file.close();
Using Arduino IDE
The Arduino IDE also supports SPIFFS uploads via the ESP32 Sketch Data Upload plugin. Once installed, you can:
- Create a
data
folder in your project directory. - Place your files in that folder.
- Go to Tools > ESP32 Sketch Data Upload to flash the files to SPIFFS.
Video Tutorial
For visual learners, check out this helpful walkthrough: