2017/06/22

ESP8266 SPIFFS

This is a summary of my experience testing out the SPIFFS feature for ESP8266.

Below is the source of the Arduino sketch used.
https://github.com/G6EJD/SPIFFS-Examples/blob/master/ESP8266_SPIFFS_Example.ino

The Test Sketch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Example of using the Serial Peripheral Interface Flash Filing System (SPIFFS) or SPI bus Flash memory Filing System

#include <FS.h> //spiff file system

void setup() {
  Serial.begin(115200);
  Serial.println("Starting...");
  SPIFFS.begin();
}

void loop() {
  // Assign a file name e.g. 'names.dat' or 'data.txt' or 'data.dat' try to use the 8.3 file naming convention format could be 'data.d'
  char filename [] = "datalog.txt";                     // Assign a filename or use the format e.g. SD.open("datalog.txt",...);

  if (SPIFFS.exists(filename)) SPIFFS.remove(filename); // First in this example check to see if a file already exists, if so delete it

  File myDataFile = SPIFFS.open(filename, "a+");        // Open a file for reading and writing (appending)
  if (!myDataFile)Serial.println("file open failed");   // Check for errors
  
  myDataFile.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");     // Write some data to it (26-characters)
  myDataFile.println(3.141592654);
  Serial.println(myDataFile.size());                    // Display the file size (26 characters + 4-byte floating point number + 6 termination bytes (2/line) = 34 bytes)
  myDataFile.close();                                   // Close the file

  myDataFile = SPIFFS.open(filename, "r");              // Open the file again, this time for reading
  if (!myDataFile) Serial.println("file open failed");  // Check for errors
  while (myDataFile.available()) {
    Serial.write(myDataFile.read());                    // Read all the data from the file and display it
  }
  myDataFile.close();                                   // Close the file
  delay(10000);                                         // wait and then do it all again
}

Note,

Be sure not to let the above test sketch run for too long because flash has limited write time. In the case of Winbond W25Q32, though it has more than 100,000 erase / program cycles, at the rate of 10 seconds (delay(10000)) per cycle, it will take less than 12 days to exceed the 100,000 limit. 

Below is an excerpt from the W25Q32BV datasheet which can be found at:
http://www.winbond.com/resource-files/w25q32bv_revi_100413_wo_automotive.pdf


References:

Using ESP8266 SPIFFS
http://www.instructables.com/id/Using-ESP8266-SPIFFS/

#121 SPIFFS and JSON to save configurations on an ESP8266
https://youtu.be/jIOTzaeh7fs

JSON Editor Online
http://www.jsoneditoronline.org/

Tech Note 030 ESP8266 Using SPIFFS to replace SD Card storage
https://youtu.be/_UBPgdp1Yug

In Chinese

Arduino读写SPI Flash存储芯片 -- W25Q80/32/64/128 (W25Q32为例)
http://www.yfrobot.com/thread-11949-1-1.html

--------------------------------------------------------------------------------------------------------------------------

DOING SPIFFS FORMAT ONLY ONCE AUTOMATICALLY
https://www.esp8266.com/viewtopic.php?f=8&t=15020


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
SPIFFS.begin();

  if (!SPIFFS.exists("/formatComplete.txt")) {
    Serial.println("Please wait 30 secs for SPIFFS to be formatted");
    SPIFFS.format();
    Serial.println("Spiffs formatted");
    
    File f = SPIFFS.open("/formatComplete.txt", "w");
    if (!f) {
        Serial.println("file open failed");
    } else {
        f.println("Format Complete");
    }
  } else {
    Serial.println("SPIFFS is formatted. Moving along...");
  }

No comments:

Post a Comment