One of the advantages that the ESP8266 has over most other IoT boards is the ability to store files in its flash memory. You can store configuration files, sketch data, and even content for a web server in the flash memory. In this recipe, we will learn about the architecture of the flash memory and the filesystem as a whole. This will enable us to know how to properly store and retrieve data in and from it.
In most microcontroller architectures, the flash memory is used as the main storage media. Configuration files, firmware, and application data are all stored in it. The flash memory allows random writes and reads and it can only be erased in blocks at a time. Moreover, it can only be erased a specific number of times (between 100,000 and 1,000,000 times) before it fails.
In the Arduino environment, the flash memory is laid out in a specific way, as shown in the following screenshot. This is to ensure proper memory use and maintenance:
This memory architecture is used in the ESP8266 and the memory allocation we are going to be concerned with is the one set aside for the filesystem. The size of the filesystem usually varies, depending on the size of the flash memory. Different ESP8266 boards will not have the same flash memory size. As such, the Arduino IDE sets the available filesystem size depending on the board you select. The filesystem sizes are as follows:
Board |
Flash chip size, bytes |
Filesystem size, bytes |
---|---|---|
Generic module |
512 k |
64 k, 128 k |
Generic module |
1 M |
64 k, 128 k, 256 k, 512 k |
Generic module |
2 M |
1 M |
Generic module |
4 M |
3M |
Adafruit HUZZAH |
4 M |
1M, 3 M |
ESPresso Lite 1.0 |
4 M |
1 M, 3 M |
ESPresso Lite 2.0 |
4 M |
1 M, 3 M |
NodeMCU 0.9 |
4 M |
1 M, 3 M |
NodeMCU 1.0 |
4 M |
1 M, 3 M |
Olimex MOD-WIFI-ESP8266(-DEV) |
2 M |
1 M |
SparkFun Thing |
512 k |
64 k |
SweetPea ESP-210 |
4 M |
1 M, 3 M |
WeMos D1 & D1 mini |
4 M |
1 M, 3 M |
ESPDuino |
4 M |
1 M, 3 M |
Working with flash memory is a difficult task, unless you are using SPIFFS. It is a module that enables access to and storage of data in the flash memory in file format.
SPIFFS partitions the available memory into pages to create a filesystem. The pages are usually small enough to be cached in the RAM. The default size is 256 bytes. The pages contain file contents and index information.
SPIFFS optimizes the writes to conserve flash memory and reduce the number of erases performed. When small writes are required, they are first written in a buffer in the RAM. When the buffer fills up, the data is written to the flash memory and a new page is started. In case there is data in the middle of a page that needs to be changed, the changes are copied to a new page and the former page is deleted.
The ESP8266FS
is a tool that enables the Arduino IDE to upload files onto the ESP8266 flash filesystem. When the tool is properly installed, a menu item appears in the Tools menu of the Arduino IDE. You can use that menu item to do the uploading.
To successfully set up the ESP8266FS
tool, follow these steps:
ESP8266FS
tool from this link: https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.2.0/ESP8266FS-0.2.0.zip.Tools
directory in your Arduino sketchbook
directory.ESP8266FS
tool into the Tools
directory. The path will look like this: My Documents/Arduino/tools/ESP8266FS/tool/esp8266fs.jar
.Sketch
| Show sketch
folder.data
.data
directory.data
directory to the ESP8266 flash filesystem.Once the upload is complete, the status bar on the IDE will display SPIFFS image Uploaded.
The library that supports SPIFFS is called FS
. Therefore, if you want to store some files in the ESP8266 flash filesystem, add the #include <FS.h>
pre-processor directive in your sketch to include the FS
library.
Some of the common functions in the FS
library you will be using include:
Filesystem
object:begin()
: Mounts the SPIFFS filesystemformat()
: Formats the filesystemend()
: Unmounts the SPIFFS filesystemopen(path, mode)
: Opens a fileexists(path)
: Returns true if there is a file with the provided pathopenDir(path)
: Opens a directory whose path is providedrename(pathfrom, pathTo)
: Renames filesremove(path)
: Deletes the provided pathDirectory
object:next()
: Returns true if there are files to scroll overfileName()
: Returns the filename of the file that is being pointed toopenfile(mode)
: Opens the fileFile
object:seek(offset, mode)
: Returns true if the position was set successfullyposition()
: Returns current position in the file in bytessize()
: Returns the size of the file in bytesname()
: Returns the filenameclose()
: Closes the fileUsing these functions, you will be able to realize the full potential of the ESP8266 filesystem. It comes in handy, especially in web server applications for storage of web pages and other web-related resources. You can also use it to save sensor data or important configurations.
Read more on the SPIFFS library and functions at this link: https://github.com/esp8266/Arduino/blob/master/doc/filesystem.md.
18.216.190.167