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.
The following diagram illustrates flash layout used in Arduino environment:
|--------------|-------|---------------|--|--|--|--|--|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:
Board | Flash chip size, bytes | File system size, bytes |
Generic module | 512k | 64k, 128k |
Generic module | 1M | 64k, 128k, 256k, 512k |
Generic module | 2M | 1M |
Generic module | 4M | 3M |
Adafruit HUZZAH | 4M | 1M, 3M |
ESPresso Lite 1.0 | 4M | 1M, 3M |
ESPresso Lite 2.0 | 4M | 1M, 3M |
NodeMCU 0.9 | 4M | 1M, 3M |
NodeMCU 1.0 | 4M | 1M, 3M |
Olimex MOD-WIFI-ESP8266(-DEV) | 2M | 1M |
SparkFun Thing | 512k | 64k |
SweetPea ESP-210 | 4M | 1M, 3M |
WeMos D1 & D1 mini | 4M | 1M, 3M |
ESPDuino | 4M | 1M, 3M |
Note: to use any of file system functions in the sketch, add the following include to the sketch:
#include "FS.h"ESP8266FS is a tool which integrates into the Arduino IDE. It adds a menu item toTools menu for uploading the contents of sketch data directory into ESP8266 flash file system.
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.
formatFormats the file system. May be called either before or after calling begin. Returnstrue if formatting was successful.
openOpens 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.
File f = SPIFFS.open("/f.txt", "w");
if (!f) {
Serial.println("file open failed");
}
Returns true if a file with given path exists, false otherwise.
openDirOpens a directory given its absolute path. Returns a Dir object.
removeDeletes the file given its absolute path. Returns true if file was deleted successfully.
renameRenames file from pathFrom to pathTo. Paths must be absolute. Returns true if file was renamed successfully.
infoFills FSInfo structure with information about the file system. Returns true is successful, false otherwise.
Filesystem information structurestruct FSInfo {
size_t totalBytes;
size_t usedBytes;
size_t blockSize;
size_t pageSize;
size_t maxOpenFiles;
size_t maxPathLength;
};
This is the structure which may be filled using FS::info method. - totalBytes — total size of useful data on the file system - usedBytes — number of bytes used by files - blockSize — SPIFFS block size - pageSize — SPIFFS logical page size -maxOpenFiles — max number of files which may be open simultaneously -maxPathLength — max file name length (including one byte for zero termination)
Directory object (Dir)The purpose of Dir object is to iterate over files inside a directory. It provides three methods: next(), fileName(), and openFile(mode).
The following example shows how it should be used:
Dir dir = SPIFFS.openDir("/data");
while (dir.next()) {
Serial.print(dir.fileName());
File f = dir.openFile("r");
Serial.println(f.size());
}
dir.next() returns true while there are files in the directory to iterate over. It must be called before calling fileName and openFile functions.
openFile method takes mode argument which has the same meaning as forSPIFFS.open function.
File objectSPIFFS.open and dir.openFile functions return a File object. This object supports all the functions of Stream, so you can use readBytes, findUntil,parseInt, println, and all other Stream methods.
There are also some functions which are specific to File object.
seekThis function behaves like fseek C function. Depending on the value of mode, it moves current position in a file as follows:
Returns true if position was set successfully.
positionReturns the current position inside the file, in bytes.
sizeReturns file size, in bytes.
nameReturns file name, as const char*. Convert it to String for storage.
closeClose the file. No other operations should be performed on File object after closefunction was called.
欢迎光临 YFROBOT创客社区 (http://www.yfrobot.com.cn/) | Powered by Discuz! X3.1 |