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

ESP32 Arduino SPIFFS: Getting total bytes used

DFRobot Apr 09 2019 965

In this esp32 tutorial we will check how to get the total used bytes in the SPIFFS file system of the ESP32, using the Arduino core.

Introduction

In this tutorial we will check how to get the total used bytes in the SPIFFS file system of the ESP32, using the Arduino core.

In the code below we will first obtain the number of bytes used after mounting the file system. After that, we will create a new file and obtain again the bytes used, to confirm the value increased.

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

The code

To get started we will include the SPIFFS.h library, so we can access the file system related functionalities.

#include "SPIFFS.h"

Then we will move on to the Arduino setup function where the first thing we will do is opening a serial connection, to output some content from our program.

Serial.begin(115200);

After that we will mount the SPIFFS file system, a procedure that we need to do before using it. We will also do an error check to make sure the mounting procedure was correctly executed.

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

After this we will check the number of used bytes in the file system by calling the usedBytes method on the SPIFFS object. Note that, internally, this method calls the esp_spiffs_info function from IDF.

The usedBytes method takes no arguments and returns as output the total number of bytes used, which we will directly print to the serial port.

Serial.println(SPIFFS.usedBytes());

After this we will create a new file in the SPIFFS file system, to confirm that the number of used bytes will increase. For a detailed tutorial on how to write a file, please check here.

We will create a file called “/test_file.txt” and write a small testing string as its content. The full procedure for writing the file can be seen below.

File file = SPIFFS.open("/test_file.txt", FILE_WRITE);
 
if (!file) {
    Serial.println("There was an error opening the file for writing");
    return;
}
if (file.print("TEST")) {
    Serial.println("File was written");
} else {
    Serial.println("File write failed");
}
 
file.close();

To finalize, we will print the total number of used bytes again, so we can compare with the previous value, before the file creation. The number printed should now be bigger.

Serial.println(SPIFFS.usedBytes());

The final source code can be seen below. It contains some additional prints to facilitate the interpretation when testing the code.

#include "SPIFFS.h"
 
void setup() {
 
  Serial.begin(115200);
 
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
  Serial.print("Used bytes before write:");
  Serial.println(SPIFFS.usedBytes());
 
  File file = SPIFFS.open("/test_file.txt", FILE_WRITE);
 
  if (!file) {
    Serial.println("There was an error opening the file for writing");
    return;
  }
  if (file.print("TEST")) {
    Serial.println("File was written");
  } else {
    Serial.println("File write failed");
  }
 
  file.close();
 
  Serial.print("Used bytes after write:");
  Serial.println(SPIFFS.usedBytes());
}
 
void loop() {}

Testing the code

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

You should get a result similar to figure 1, which shows the number of used bytes before and after the creation of the file. Naturally, the values you will get will most likely differ from mine depending on how many files previously existed on your file system.

As expected, the number of bytes used increased after the creation of the file.

Note that even though we have written a very small string to the file, the number of bytes occupied in the file system has increased much more. You can read here more about the file system structure and the factors that influence the size occupied by a file, such as the page size or the additional metadata related to the file [1].

REVIEW