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

ESP32 Tutorial Arduino: 19 FAT file system: writing a file

DFRobot Jan 25 2019 713

Introduction

In this tutorial we will check how to create and write to a file on the ESP32 FAT file system. Please check this previous tutorial which explains the procedure that needs to be executed before starting to use the ESP32 FAT file system.

As can be here, the F_Fat class, which we use to interact with the FAT file system, extends the FS class. The same way, the SPIFFSFS class that we use to interact with a SPIFFS file system also extends the FS class, as covered here.

This means that they have a method with the same signature to open a file from the file system, making the procedure for writing a file in the FAT file system very similar to the one we have followed here for the SPIFFS file system.

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

The code

The first thing we will need to do is including the “FFat.h”, so we have access to the FFat extern variable, which is used to interact with the file system.

#include "FFat.h"

Moving on to the Arduino setup function, we start by opening a serial connection, to output the results of our program.

Serial.begin(115200);

After that, we need to mount the FAT file system before we can start creating a file and writing content to it. This procedure is covered in detail in this previous post.

As indicated there, we need to call the begin method on the FFat object and, as a safeguard, we can pass as input the Boolean value true to ensure the file system is formatted in case the mounting procedure fails. Formatting the file system is needed the first time we use it

Since this method returns a Boolean value indicating if the procedure was successful or not, we will use that value for error checking.

if(!FFat.begin(true)){
     Serial.println("Mount Failed");
     return;
}

Serial.println("File system mounted");

After mounting the file system, we need to call the open method on the FFat object, passing as first input the name of the file we want to write. We will call our file “/testfat.txt”. Note that if the file doesn’t exist beforehand, then it will be automatically created.

As second argument of the open method, we need to specify the opening mode. In our case, since we want to write content, we use the constant FILE_WRITE.

As output, this method call will return an object of class File, which we will use from this point onward to write the content.

File file = FFat.open("/testfat.txt", FILE_WRITE);

Note that this File class is exactly the same that is returned when reading a file from the SPIFFS file system, like we covered here. This means the methods and behavior is the same. So, as we have seen in that tutorial, the File class overloads the C++ Boolean operator, which means we can check if the file was opened with success using an IF condition.

if (!file) {
   Serial.println("There was an error opening the file for writing");
   return;
}

Now, to write some content, we can use the print method of our File object, passing as input the content to be written. We will write a simple test string.

This method returns as output the number of bytes written to the file, which we an use for error checking, as shown below.

if (file.print("TEST")) {
   Serial.println("File was written");
} else {
   Serial.println("File write failed");
}

To finalize, we need to close the file with a call to the close method, which receives no arguments.

file.close();

The final code can be seen below.

#include "FFat.h"

void setup(){

    Serial.begin(115200);

    if(!FFat.begin(true)){
        Serial.println("Mount Failed");
        return;
    }

    Serial.println("File system mounted");

    File file = FFat.open("/testfat.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();

}

void loop(){}

Testing the code

To test the code, compile it and upload it to your device using the Arduino IDE with support for the ESP32 Arduino core. After the uploading procedure finishes, open the Arduino IDE serial monitor. You should get an output like figure 1, which indicates the writing procedure was successful.

Arduino IDE showing result of writing to a file in the ESP32 FAT file system

Figure 1 – Output of the program.

REVIEW