In this tutorial we will learn how to read the content of file from the ESP8266 SPIFFS file system, using the Arduino core. The tests shown on this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board.
The code
In this tutorial we will learn how to read the content of file from the ESP8266 SPIFFS file system, using the Arduino core.
We will create the file beforehand, to make sure we have content available to read. The procedure on how to create a file on the ESP8266 file system was covered in detail on the previous tutorial.
The tests shown on this tutorial were performed on a DFRobot’s ESP8266 FireBeetle board.
Introduction
The first thing we will do is importing the FS.h library, like we have been doing in previous tutorials. This way, we will have access to the SPIFFS extern variable, which we can use to interact with the ESP8266 file system.
#include <FS.h>
Then we will move on to the Arduino Setup function, where we will start by opening a serial connection. We will use it to later output the results of our program.
Serial.begin(115200);
Serial.println();
After this we will mount the SPIFFS file system. This procedure always needs to be executed before we start to interact with the file system.
if (!success) {
Serial.println("Error mounting the file system");
return;
}
Then we will write a file in the file system, to make sure we have some content to read. The procedure on how to write a file was covered in detail on this previous article.
In short, to write a file to the SPIFFS file system of the ESP8266, we first need to open it in writing mode. This is done with a call to the openmethod on the SPIFFS variable, passing as first input the name of the file and as second input the opening mode.
Note that we will call our file “/file.txt“, which means that we need to use this same name when later opening it for reading.
After opening the file in writing mode and checking that no error occurred during the procedure, we can write some content to it with a call to the print method on our File object.
This method takes as argument a string with the content to write to the file and returns as output the number of bytes written. We can use the returned value to perform an error check.
Finally, once the content is written to the file, we can close it with a call to the close method, also on the File object.
The full code for writing the file can be seen below.
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 write failed");
return;
}
file.close();
Now that we have handled the creation of the file, we can read its content and print it to the serial port.
The first thing we need to do is opening the file with a call to the open method on the SPIFFS object. We should use the file name mentioned before (“/file.txt”) and this time we should open it in reading mode.
So, to open a file in reading mode, we need to pass the value “r” as second input of the open method.
We will store in a variable the File object returned by the open method and then perform an error check to make sure the opening procedure did not fail.
File file2 = SPIFFS.open("/file.txt", "r");
if (!file2) {
Serial.println("Failed to open file for reading");
return;
}
Now we will take care of reading the content of the file. To do it, we can leverage the available method of the File object, which returns the number of bytes left to read from the file.
The value returned by this method can be used as stopping condition of a loop where we will read the content of the file byte by byte.
To read a byte from the file, we simply need to call the read method on the File object. We can directly print the returned value to the serial port.
while (file2.available()) {
Serial.write(file2.read());
}
To finalize we just need to close the file with a call to the close method.
file2.close();
The final source code can be seen below.
#include <FS.h>
void setup() {
Serial.begin(115200);
Serial.println();
bool success = SPIFFS.begin();
if (!success) {
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 write failed");
return;
}
file.close();
File file2 = SPIFFS.open("/file.txt", "r");
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() {}
Testing the code
To test the code, simply compile it and upload it to your device, using the Arduino IDE. Once the procedure ends, open the Arduino IDE serial monitor.
You should get an output similar to figure 1. As can be seen, the content we had previously written to the file was printed to the serial port, as expected.
Figure 1 – Output of the program, showing the content of the file.
没有评论:
发表评论