Discovering the filesystem of the ESP8266

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.

Flash memory

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:

Flash memory

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

Filesystem memory management

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.

Setting up the ESP8266FS tool

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:

  1. Download the ESP8266FS tool from this link: https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.2.0/ESP8266FS-0.2.0.zip.
  2. Create a Tools directory in your Arduino sketchbook directory.
  3. Unzip the ESP8266FS tool into the Tools directory. The path will look like this: My Documents/Arduino/tools/ESP8266FS/tool/esp8266fs.jar.
  4. Restart the Arduino IDE.
  5. Create a new sketch or open one that already exists.
  6. Click on the Sketch | Show sketch folder.
  7. Create a directory called data.
  8. Ensure that all the files you are intending to save in the filesystem are in the data directory.
  9. Select the board and port and close the serial monitor if it is open.
  10. Click on Tools | ESP8266 Sketch Data Upload.
  11. The IDE will start uploading the files in the 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 filesystem
    • format(): Formats the filesystem
    • end(): Unmounts the SPIFFS filesystem
    • open(path, mode): Opens a file
    • exists(path): Returns true if there is a file with the provided path
    • openDir(path): Opens a directory whose path is provided
    • rename(pathfrom, pathTo): Renames files
    • remove(path): Deletes the provided path
  • Directory object:
    • next(): Returns true if there are files to scroll over
    • fileName(): Returns the filename of the file that is being pointed to
    • openfile(mode): Opens the file
  • File object:
    • seek(offset, mode): Returns true if the position was set successfully
    • position(): Returns current position in the file in bytes
    • size(): Returns the size of the file in bytes
    • name(): Returns the filename
    • close(): Closes the file

Using 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.

There's more…

Read more on the SPIFFS library and functions at this link: https://github.com/esp8266/Arduino/blob/master/doc/filesystem.md.

See also

Now that you have understood the ESP8266 filesystem, you should apply it in your sketches. Proceed to the next recipe and learn how to use the filesystem to store data in your projects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.190.167