2019年7月30日星期二

ESP8266 SPIFFS: Appending content to file

In this tutorial, we will learn how to append content to a file on the ESP8266 SPIFFS file system. The tests shown on this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board.


Introduction

In this tutorial we will learn how to append content to a file on the ESP8266 SPIFFS file system.

We will create the file beforehand to make sure we have one available in our file system, so we can append content to it.

The tests shown on this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board.

The code

The first thing we will do is importing the FS.h library, so we have access to all the file system related functionalities we need.

#include "FS.h"
Moving on to the Arduino setup, we will start by opening a serial connection. Then we will mount the file system, since we always need to do it before we start interacting with it.

Serial.begin(115200);
Serial.println();
  
if(!SPIFFS.begin()){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
}
After that, we will create a file called “test.txt”, to make sure we have a file to which we can later append content. You can check in detail how to write a file on this previous post.

File fileToWrite = SPIFFS.open("/test.txt", "w");
  
if(!fileToWrite){
    Serial.println("There was an error opening the file for writing");
    return;
}
  
if(fileToWrite.println("ORIGINAL FILE LINE")){
    Serial.println("File was written");
} else {
    Serial.println("File write failed");
}

Now that we have the file in our file system, we will open it in appending mode. This means that any content we add to the file will be appended to the already existing content.

To open the file in appending mode, we call again the open method on the SPIFFS extern variable, passing as first input the name of the file and as second the value “a”.

Recall from previous tutorials that the second argument of the open method indicates the file opening mode. In this case, the value “a” means that we are opening the file in appending mode.

File fileToAppend = SPIFFS.open("/test.txt", "a");

After this, we will check if there was no error during the opening procedure. Since the File class overloads the C++ Boolean operator, we can check if some problem has occurred by enclosing in an IF condition the File object returned by the open method.

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

Then we will write more content to the file, which should now be appended. We can do it by calling the println method on our File object and passing as input the content to write. Note that this method adds a new line character at the end of the content.

We are also going to do an error check to make sure some content was written to the file.

if(fileToAppend.println("APPENDED FILE LINE")){
     Serial.println("File content was appended");
} else {
     Serial.println("File append failed");
}
Then we will close the file with a call to the close method on our File object.

fileToAppend.close();

To finalize the code, we will now open the file in reading mode and print all its content to the serial port, to make sure the original content was kept and the new one was appended, as expected.

You can check here a detailed tutorial on how to read content from a file.

File fileToRead = SPIFFS.open("/test.txt", "r");
  
if(!fileToRead){
    Serial.println("Failed to open file for reading");
    return;
}
  
Serial.println("File Content:");
  
while(fileToRead.available()){
  
    Serial.write(fileToRead.read());
}
  
fileToRead.close();
The final complete code can be seen below.

#include "FS.h"
  
void setup() {
  
   Serial.begin(115200);
   Serial.println();
  
   if(!SPIFFS.begin()){
        Serial.println("An Error has occurred while mounting SPIFFS");
        return;
   }
  
    //--------- Write to file ---------------
    File fileToWrite = SPIFFS.open("/test.txt", "w");
  
    if(!fileToWrite){
        Serial.println("There was an error opening the file for writing");
        return;
    }
  
    if(fileToWrite.println("ORIGINAL FILE LINE")){
        Serial.println("File was written");
    } else {
        Serial.println("File write failed");
    }
  
    fileToWrite.close();
  
    //--------- Apend to file --------------
    File fileToAppend = SPIFFS.open("/test.txt", "a");
  
    if(!fileToAppend){
        Serial.println("There was an error opening the file for appending");
        return;
    }
  
    if(fileToAppend.println("APPENDED FILE LINE")){
        Serial.println("File content was appended");
    } else {
        Serial.println("File append failed");
    }
  
    fileToAppend.close();
  
    //---------- Read from file --------------
    File fileToRead = SPIFFS.open("/test.txt", "r");
  
    if(!fileToRead){
        Serial.println("Failed to open file for reading");
        return;
    }
  
    Serial.println("File Content:");
  
    while(fileToRead.available()){
  
        Serial.write(fileToRead.read());
    }
  
    fileToRead.close();
}
  
void loop() {}

Testing the code

To test the code, first compile it and upload it to your device using the Arduino IDE. Then, when the procedure finishes, open the IDE serial monitor.

You should get an output similar to figure 1. As can be seen, the original content was kept and the new one was appended.

Figure 1 – Output of the program

没有评论:

发表评论