ESP8266 - SPIFFS file system

Zápisník experimentátora

Hierarchy: ESP8266

The ESP8266 microcontroller allows you to store data using the SPIFFS file system. The data is stored in internal Flash memory. Flash memory is divided into several blocks. Each block can save data without affecting other blocks. The microcontroller is most often sold on a 4MB Flash board. The memory is divided into areas.

  • Sketch
  • OTA
  • SPIFFS
  • EEPROM
  • WiFi config

In the Flash size setting, you can select the size which is reserved for the SPIFFS file system.

  • None - since core version 2.5.0.
  • 1 MB
  • 2 MB
  • 3 MB

Each item is different in size. When you first set the size and run the program in the microcontroller, the file system initializes, which takes a few seconds. Each time the setting is changed, it is reinitialized. Files will be retained. It is reasonable to think about how much space you need and adjust it accordingly.

Used parts

This is a simple project, so we will need some board with the ESP8266 microcontroller, which can be directly connected to USB. To avoid a possible short circuit, you can plug the board into a breadboard.

  • ESP8266 {linkESP8266}
  • Breadboard {linkBreadBoard}

SPIFFS setup information

You can try the following program that will give you basic information about your SPIFFS.

#include <FS.h>

void setup() {
  Serial.begin(115200);

  bool res = SPIFFS.begin();
  Serial.print("SPIFFS.begin() = ");
  Serial.println(res);

  if (res) {
    FSInfo fs_info;
    SPIFFS.info(fs_info);

    Serial.print("fs_info.totalBytes = ");
    Serial.println(fs_info.totalBytes);
    Serial.print("fs_info.usedBytes = ");
    Serial.println(fs_info.usedBytes);
    Serial.print("fs_info.blockSize = ");
    Serial.println(fs_info.blockSize);
    Serial.print("fs_info.pageSize = ");
    Serial.println(fs_info.pageSize);
    Serial.print("fs_info.maxOpenFiles = ");
    Serial.println(fs_info.maxOpenFiles);
    Serial.print("fs_info.maxPathLength = ");
    Serial.println(fs_info.maxPathLength);
  }
}

void loop() {
}

The result will look like this.

SPIFFS.begin() = 1
fs_info.totalBytes = 957314
fs_info.usedBytes = 0
fs_info.blockSize = 8192
fs_info.pageSize = 256
fs_info.maxOpenFiles = 5
fs_info.maxPathLength = 32

Basic SPIFFS objects

The file system uses the following objects and classes.

  • SPIFFS - This global object provides basic control of the SPIFFS file system.
  • File - If you want to work with file content, ask SPIFFS to create or open it with the open function. The function has two parameters, a file name and a mode to open it. Modes are compatible with C language function fopen.
  • Dir - If you want to work with directories, ask SPIFFS for directory contents using openDir. SPIFFS does not really know any directories, but if you use the / character in the file name, you can simulate directories. You can use up to 31 characters for the whole file path.

Several basic operations with SPIFFS

I have written an example that shows the use of several basic operations. SPIFFS provides more functionality, but this is the basis for virtually any work with files. Create a file, read the file contents, delete a file, and view the list of files in the directory.

#include <FS.h>

void setup() {
  Serial.begin(115200);

  bool res = SPIFFS.begin();
  Serial.print("SPIFFS.begin() = ");
  Serial.println(res);

  if (res) {
    testCreate();
    printFiles();
    printExample();
    testDelete();
    printFiles();
  }
}

void loop() {
}

void testCreate() {
  Serial.println(__FUNCTION__);
  // create file
  File f = SPIFFS.open("example.txt", "w");
  if (!f)
    Serial.println("File 'example.txt' open failed.");
  else {
    f.println("abc");
    f.println("def");
    f.println("ghi");
    f.close();
  }
}

void printFiles() {
  Serial.println(__FUNCTION__);
  Dir dir = SPIFFS.openDir("");
  while (dir.next()) {
    Serial.print(dir.fileName());
    Serial.print(" - ");
    Serial.println(dir.fileSize());
  }
}

void printExample() {
  Serial.println(__FUNCTION__);
  File f = SPIFFS.open("example.txt", "r");
  if (!f)
    Serial.println("File 'example.txt' open failed.");
  else {
    while (f.available())
      Serial.write(f.read());
    f.close();
  }
}

void testDelete() {
  Serial.println(__FUNCTION__);
  SPIFFS.remove("example.txt");
}

Výstup z programu vyzerá takto.

printFiles
example.txt - 15
printExample
abc
def
ghi
testDelete
printFiles

Upload files to SPIFFS

You can also upload files to SPIFFS from your disk. You need to install the ESP8266FS plug-in. You need to download it from the Internet from the address https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.3.0/ESP8266FS-0.3.0.zip.

  • In the directory with your programs, create the tools directory and unpack the contents of the downloaded ZIP file. The whole file path must look like this <home_dir>/Arduino/tools/ESP8266FS/tool/esp8266fs.jar.
  • Then restart the Arduino IDE.
  • In the directory with your program, create a data directory and upload all files to it.
  • Use the menu command Tools/ESP8266 Sketch Data Upload. Uploading files to ESP8266 will take quite a while.

After uploading the files, you can check the SPIFFS content using the following program. The program also contains a sample data directory with one example.txt file. Note that the file will be saved in SPIFFS as /example.txt.

#include <FS.h>

void setup() {
  Serial.begin(115200);

  bool res = SPIFFS.begin();
  Serial.print("SPIFFS.begin() = ");
  Serial.println(res);

  if (res) {
    printFiles();
    printExample();
  }
}

void loop() {
}

void printFiles() {
  Serial.println(__FUNCTION__);
  Dir dir = SPIFFS.openDir("");
  while (dir.next()) {
    Serial.print(dir.fileName());
    Serial.print(" - ");
    Serial.println(dir.fileSize());
  }
}

void printExample() {
  Serial.println(__FUNCTION__);
  File f = SPIFFS.open("/example.txt", "r");
  if (!f)
    Serial.println("File '/example.txt' open failed.");
  else {
    while (f.available())
      Serial.write(f.read());
    f.close();
  }
}

Source code

The source code is located on the GitHub server.


21.05.2019


Menu