Esp 8266
Esp 8266
(SPIFFS)
January 31, 2018 ESP8266 esp, ESP8266, File System, Flash, NodeMCU, SPIFFS
This tutorial explains in depth ESP8266 Flash File System Called as (SPIFFS). There are two ways to
store data on ESP8266 one is using internal EEPROM which is of 512 Bytes but you can write data 1
millions of times (no file system). and Second is use of SPI Flash (64kBytes to 3Mbyte), when you see
ESP-01 a small 8-Pin Chip is present near to the ESP8266 which is FLASH memory connected to ESP
through SPI. 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.
Even though file system is stored on the same flash chip as the program, programming new sketch
will not modify file system contents. This allows to use file system to store sketch data, configuration
files, or content for Web server.
1 |--------------|-------|---------------|--|--|--|--|--|
2 ^ ^ ^ ^ ^
3 Sketch OTA update File system EEPROM WiFi config (SDK)
4
File system size depends on the flash chip size. Depending on the board which is selected in IDE, you
have the following options for flash size:
Generic module 2M 1M
Generic module 4M 3M
26
27 //Format File System
28 if(SPIFFS.format())
29 {
30 Serial.println("File System Formated");
31 }
32 else
33 {
34 Serial.println("File System Formatting Error");
35 }
36
37 //Create New File And Write Data to It
38 //w=Write Open file for writing
39 File f = SPIFFS.open(filename, "w");
40
41 if (!f) {
42 Serial.println("file open failed");
43 }
44 else
45 {
46 //Write data to file
47 Serial.println("Writing Data to File");
48 f.print("This is sample data which is written in file");
49 f.close(); //Close file
50 }
51
52 }
53
54 void loop() {
55 int i;
56
57 //Read File data
58 File f = SPIFFS.open(filename, "r");
59
60 if (!f) {
61 Serial.println("file open failed");
62 }
63 else
64 {
65 Serial i tl ("R di D t f Fil ")
65 Serial.println("Reading Data from File:");
66 //Data from file
67 for(i=0;i<f.size();i++) //Read upto complete file size
68 {
69 Serial.print((char)f.read());
70 }
71 f.close(); //Close file
72 Serial println("File Closed");
Results
Open serial monitor and see file data writing and reading.
This method mounts SPIFFS file system. It must be called before any other FS APIs are used. Returns
true if file system was mounted successfully, false otherwise.
format
SPIFFS.format()
Formats the file system. May be called either before or a er calling begin. Returns true if formatting
was successful.
open
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”, “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.
exists
SPIFFS.exists(path)
openDir
SPIFFS.openDir(path)
remove
SPIFFS.remove(path)
Deletes the file given its absolute path. Returns true if file was deleted successfully.
rename
SPIFFS.rename(pathFrom, pathTo)
Renames file from pathFrom to pathTo. Paths must be absolute. Returns true if file was renamed
successfully.
info
1 FSInfo fs_info;
2 SPIFFS.info(fs_info);
Fills FSInfo structure with information about the file system. Returns true is successful, false
otherwise.
seek
file.seek(o set, mode)
This function behaves like fseek C function. Depending on the value of mode, it moves current
position in a file as follows:
position
file.position()
size
file.size()
name
String name = file.name();
close
file.close()
Close the file. No other operations should be performed on File object a er close function was called.
This way you can perform File System operations in ESP8266 and NodeMCU.