Begining Guide to SPIFFS-Flash File System with NodeMCU

SPIFFS-Flash File System NodeMCU

Today in this tutorial I will show you Begining Guide to SPIFFS-Flash File System with NodeMCU step by step complete process.

NodeMCU Flash File System Named as (SPIFFS). There are two methods to store static data on NodeMCU one using internal EEPROM which 512 Bytes. but you can write data 1 million times. and Second, the use of SPI Flash (64kBytes to 3Mbyte). if you see ESP-01 a little 8-Pin Chip now near to the ESP8266 which  FLASH memory joined to ESP by SPI. In this flash memory, ESP stores the program, files, Wi-Fi, and configurations. The condition of this memory it has only 10000 (ten thousand) write cycles.

Also though the file system saved on the same flash chip as the program. now time to programming new sketch will not modify file system contents. This allows us to use a file system to store sketch data, configuration files, or content for Web servers.


≡ Required Component :


Here is a list of the hardware we remanent  Begining Guide to SPIFFS-Flash File System with NodeMCU

  1. NodeMcu8266
  2. Breadboard
  3. Connecting wire
  4. Breadboard Power Supply Module

≡ Working method SPIFFS on NodeMCU


begin

SPIFFS.begin()

This method attaches the SPIFFS file system. It needs to call before any other FS APIs utilized. Replies true if the file system attached successfully, false otherwise.

format

SPIFFS.format()

Formats the file system. May called each before or after calling begin. Returns true if the formatting was successful.

open

SPIFFS.open(path, mode)

Starts a file. the path should be an ideal path starting with a slash (e.g. /dir/filename.txt). the method of a string defining access method. It can be example of “r”, “w”, “a”, “r+”, “w+”, “a+”. The meaning of these methods the same as for open C function. Returns File object. To check whether the file opened successfully, utilize the boolean operator.

File f = SPIFFS.open("/samplefile.txt", "w");
if (!f) {
   Serial.println("file open failed");
}

exists

SPIFFS.exists(path)

Returns valid if a file with a given path exists, false otherwise.

openDir

SPIFFS.openDir(path)

Opens a list given its complete path. Returns a Dir object.

remove

SPIFFS.remove(path)

Removes the file given its entire path. Returns true if the file was removed successfully.

rename

SPIFFS.rename(pathFrom, pathTo)

Renames file from the path from to pathTo. Paths must be fixed. Replies true if the file was renamed successfully.

position

file.position()

Passes the current situation inside the file, in bytes.

size

file.size()

Returns file size, in bytes.

name

String name = file.name();

The returns filename, as const char*. Convert it to String for storage.

close

file.close()

Connect the file. No other operations should be performed on File object after the close function was called.


≡ SPIFFS Example


#include <ESP8266WiFi.h>
#include <FS.h>   //Include File System Headers
const char* filename = "/samplefile.txt";
void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  //Initialize File System
  if(SPIFFS.begin())
  {
    Serial.println("SPIFFS Initialize....ok");
  }
  else
  {
    Serial.println("SPIFFS Initialization...failed");

  }
  //Format File System
  if(SPIFFS.format())
  {
    Serial.println("File System Formatted");

  }

  else

  {
    Serial.println("File System Formatting Error");
  }
  //Create New File And Write Data to It
  //w=Write Open file for writing
  File f = SPIFFS.open(filename, "w");
  if (!f) {
    Serial.println("file open failed");
  }
  else
  {     //Write data to file

      Serial.println("Writing Data to File");
      f.print("This is sample data which is written in file");
      f.close();  //Close file
  }
}
void loop() {
  int i;
  //Read File data
  File f = SPIFFS.open(filename, "r");
  if (!f) {
    Serial.println("file open failed");
  }
  else
  {
     Serial.println("Reading Data from File:");
      //Data from file
      for(i=0;i<f.size();i++) //Read upto complete file size
      {
        Serial.print((char)f.read());
      }
      f.close();  //Close file
     Serial.println("File Closed");
  }
  delay(5000);
}

File contents in  the serial monitor

SPIFFS-Flash File System NodeMCU
SPIFFS Example

≡ Directly uploading files to NodeMCU


ESP8266FS is a tool that integrates into the Arduino IDE. It joins a menu item to the Tools menu for uploading the contents of the sketch data directory into the ESP8266 flash file system.

1.Download the tool:

No Title

No Description

2. Unpack and copy the ESP8266FS folder into the tools directory(the path will look similar to path C:\Program Files (x86)\Arduino\tools\ESP8266FS\).

SPIFFS-Flash File System NodeMCU
Unpack and copy the ESP8266FS folder

3. Restart Arduino IDE

4. Open a sketch (or create a new one and save it)

5. Go to sketch directory (choose Sketch > Show Sketch Folder)

6. Build a directory called data and put your files (ex. notes.txt) you want in the file system there

SPIFFS-Flash File System NodeMCU
sketch data folder location

7. Make sure you have selected a board, port, and closed Serial Monitor

8. Select Tools > ESP8266 Sketch Data Upload. This should start uploading the files into the ESP8266 flash file system. When done, the IDE status bar will display the SPIFFS Image Uploaded message. Note during upload it takes a longer time.

SPIFFS-Flash File System NodeMCU
Sketch Data Upload Option

Upload sketch in NodeMCU. Uploading of the sketch method will not delete/affect Sketch Data our uploaded file.

#include<ESP8266WiFi.h>
#include<FS.h>
//enter your file name
const char* file="/notes.txt";
void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println();
//Initialize File system
SPIFFS.begin();
Serial.println("File System Initialized");
//open file for reading
File dataFile=SPIFFS.open(file,"r"); 
Serial.println("Reading Data from File:");
//Data from file
//Read upto complete size
for(int i=0;i<dataFile.size();i++)
{
Serial.print((char)dataFile.read());
}
dataFile.close();
}
void loop()
{
}

Open your serial monitor and press the reset button on NoseMCU. The serial monitor will show contents of the File.

SPIFFS-Flash File System NodeMCU
Reading contents of the directly uploaded file

Visit to get more tutorials on ESP8266

 291 total views,  5 views today