0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino Tutorial 27. FAT file system: Append content to file

DFRobot Feb 02 2019 3875

Introduction

In this tutorial we will check how to append content to a file created on the ESP32 FAT file system, using the Arduino core. For an introductory tutorial on the FAT file system, please check here.

Note that the procedure of appending content to a file on the FAT file system is very similar to the one used on the SPIFFS file system, which was covered here.

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

The code

As usual, we will start the code by including the FFat.h library, which exposes the functionality needed to interact with the FAT file system on the ESP32.

#include "FFat.h"

Moving on to the Arduino setup, we will open a serial connection to later output the content we will read from a file. After that, we will mount the FAT file system by calling the begin method of the FFat extern variable.

Serial.begin(115200);

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

After the mounting procedure, we will create a file and write some text to it, so we can later append more content.

As covered here in detail, we first need to open the file in writing mode. We do this by calling the open method on the FFat extern variable, passing as first input the name of the file and as second the constant FILE_WRITE, which specifies the file will be opened in writing mode. As output, this method call will return an object of class File.

After doing an error check with an IF condition to confirm the file was correctly opened, we will write some content to it by calling the println function on our File object. This method receives as input a string with the content to write to the file and it will append a newline at the end.

When the writing procedure is finished, we should call the close method on our File object, to free the resources.

File fileToWrite = FFat.open("/test.txt", FILE_WRITE);

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

if(fileToWrite.println("ORIGINAL CONTENT")){
    Serial.println("File was written");;
} else {
    Serial.println("File write failed");
}

fileToWrite.close();

Now, in order to append some content to the file, we need to open it again. As before, we use the open method of the FFat extern variable, passing as first input the name of the file and as second the opening mode. This time, in order to open it for appending, we pass the constant FILE_APPEND.

As before, after opening the file, we perform an error check on the returned File object with an IF condition. Note that this is possible because the File class overloads the C++ Boolean operator

File fileToAppend = FFat.open("/test.txt", FILE_APPEND);

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

Now we simply write the new content to the file, like we did before, using the println method. Nonetheless, due to the fact that the file was opened in appending mode, instead of the new content overwriting the previous, it will be appended.

if(fileToAppend.println("APPENDED LINE")){
    Serial.println("Content was appended");
} else {
    Serial.println("File append failed");
}

Again, after the procedure, we will close the file.

fileToAppend.close();

Finally, we will read the whole content of the file. The reading procedure is detailed in this previous tutorial. In short, we open again the file in reading mode (note that this is the default mode when opening a file, when we don’t explicitly pass the mode as second argument of the open method).

After that, we will read the content byte by byte, in a loop, and print it to the serial port. When we finish reading the whole content, we close the file.

File fileToRead = FFat.open("/test.txt");

if(!fileToRead){
    Serial.println("Failed to open file for reading");
    return;
}

Serial.println("Final file Content:");

while(fileToRead.available()){

    Serial.write(fileToRead.read());
}

fileToRead.close();

The final code can be seen below.

#include "FFat.h"

void setup() {

   Serial.begin(115200);

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

    //--------- Write to file
    File fileToWrite = FFat.open("/test.txt", FILE_WRITE);

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

    if(fileToWrite.println("ORIGINAL CONTENT")){
        Serial.println("File was written");;
    } else {
        Serial.println("File write failed");
    }

    fileToWrite.close();

    //--------- Apend content to file
    File fileToAppend = FFat.open("/test.txt", FILE_APPEND);

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

    if(fileToAppend.println("APPENDED LINE")){
        Serial.println("Content was appended");
    } else {
        Serial.println("File append failed");
    }

    fileToAppend.close();

    //---------- Read file
    File fileToRead = FFat.open("/test.txt");

    if(!fileToRead){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.println("Final file Content:");

    while(fileToRead.available()){

        Serial.write(fileToRead.read());
    }

    fileToRead.close();
}

void loop() {}

Testing the code

As usual, to test the code, simply compile it and upload it to the Arduino IDE and, when the procedure finishes, open the serial monitor. You should get an output similar to figure 1, which shows the final content of the file, composed by the first line we have written and by the second line we appended after.

Output of the file created in the ESP32 file system, after appending content

Figure 1 – Output of the program, showing the content of the file.