ESP8266 Arduino: Mounting the SPIFFS file system

In this tutorial we will check how to mount the ESP8266 SPIFFS file system, using the Arduino core. The tests from this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board.

Introduction

In this tutorial we will check how to mount the ESP8266 SPIFFS file system, using the Arduino core.

SPIFFS was selected as the file system of the ESP8266 to accommodate the constraints o the device, including the limited RAM [1]. You can read more about the ESP8266 file system here. You can also learn more about SPIFFS here.

In this introductory tutorial we will only check how to mount the SPIFFS file system. Nonetheless, this is a very important procedure since we need to execute it before we use any other SPIFFS file system function [1].

The tests from this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board. The version of the ESP8266 Arduino core used was 2.5.2.

The code

We will start our code by including the FS.h library. This library will expose the functionalities we need to mount the file system.

#include <FS.h>

After this we can move to the Arduino setup function, where we will write the rest of our code. The first thing we will do is opening a serial connection, so we can output content from our program.

Serial.begin(115200);

After including the FS.h library, like we did at the beginning of our code, we will have access to an extern variable called SPIFFS. We will use this variable to interact with the file system.

For this simple example we only want to mount the file system. This is done with a call to the begin method on the SPIFFS variable.

This method takes no arguments and it will return a Boolean value indicating if the file system was mounted successfully (true) or not (false) [1].

Note that this method call will trigger a file system format if it is unable to mount it on the first try [1].

We will store the result of this method call in a variable so we can do an error check afterwards.

bool success = SPIFFS.begin();

To finalize, we will check if the mounting procedure occurred successfully, using the variable we have just saved. We will print an informative message to the serial port, indicating the result.

if(success){
    Serial.println("File system mounted with success");  
}else{
    Serial.println("Error mounting the file system");  
}

The final complete code can be seen below. The main loop was left empty since, for this introductory example, we just want to test if the mounting procedure executes correctly.

#include <FS.h>  

void setup() {
  
  Serial.begin(115200);
  
  bool success = SPIFFS.begin();

  if(success){
    Serial.println("File system mounted with success");  
  }else{
    Serial.println("Error mounting the file system");  
  }
}

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP8266 device, using the Arduino IDE. Once the procedure finishes, open the IDE serial monitor.

You should get an output similar to figure 1, which illustrates the success message getting printed.

Output of the program on the Arduino IDE serial monitor, showing success on mounting the ESP8266 SPIFFS file system.
Figure 1 – Output of the program, showing success on the ESP8266 SPIFFS file system mount procedure.

Related content

References

[1] https://arduino-esp8266.readthedocs.io/en/2.5.2/filesystem.html

Leave a Reply

%d bloggers like this: