File object

There are two functions that return a File object, SPIFFS.open and dir.OpenFile and this File object allows us to read bytes, peek into file, and get the current position in file, get the name of the file or its size. The following are the various functions used by the File object:

  • close: Close the file. No other operations should be performed on the File object after the close() function was called:
file_name.close(); 
  • name: Returns the name of the current file:
String name = my_file.name(); 
  • position: Returns the current position in the file in bytes. You can use it if you are storing same size data and iterare in the file.
  • seek: Allows you to move into a file. It take two parameters; one is the offset in the file and the other is the mode. Returns true for success and false otherwise:
my_file.seek(offset, mode); 
  • Parameter mode can have the same values as it has in the fseek() C function:

If mode is SeekSet, the position is set to offset bytes from the beginning

If mode is SeekCur, the current position is moved by offset bytes

If mode is SeekEnd, the position is set to offset bytes from the end of the file

  • size: Get the current file size in bytes:
Serial.println(my_file.size()); 

As an example let's create a file, write something in it, and read what was written; if the GPIO 4 is put to GND, it will format the flash.

If you remember, FS.h needs to be included to have access to the SPIFFS object:

#include "FS.h" 

Set the interruptPin to value 4, and instantiate the interruptCounter to zero:

const byte interruptPin = 4; 
volatile byte interruptCounter = 0; 

This function will be called if the GPIO is put to GND. In it it will increment the interruptCounter and will be verifying its value in the loop() function. Try not to allocate memory at interrupt level; this is a recipe for disaster:

void handleInterrupt() { 
  interruptCounter++; 
} 

In setup(), we will set the GPIO 4 as input pin and use the attacheInterrupt function for FALLING ( VCC to GND) to trigger the handleInterrupt function. Next we open the my_file.txt and write a message in it. After the file is closed, it will be opened again, but now it is in read mode and it will read its content:

 void setup() { 
  Serial.begin(115200); delay(10); 
  //GPIO 4 format SPIFFS 
  pinMode(interruptPin, INPUT_PULLUP); 
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING); 
 
  if(SPIFFS.begin()) 
  { 
    Serial.println(F("File system was mounted.")); 
    // open file for writing 
    File my_file = SPIFFS.open("/my_file.txt", "w+"); 
    if (!my_file) { 
      Serial.println("file open failed"); 
    } 
    Serial.println(F("Writing to SPIFFS ")); 
    //print something to my_file.txt 
    my_file.println("SPIFFS is cool!"); 
    //close now the file 
    my_file.close(); 
 
    // open file for reading. Now I can use other File object     
    File f = SPIFFS.open("/my_file.txt", "r"); 
    if (!f)  
    { 
       Serial.println(F("Failed to open my_file.txt")); 
    }  
    //now read the file content       
    String s=f.readStringUntil('
');       
    Serial.println(s); 
    //closing the file now 
    f.close();   
  } 
  else 
  { 
    Serial.println(F("Failed to mount SPIFFS. Restart")); 
    ESP.restart(); 
  } 
} 

In the loop file we check for the intrruptCounter value and if it is greater than zero it will format the filesystem and restart the ESP:

void loop()  
{ 
  if(interruptCounter>0) 
  { 
    interruptCounter--; 
    Serial.println(F("Formating the file system... Please wait!")); 
    SPIFFS.format();   
    Serial.println(F("Done formating the file system."));   
    ESP.restart();   
  }   
} 

The Serial output will show what was read from the file, also the formating messages and the new restart after that, as seen in the following screenshot:

Output for write, read, and format

Now that you have managed to understand the SPIFFS I am sure that you will use it in all your projects since it is a very handy way to keep configuration data or to store all kinds of information such as readings from sensors, especially if you are running on batteries. You can store all the readings in a file in JSON format and once a day connect to Wi-Fi, read the file, and send all the data at once.

After this long but useful introduction, let's go back to our project, a thermostat using the ESP8266. To complete this, besides the ESP8266 we need:

  • Temperature sensor
  • Relay board
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.148.105