$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: Formatting the SPIFFS file system

DFRobot Mar 14 2019 738

In this esp32 tutorial we will check how to format the SPIFFS file system of the ESP32, using the Arduino core. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this esp32 tutorial we will check how to format the SPIFFS file system of the ESP32, using the Arduino core.

For this tutorial, you should have some files beforehand on the file system. You can use this Arduino IDE plugin to upload files. For a detailed tutorial on how to use it, please check here.

Note that the path and the content of the files don’t matter for this tutorial, as the formatting procedure will erase everything. If you have any important file in your file system, please don’t follow this tutorial before you can save it in other place.

Just for completion, this is the folder structure I will be using:

|– emptyFolder
|– folder1
|– — other_file.txt
|– — nested
|– — — nested.txt
|– folder2
|– — some_file.txt
|– file.txt

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

We will start by including the SPIFFS.h library, so we have access to the SPIFFS extern variable.

#include "SPIFFS.h"

Then we will declare a function called listAllFiles that will list all the files of the SPIFFS file system. You can check in detail how to list all the files of the file system on this previous tutorial.

In short, in this function, we will open the root directory “/” and then we will iterate through all the files by calling the openNextFile method. In each iteration we will print the name of the current file.

void listAllFiles(){
 
  File root = SPIFFS.open("/");
 
  File file = root.openNextFile();
 
  while(file){
 
      Serial.print("FILE: ");
      Serial.println(file.name());
 
      file = root.openNextFile();
  }
 
}

Moving on to the setup function, we will start by opening a serial connection, so we can print the results of running our program.

Serial.begin(115200);

Then we will mount the SPIFFS file system, so we can use it.

if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
}

After this, we will list all the files in the SPIFFS file system, before we format it.

Serial.println("\n\n----Listing files before format----");
listAllFiles();

After this, we will list all the files in the SPIFFS file system, before we format it.

Serial.println("\n\n----Listing files before format----");
listAllFiles();

Then, we will format the file system by calling the format method on the SPIFFS object. This method takes no arguments and returns as output a Boolean value indicating if the procedure was successful (true) or failed (false). We will store the returned value for error checking.

bool formatted = SPIFFS.format();

Then, we will do an error checking on the returned value to confirm if the procedure was successful. We will print a message indicating to the user the result of the operation.

if(formatted){
    Serial.println("\n\nSuccess formatting");
}else{
    Serial.println("\n\nError formatting");
}

To finalize, we will print again the files on the SPIFFS file system, to confirm they were indeed deleted. This second call should print an empty list.

Serial.println("\n\n----Listing files after format----");
listAllFiles();

The final source code can be seen below.

#include "SPIFFS.h"
 
void listAllFiles(){
 
  File root = SPIFFS.open("/");
 
  File file = root.openNextFile();
 
  while(file){
 
      Serial.print("FILE: ");
      Serial.println(file.name());
 
      file = root.openNextFile();
  }
 
}
 
void setup() {
 
  Serial.begin(115200);
 
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
  Serial.println("\n\n----Listing files before format----");
  listAllFiles();
 
  bool formatted = SPIFFS.format();
 
  if(formatted){
    Serial.println("\n\nSuccess formatting");
  }else{
    Serial.println("\n\nError formatting");
  }
 
  Serial.println("\n\n----Listing files after format----");
  listAllFiles();
}
 
void loop() {}

Testing the code

To test the code, first open the Arduino IDE serial monitor to make sure you don’t loose the output of the program. Then, compile the code and upload it to your ESP32, assuming that you previously have some files in the SPIFFS file system.

You should get an output similar to figure 1 on the serial monitor. As can be seen, before the formatting procedure, the files are listed. After formatting, the previously existing files are erased and thus nothing gets printed.

Note that the formatting procedure may take a while, so you will need to wait some time until the empty list gets printed.


Reference: https://techtutorialsx.com/2019/02/24/esp32-arduino-formatting-the-spiffs-file-system/


REVIEW