Arduino ESP8266 SPIFFS学习 FS.h 闪存文件系统(SPIFFS)
闪存用于存放上传的程序网页文件或者系统配置文件
为Arduino IDE安装插件:
- 下载文件系统上传工具 工具地址
- 导入工具参考目录 ******/tools/ESP8266FS/tool/esp8266fs.jar
- 重启arduino
1.如何选择我的SPIFFS大小
- 在你的IDE菜单中(工具 > Falsh Size > 4M(1M SPIFFS)),视情况选择大小。(Falsh)一般ESP01为1M大小,ESP12为4M大小。
2.是否有文件限制
- 传,都可以传(只要有空间),点击工具菜单中ESP8266 SKetch Data Upload来上传data中的文件。
3.FS库的文档
使用SPIFFS
ESP8266 Skech Data Upload Arduino IDE就会将这个目录的文件写入到SPIFFS中了。要注意的是文件的大小不能超过板子SPIFFS的大小,否则会上传失败。
index.html读出来:
#include"FS.h" void setup() { Serial.begin(115200); bool ok = SPIFFS.begin(); if (ok) { Serial.println("ok"); //检查文件是否存在 bool exist = SPIFFS.exists("/index.html"); if (exist) { Serial.println("The file exists!"); File f = SPIFFS.open("/index.html", "r"); if (!f) { // 在打开过程中出现问题f就会为空 Serial.println("Some thing went wrong trying to open the file..."); } else { int s = f.size(); Serial.printf("Size=%d ", s); //读取index.html的文本内容 String data = f.readString(); Serial.println(data); //关闭文件 f.close(); } } else { Serial.println("No such file found."); } } } void loop() { // put your main code here, to run repeatedly: }
FS的参考
SPIFFS对象
begin
SPIFFS.begin()
该方法用于挂载SPIFFS文件系统,必须在使用SPIFFS之前就调用,一般都会在false
。
format
SPIFFS.format()
格式化文件系统。返回true
表示格式化成功。
open
SPIFFS.open(path, mode)
File对象。
-
/test.text
) -
File对象,否则就会返回空。
File f = SPIFFS.open("/f.txt", "w"); if (!f) { Serial.println("file open failed"); }
exists
SPIFFS.exists(path)
检测指定文件或目录是否存在。
openDir
SPIFFS.openDir(path)
打开指定目录并返回一个目录对象实例。
remove
SPIFFS.remove(path)
删除指定绝对路径上的文件或目录。
rename
SPIFFS.rename(pathFrom, pathTo)
重命名。
info
FSInfo fs_info; SPIFFS.info(fs_info);
获取一个文件系统信息结构。
文件系统信息结构
struct FSInfo { size_t totalBytes; // 可用量 size_t usedBytes; // 已用 size_t blockSize; // 块大小 size_t pageSize; // 页大小 size_t maxOpenFiles; // 最大打开文件数 size_t maxPathLength; // 最大文件路径长度 };
目录 (Dir)
openFile(mode)
以下例子用于枚举指定目录下的子目录、文件名和文件大小:
Dir dir = SPIFFS.openDir("/data"); while (dir.next()) { Serial.print(dir.fileName()); File f = dir.openFile("r"); Serial.println(f.size()); }
openFile函数。
文件对象
println。
seek
file.seek(offset, mode)
移动文件指针。
position
file.position()
返回当前文件指针的位置 。
size
file.size()
返回文件的大小。
name
String name = file.name();
返回文件名。
close
file.close()
关闭并释放文件对象。
在实际的运用场景中,合理地使用SPIFFS会给我们省下很多的时间甚至是生产成本,希望这篇短文能给你在使用ESP8266的过程中给予一些帮助。
链接:https://www.jianshu.com/p/014bcae94c8b
链接:https://blog.****.net/weixin_44529350/article/details/107977175
链接:https://blog.****.net/Ay_yzx/article/details/107523404?utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242
链接:https://www.cnblogs.com/Lonelychampion/p/12230368.html