ESP32: integrated SPIFFS FileSystem – Part 2

Spread the love
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

ESP32 integrated SPIFFS FileSystem
ESP32 integrated SPIFFS FileSystem

We continue to explore e32 devices, very powerful microcontroller.

You can find It here AliExpress ESP32 Dev Kit v1 - AliExpress selectable - AliExpress TTGO T-Display 1.14 ESP32 - AliExpress NodeMCU V3 V2 ESP8266 Lolin32

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.
Standard version (dev kit) of esp32 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.

Function Block diagram Espressif esp32 Wi-Fi Bluetooth Microcontroller
Function Block diagram Espressif esp32 Wi-Fi Bluetooth Microcontroller

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.

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\ESP32FS\tool and add the jar file esp32fs.jar there.

Arduino IDE esp32 SPIFFS Sketch Data Upload folder jar
Arduino IDE esp32 SPIFFS Sketch Data Upload folder jar

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

Arduino IDE esp32 SPIFFS Sketch Data Upload
Arduino IDE esp32 SPIFFS Sketch Data Upload

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 ESP32 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”. 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.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.totalBytes()
Return the total bytes enabled on SPIFFS. Returns bytes.

SPIFFS.usedBytes()
Return the total used bytes enabled on SPIFFS. Returns bytes.

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.

/*
 *  ESP32
 *  SPIFFS get info, read dir and show all file uploaded
 *  add a data folder to use with ESP32 Sketch data uploader
 *  by Mischianti Renzo <https://www.mischianti.org>
 *
 *  https://www.mischianti.org
 *
 */

#include "Arduino.h"
#include "SPIFFS.h"

void printDirectory(File dir, int numTabs = 3);

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

	unsigned int totalBytes = SPIFFS.totalBytes();
	unsigned int usedBytes = SPIFFS.usedBytes();

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

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

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

	Serial.println();

	// Open dir folder
	File dir = SPIFFS.open("/");
	// Cycle all the content
	printDirectory(dir);
}

void loop()
{

}

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

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.

/*
 *  ESP32
 *  SPIFFS write, read and seek file
 *  by Mischianti Renzo <https://www.mischianti.org>
 *
 *  https://www.mischianti.org/
 *
 */
#include "Arduino.h"
#include "SPIFFS.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. ESP32: pinout, specs and Arduino IDE configuration
  2. ESP32, integrated SPIFFS Filesystem
  3. […]


Spread the love
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

You may also like...

1 Response

  1. 16 June 2019

    […] To send attachments you must select a storage device, to simplify the management we are going to use SPIFFS integrated filesystem, refer to this article on how to put data on SPIFFS “ESP32: integrated SPIFFS FileSystem“. […]

Leave a Reply

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