WeMos D1 mini (esp8266), integrated SPIFFS Filesystem – Part 2

Spread the love

SPI Flash File System (SPIFFS)

Serial Peripheral Interface Flash File System, or SPIFFS for short. It’s a light-weight file system for microcontrollers with an SPI flash chip. The on-board flash chip of the ESP8266 has plenty of space for your files.
WeMos D1 mini have 4Mb of flash and you can use since 3Mb of that for your file.
SPIFFS let’s you access the flash memory as if it was a normal file system like the one on your computer (but much simpler of course): you can read and write files, create folders etc.

In this flash memory ESP stores the program. Along with program you can store your files on it. Limitation of this memory is it has only 10000 (ten thousand) write cycles.

Sketch OTA update File system EEPROM WiFi config

Important thing is that SPIFFS on esp8266 not support directory, and max file name size is 32.

Add file from IDE to SPIFFS

This operation without an extension for the Arduino IDE is not so simple, but here we are going to explain the simpliest way.

First you must download the plugin for Arduino IDE here the 0.5.0 release is this.

Than you must find your Shetckbook folder, so you must go to File --> Preferences, in that screen you can find at the top of window the Sketchbook location.

Sketchbook Location, additional file Arduino IDE
Preferences window

Now you must create (if not exist) the folder tools\ESP8266FS\tool and add the jar file esp8266fs.jar there.

Path of file esp8266fs.jar

Now restart the IDE and on Tools menu you can find new menu line ESP8266 Sketch Data Upload.

ESP8266 Sketch Data Upload Menu

On Arduino IDE do Ctrl+K to open a filebrowser on directory of the sketch.
Create a directory data where you are going to put the data you want to upload.

Sketch to upload data on SPIFFS on esp8266: directory structure

Set the size of SPIFFS on Tools --> Flash size and set the size of your Microcontroller SPIFFS.

Upload your sketch, and than click on ESP8266 Sketch Data Upload.

Now go to the example sketch to check if all is OK.

Commands

There are some standard command that you can use with this filesystem

SPIFFS.begin()
This method mounts SPIFFS file system and It must be called before any other FS APIs are used. Returns true if file system was mounted successfully, false otherwise.

SPIFFS.format()
Formats the file system. Returns true if formatting was successful.

SPIFFS.open(path, mode)
Opens a file. path should be an absolute path starting with a slash (e.g. /dir/filename.txt). mode is a string specifying access mode. It can be one of “r”, “w”, “a”, “r+”, “w+”, “a+”. Meaning of these modes is the same as for fopen C function.
Returns File object. To check whether the file was opened successfully, use the boolean operator.

SPIFFS.exists(path)
Returns true if a file with given path exists, false otherwise.

SPIFFS.openDir(path)
Opens a directory given its absolute path. Returns a Dir object.

SPIFFS.remove(path): Deletes the file given its absolute path. Returns true if file was deleted successfully.

SPIFFS.rename(pathFrom, pathTo)
Renames file from pathFrom to pathTo. Paths must be absolute. Returns true if file was renamed successfully.

SPIFFS.info(fs_info)
Fills FSInfo structure with information about the file system. Returns true is successful, false otherwise.

file.seek(offset, mode)
This function behaves like fseek C function. Depending on the value of mode, it moves current position in a file as follows:

  • if mode is SeekSet, position is set to offset bytes from the beginning.
  • if mode is SeekCur, current position is moved by offset bytes.
  • if mode is SeekEnd, position is set to offset bytes from the end of the file.
  • Returns true if position was set successfully.

file.position()
Returns the current position inside the file, in bytes.

file.size()
Returns file size, in bytes.

file.name()
Returns file name, as const char*.

file.close()
Close the file.

Pratical examples

Here a sketch to get info and check all file in your SPIFFS.

/*
 *  WeMos D1 mini (esp8266)
 *  SPIFFS get info, read dir and show all file uploaded
 *  add a data folder to use with esp8266 data uploader
 *  by Mischianti Renzo <https://mischianti.org>
 *
 *  https://mischianti.org/wemos-d1-mini-esp8266-integrated-spiffs-filesistem-part-2/
 *
 */

#include "Arduino.h"
#include "FS.h"

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

	delay(500);

	Serial.println(F("Inizializing FS..."));
	if (SPIFFS.begin()){
		Serial.println(F("done."));
	}else{
		Serial.println(F("fail."));
	}

	// To format all space in SPIFFS
	// SPIFFS.format()

	// Get all information of your SPIFFS
	FSInfo fs_info;
	SPIFFS.info(fs_info);

	Serial.println("File sistem info.");

	Serial.print("Total space:      ");
	Serial.print(fs_info.totalBytes);
	Serial.println("byte");

	Serial.print("Total space used: ");
	Serial.print(fs_info.usedBytes);
	Serial.println("byte");

	Serial.print("Block size:       ");
	Serial.print(fs_info.blockSize);
	Serial.println("byte");

	Serial.print("Page size:        ");
	Serial.print(fs_info.totalBytes);
	Serial.println("byte");

	Serial.print("Max open files:   ");
	Serial.println(fs_info.maxOpenFiles);

	Serial.print("Max path length:  ");
	Serial.println(fs_info.maxPathLength);

	Serial.println();

	// Open dir folder
	Dir dir = SPIFFS.openDir("/");
	// Cycle all the content
	while (dir.next()) {
		// get filename
	    Serial.print(dir.fileName());
        Serial.print(" - ");
        // If element have a size display It else write 0
	    if(dir.fileSize()) {
	        File f = dir.openFile("r");
	        Serial.println(f.size());
	        f.close();
	    }else{
	    	Serial.println("0");
	    }
	}
}

void loop()
{

}

Here a sketch with more pratical commands, write a string in a file, read all file content, positioning on the 9 byte of the file and read from there the data.

/*
 *  WeMos D1 mini (esp8266)
 *  SPIFFS write, read and seek file
 *  by Mischianti Renzo <https://mischianti.org>
 *
 *  https://mischianti.org/wemos-d1-mini-esp8266-integrated-spiffs-filesistem-part-2/
 *
 */
#include "Arduino.h"
#include "FS.h"

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

	delay(500);

	Serial.println(F("Inizializing FS..."));
	if (SPIFFS.begin()){
		Serial.println(F("done."));
	}else{
		Serial.println(F("fail."));
	}

	// To remove previous test
	// SPIFFS.remove(F("/testCreate.txt"));

	File testFile = SPIFFS.open(F("/testCreate.txt"), "w");

	if (testFile){
		Serial.println("Write file content!");
		testFile.print("Here the test text!!");

		testFile.close();
	}else{
		Serial.println("Problem on create file!");
	}

	testFile = SPIFFS.open(F("/testCreate.txt"), "r");
	if (testFile){
		Serial.println("Read file content!");
		/**
		 * File derivate from Stream so you can use all Stream method
		 * readBytes, findUntil, parseInt, println etc
		 */
		Serial.println(testFile.readString());
		testFile.close();
	}else{
		Serial.println("Problem on read file!");
	}

	testFile = SPIFFS.open(F("/testCreate.txt"), "r");
	if (testFile){
		/**
		 * mode is SeekSet, position is set to offset bytes from the beginning.
		 * mode is SeekCur, current position is moved by offset bytes.
		 * mode is SeekEnd, position is set to offset bytes from the end of the file.
		 * Returns true if position was set successfully.
		 */
		Serial.println("Position inside the file at 9 byte!");
		testFile.seek(9, SeekSet);

		Serial.println("Read file content!");
		Serial.println(testFile.readString());
		testFile.close();
	}else{
		Serial.println("Problem on read file!");
	}


}

void loop()
{

}

Thanks

  1. WeMos D1 mini (esp8266), specs and IDE configuration
  2. WeMos D1 mini (esp8266), integrated SPIFFS Filesystem
  3. WeMos D1 mini (esp8266), debug on secondary UART
  4. WeMos D1 mini (esp8266), the three type of sleep mode to manage energy savings
  5. WeMos D1 mini (esp8266), integrated LittleFS Filesystem
  6. esp12 esp07 (esp8266): flash, pinout, specs and IDE configuration
  7. Firmware and OTA update management
    1. Firmware management
      1. esp8266: flash firmware binary (.bin) compiled and signed
      2. esp8266: flash firmware and filesystem binary (.bin) compiled with GUI tools
    2. OTA update with Arduino IDE
      1. esp8266 OTA update with Arduino IDE: filesystem, signed and password
    3. OTA update with Web Browser
      1. esp8266 OTA update with Web Browser: firmware, filesystem and authentication
      2. esp8266 OTA update with Web Browser: sign the firmware and HTTPS (SSL/TLS)
      3. esp8266 OTA update with Web Browser: custom web interface
    4. Self OTA uptate from HTTP server
      1. esp8266 self OTA update firmware from server
      2. esp8266 self OTA update firmware from server with version check
      3. esp8266 self OTA update in HTTPS (SSL/TLS) with trusted self signed certificate
    5. Non standard Firmware update
      1. esp8266 firmware and filesystem update from SD card
      2. esp8266 firmware and filesystem update with FTP client
  8. esp32 and esp8266: FAT filesystem on external SPI flash memory
  9. i2c esp8266: how to, network 5v, 3.3v, speed, and custom pins
  10. […]

Spread the love

11 Responses

  1. LekA says:

    Thank you for sharing info to use SPIFFS on Wemos D1 mini. I follow your instruction and once click ESP8266 Sketch Data Upload then get an error ‘SPIFFS Error : esptool not found!. Not sure something missing?

Leave a Reply

Your email address will not be published. Required fields are marked *