In this tutorial we will check how to read content from a file using the SPIFFS file system of the ESP32, running the Arduino core. For a detailed tutorial on how to write a file, please check the previous tutorial.
In the code below we will write the file before reading it but if you have already followed the previous tutorial, you can skip the writing file part, since the file should have been persisted in the SPIFFS file system.
The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.
We start the code by including the SPIFFS.h library, so we have access to the methods needed to both write and read from a file. Remember from the previous tutorial that, by including this library, we will have access to the SPIFFS extern variable, which will be the one used to interact with the file system.
#include "SPIFFS.h"
Moving on to the Arduino setup function, we start by initializing a serial connection, so we can later print the content read from the file.
Serial.begin(115200);
Now we will mount the file system by calling the begin method on the SPIFFS extern variable, passing as input the value true, which ensures the file system will be formatted in case mounting fails.
Since the call to the begin method returns true in case the file system mounting is successful and false otherwise, we will also do an error check to know everything was correctly initialized.
if(!SPIFFS.begin(true)){ Serial.println("An Error has occurred while mounting SPIFFS"); return; }
Now we will take care of writing to a file called “/test.txt“, accordingly to what we have covered in the previous tutorial. If you have followed the previous tutorial and already created the file, you can skip this part of the code.
So, we will start by opening the file for writing, calling the open method on the SPIFFS object. As first input we pass the name of the file and as second we pass the constant FILE_WRITE, so the file is opened in writing mode.
This method call will return an object of class File, which we will use to write the content.
In case the file is successfully opened, we will then call the print method on the File object, passing as input the string with the content to write.
We will finally call the close method on the File object, in order to close the file.
File file = SPIFFS.open("/test.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();
Now that we have created the file and written some content, we will open it to read its content.
In order to open the file for read, we call again the open method on the SPIFFS extern variable, passing as input the name of the file (remember it was “/test.txt”). As can be seen in this header file, the default opening mode of a file is reading, so we don’t need to pass the second argument that indicates the opening mode.
Again, this will return an object of class File. Remember from the previous tutorial that the File class overrides the Boolean C++ operator, so we can use an if condition to check if the file was successfully opened.
File file2 = SPIFFS.open("/test.txt"); if(!file2){ Serial.println("Failed to open file for reading"); return; }
We will now read the content from the file in a loop. We will call the available method on the File object to check how many bytes are left to read, and use the returned value as stopping condition of the reading loop.
To read a byte from the file, we call the read method of the File object, and print the result to the serial port.
while(file2.available()){ Serial.write(file2.read()); }
After reading all the bytes, we close the file by calling the close method on the File object.
file2.close();
The final source code can be seen below.
#include "SPIFFS.h" void setup() { Serial.begin(115200); if(!SPIFFS.begin(true)){ Serial.println("An Error has occurred while mounting SPIFFS"); return; } File file = SPIFFS.open("/test.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(); File file2 = SPIFFS.open("/test.txt"); if(!file2){ Serial.println("Failed to open file for reading"); return; } Serial.println("File Content:"); while(file2.available()){ Serial.write(file2.read()); } file2.close(); } void loop() {}
To test the code, compile it and upload it to your ESP32. After the procedure is finished, open the Arduino IDE serial monitor. You should get an output similar to figure 1, which shows the content of the file being printed to the monitor.
Figure 1 – Reading a file from the ESP32 SPIFFS file system.
DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.