ad space

Write and Read from NodeMCU using SPIFFS

NodeMCU interfacing with DHT11 sensor

In this tutorial we will show how to write to and read from NodeMCU using SPIFFS(Serial Peripheral Interface Flash File System). We will write temperature data from DHT11 sensor into the flash memory file system using SPIFFS. Then we will read the same stored data in flash memory file system.

The NodeMCU with ESP8266 chip or board has flash memory chip in which data such as sensor data, data such as html, css, javascript files to build web server, configuration files, sketch, firmware etc can be stored. The flash memory architecture divides the flash memory into various sections such as sketch, OTA, File System, EEPROM, WiFi configuration. The file system is thus one section of the flash memory where we can store data such as sensor data log, HTML, CSS, Javascript etc. The size of the file system depends upon the flash memory size which in turns depends upon different ESP8266 boards and ESP32 boards.

SPIFFS(Serial Peripheral Interface Flash File System) is a module that allows one to store and access data in the file system area of the flash chip in file format. SPIFFS also partitions the available file system memory into pages which are small enough(default size is 256 bytes) to be cached in RAM. SPIFFS also optimizes number of erase. 

In order to use SPIFFS with NodeMCU(ESP8266 board) with Arduino IDE we need to install a tool called ESP8266FS. That is if this tool is installed on Arduino IDE, an upload tool called "ESP8266 Sketch Data Upload" appears in the tool dropdown in Arduino IDE. Clicking on that option we can upload file in the data folder in the Arduino sketch folder.

How to setup the ESP8266FS plugin tool in Arduino IDE

The following is the steps to install the Arduino ESP8266FS plugin.

1. Download the ESP8266FS-0.5.0.zip from the following link

https://github.com/esp8266/arduino-esp8266fs-plugin/releases/tag/0.5.0

2. In your Arduino Sketchbook location create a new folder called "tools". You can find the sketchbook location from File> Preference as shown below.

3. After you have created the tools folder place the downloaded zip file in step 1 into that tools folder. Unzip the zipped file in the tools folder. If correctly unzipped then there should be eso8266fs jar file in the following location in windows OS.

C:\Users\Dell\Documents\Arduino\tools\ESP8266FS\tool

This is as shown below.

 4. Close and reopen Arduino IDE. In the tool menu you should see ESP8266 Sketch Data Upload tool as shown below.

If you see this then the tool is successfully installed. 

See the following video that shows how to install ESP8266FS plugin/tool on Arduino IDE.


Writing to NodeMCU Flash Memory with SPIFFS

In this tutorial we will read DHT11 temperature data with ESP8266 and write this temperature log data into a log.txt file. This log.txt file is uploaded into the flash memory of ESP8266. 

To do this we create a log.txt file in a folder called data. First we create a folder called data in the same folder as the sketch folder. Here the sketch is called ESP8266_SPIFFS_WRITE.ino. The following shows the data folder along with the sketch.

Inside the data folder we create a text file called log.txt as shown below.

After creating the log file we upload it to the file system using SPIFFS using the tool installed above. Go and click on Tools > ESP8266 Sketch Data Upload.


 The next is to write the sketch which reads the sensor data and writes it to the log file. This data will be then uploaded into the log file in the file system in flash memory. Before writing the sketch the hardware connection is done first.

The DHT11 sensor is connected the ESP8266 GPIO pin 5(digital pin D1). This is same circuit diagram as in the tutorial How to transfer data from NodeMCU via WiFi?.  The schematic diagram and the breadboard connection is shown below.

NodeMCU interfacing with DHT11 sensor

nodemcu interfacing with DHT11

Program Code

The following is Arduino sketch is uploaded into the NodeMCU board. It reads the temperature data from DHT sensor and open/write the data into the log.txt.

ESP8266_SPIFFS_WRITE.ino


#include "FS.h"
#include "DHT.h"

#define DHTPIN 5 // the ESP8266 pin where DHT sensor pin is connected to
#define DHTTYPE DHT11 // the DHT sensor type used

DHT dht(DHTPIN, DHTTYPE); // create DHT object

void setup() {
  Serial.begin(115200);
  dht.begin(); // intialize DHT object
  
  // mount and check the filesystem
  bool ok = SPIFFS.begin();
  if (ok) {
    Serial.println("SPIFFS initialized successfully");
    }
  else{
    Serial.println("SPIFFS intialization error");
    }
}
void loop() {
  // Wait a few seconds between measurements.
  delay(2000);
  //read temperature as Celcius
  float T = dht.readTemperature();

  // Check if DHT reading fails to exit early
  if (isnan(T)) {
  Serial.println("Failed to read from DHT sensor!");
  return;
  }
  
  //open log.txt file
  File file = SPIFFS.open("/log.txt", "a");
  if (!file) {
  Serial.println("file open failed");
  }
  
  // save temperature reading
  file.print(T);
  file.println("deg C");
  Serial.println(T);
  
  //close file
  file.close();
  delay(8000);
}

This sketch should be then uploaded in the NodeMCU/ESP8266 board.

Reading from NodeMCU Flash Memory with SPIFFS

To verify that the temperature data was successfully written into the Flash Memory in the log.txt file we need to read it from the NodeMCU flash memory. The following sketch will read earlier saved temperature data in log.txt in the flash memory of NodeMCU.

ESP8266_SPIFFS_READ.ino 


#include "FS.h"


void setup() {
  Serial.begin(115200);
  
  // mount and check the filesystem
  bool ok = SPIFFS.begin();
  if (ok) {
    Serial.println("SPIFFS initialized successfully");
    }
  else{
    Serial.println("SPIFFS intialization error");
    }

  //
   File file = SPIFFS.open("/log.txt", "r");
  if(!file){
    Serial.println("Failed to open file for reading");
    return;
  }
  
  Serial.println();
  Serial.println("File Content:");
  while(file.available()){
    Serial.write(file.read());
  }
  file.close();
}
void loop() {

  }

This sketch then be uploaded into the NodeMCU. Once uploaded the serial monitor should be opened and we can see the file content(the content in log.txt in flash memory).


Post a Comment

Previous Post Next Post