ESP8266 SPIFFS: Writing a file

In this tutorial we will check how to write a file to the SPIFFS file system of the ESP8266, using the Arduino core. The tests shown on this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board.

Introduction

In this tutorial we will check how to write a file to the SPIFFS file system of the ESP8266, using the Arduino core.

For an introductory tutorial on how to mount the SPIFFS file system on the ESP8266, please check here.

The tests shown on this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board. The version of the ESP8266 Arduino core used was 2.5.2.

The code

We will start by including the FS.h library, so we can have access to the SPIFFS extern variable, which we will use to interact with the file system.

#include <FS.h>

Then we will move to the Arduino setup function, where we will write the rest of our code. The first thing we will do is opening a serial connection, so we can output the results of our program.

Serial.begin(115200);

After that, we will mount the file system with a call to the begin method on the SPIFFS extern variable. Note that we always need to mount the file system before we start interacting with it.

We will also perform an error check to make sure the file system mounted properly before we try to write a file.

if (success) {
    Serial.println("File system mounted with success");
} else {
    Serial.println("Error mounting the file system");
    return;
}

Then we will take care of the creation of the file. To do it, we need to call the open method o the SPIFFS extern variable.

As first input we should pass a string with the absolute path of the file, starting with a forward slash [1]. We will call our file “file.txt“, which means the absolute path should be “/file.txt“.

As second input, the open method receives a string representing the file opening mode. Since we want to write some content to the file, we should use the mode “w“. Note that if the file doesn’t exist beforehand, it will be created [1].

For reference, you can check here the other available file opening modes.

As output, this method returns an object of class File, which we will store in a variable. We will later make use of it to write the content to the file.

File file = SPIFFS.open("/file.txt", "w");

As can be seen here, the File class overloads the C++ Boolean operator. Thus, we can use an IF condition to check if the file was opened with success or some error occured.

if (!file) {
    Serial.println("Error opening file for writing");
    return;
}

To write the content to the file, we can call the print method on our File object, passing as input the content we want to write. We will write a very simple testing string.

As output, this method will return the number of bytes written to the file. We can use this value for error checking.

Here, for simplicity, we are just assuming that if more than 0 bytes were written, then the procedure was successful. Naturally, this error checking can be improved to check if exactly the same number of characters we expect were written to the file.

int bytesWritten = file.print("TEST SPIFFS");

if (bytesWritten > 0) {
    Serial.println("File was written");
    Serial.println(bytesWritten);

} else {
    Serial.println("File write failed");
}

To finish, we should close the file after all the content is written. We do this with a call to the close method on the SPIFFS variable.

file.close();

The full source code can be seen below.

#include <FS.h>

void setup() {

  Serial.begin(115200);
  Serial.println();  

  bool success = SPIFFS.begin();

  if (success) {
    Serial.println("File system mounted with success");
  } else {
    Serial.println("Error mounting the file system");
    return;
  }

  File file = SPIFFS.open("/file.txt", "w");

  if (!file) {
    Serial.println("Error opening file for writing");
    return;
  }

  int bytesWritten = file.print("TEST SPIFFS");

  if (bytesWritten > 0) {
    Serial.println("File was written");
    Serial.println(bytesWritten);

  } else {
    Serial.println("File write failed");
  }

  file.close();

}

void loop() {}

Testing the code

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

You should get an output similar to figure 1. As can be seen, the file system mounted with success and afterwards the file was written. We can also see that a total of 11 bytes were written to the file, which matches the number of characters of the sentence defined in the code.

Output of the program for writing a file to the SPIFFS file system of the ESP8266.
Figure 1 – Writing a file to the SPIFFS file system of the ESP8266.

Related Posts

References

[1] https://arduino-esp8266.readthedocs.io/en/2.5.2/filesystem.html#open

2 Replies to “ESP8266 SPIFFS: Writing a file”

  1. I’ve run the code and so many other examples but non of then have worked inclouding this one. I’m using wemos d1 mini esp8266

Leave a Reply