arduino開發ESP8266學習筆十-----ESP8266閃存文件系統


  通常ESP8266的閃存文件系統大小是4Mb,其中有1Mb的空間是程序存儲大小,剩下的3Mb是文件存儲,但是其中有一部分是存儲系統文件的,所以用戶可用的文件存儲空間是小於3Mb的。

更多信息詳見https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html

所使用的FS.h

 /*FS.h - file system wrapper Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. This file is part of the esp8266 core for Arduino environment. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef FS_H #define FS_H #include <memory> #include <Arduino.h>

namespace fs { class File; class Dir; class FileImpl; typedef std::shared_ptr<FileImpl> FileImplPtr; class FSImpl; typedef std::shared_ptr<FSImpl> FSImplPtr; class DirImpl; typedef std::shared_ptr<DirImpl> DirImplPtr; template <typename Tfs>
bool mount(Tfs& fs, const char* mountPoint); enum SeekMode { SeekSet = 0, SeekCur = 1, SeekEnd = 2 }; class File : public Stream { public: File(FileImplPtr p = FileImplPtr()) : _p(p) {} // Print methods:
    size_t write(uint8_t) override; size_t write(const uint8_t *buf, size_t size) override; // Stream methods:
    int available() override; int read() override; int peek() override; void flush() override; size_t readBytes(char *buffer, size_t length)  override { return read((uint8_t*)buffer, length); } size_t read(uint8_t* buf, size_t size); bool seek(uint32_t pos, SeekMode mode); bool seek(uint32_t pos) { return seek(pos, SeekSet); } size_t position() const; size_t size() const; void close(); operator bool() const; const char* name() const; String readString() override; protected: FileImplPtr _p; }; class Dir { public: Dir(DirImplPtr impl = DirImplPtr()): _impl(impl) { } File openFile(const char* mode); String fileName(); size_t fileSize(); bool next(); protected: DirImplPtr _impl; }; struct FSInfo { size_t totalBytes; size_t usedBytes; size_t blockSize; size_t pageSize; size_t maxOpenFiles; size_t maxPathLength; }; class FS { public: FS(FSImplPtr impl) : _impl(impl) { } bool begin(); void end(); bool format(); bool info(FSInfo& info); File open(const char* path, const char* mode); File open(const String& path, const char* mode); bool exists(const char* path); bool exists(const String& path); Dir openDir(const char* path); Dir openDir(const String& path); bool remove(const char* path); bool remove(const String& path); bool rename(const char* pathFrom, const char* pathTo); bool rename(const String& pathFrom, const String& pathTo); protected: FSImplPtr _impl; }; } // namespace fs
 #ifndef FS_NO_GLOBALS using fs::FS; using fs::File; using fs::Dir; using fs::SeekMode; using fs::SeekSet; using fs::SeekCur; using fs::SeekEnd; using fs::FSInfo; #endif //FS_NO_GLOBALS

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)
extern fs::FS SPIFFS; #endif

#endif //FS_H

 

一、通過程序向閃存文件系統寫入信息

  需要注意的是,在使用閃存文件系統的時候,在工具--Flash Size----4M(1M SPIFFS)不可以選擇(no SPIFFS)的,如圖1

 

 

 圖1

 

 

 圖2

二、通過程序從閃存文件系統中讀取信息

代碼:

代碼部分將寫和讀都包括在內,格式化結束后,向文件系統寫入信息(注,寫入操作是從文件系統最開始寫入信息和添加信息有所不同),之后在通過讀,讀出內容,在用串口發送到串口監視器上。

#include <FS.h> String file_name="/taichi-maker/notes.txt";//被讀取的文件位置和名稱
void setup() { Serial.begin(9600); Serial.println(""); Serial.println("SPIFFS format start"); SPIFFS.format();//格式化SPIFFS
  Serial.println("SPIFFS format finish"); if(SPIFFS.begin()) { Serial.println("SPIFFS Started"); } else { Serial.println("SPIFFS Failed to Start"); } File dataFile=SPIFFS.open(file_name,"w");//建立File對象用於向SPIFFS中的file對象,“w”,就是寫的意思
  dataFile.print("Hello IOT World ,Second Time");//向dataFile寫入字符串信息
  dataFile.close();//完成文件寫入后關閉
  Serial.println("Finished Writing data to SPIFFS"); //讀取文件系統中的內容
  if (SPIFFS.exists(file_name)) { Serial.print(file_name); Serial.print(" FOUND"); } else { Serial.print(file_name); Serial.print(" NOT FOUND"); } dataFile=SPIFFS.open(file_name,"r");//讀取文件內容且通過串口監視器打印出來
  for(int i=0;i<dataFile.size();i++) { Serial.print((char)dataFile.read()); } dataFile.close();//完成文件寫入后關閉
 } void loop() { // put your main code here, to run repeatedly:
 }

 

 

  圖3

 

 

三、向文件系統添加信息

添加信息和寫入信息不同的地方就是,寫入信息不管你之前有沒有入過信息,他都直接從第一個空間開始寫入,而添加呢,則就是在你之前寫過信息的基礎上,也就是在之前信息的結尾,加上需要添加的信息。

 1 #include <FS.h>
 2 String file_name="/taichi-maker/notes.txt";//被讀取的文件位置和名稱
 3 void setup()  4 {  5   Serial.begin(9600);  6   Serial.println("");  7   Serial.println("SPIFFS format start");  8   //SPIFFS.format();//格式化SPIFFS  9   //Serial.println("SPIFFS format finish");
10 
11   if(SPIFFS.begin()) 12  { 13       Serial.println("SPIFFS Started"); 14  } 15   else 
16  { 17       Serial.println("SPIFFS Failed to Start"); 18  } 19   File dataFile=SPIFFS.open(file_name,"a");//建立File對象用於向SPIFFS中的file對象,“a”,就是添加的意思
20   dataFile.println("This is Appended Info.");//向dataFile的結尾添加字符串信息
21   dataFile.close();//完成文件寫入后關閉
22   Serial.println("Finished Appended data to SPIFFS"); 23 
24   if (SPIFFS.exists(file_name)) 25  { 26  Serial.print(file_name); 27       Serial.print(" FOUND"); 28  } 29   else
30  { 31  Serial.print(file_name); 32       Serial.println(" NOT FOUND"); 33  } 34    dataFile=SPIFFS.open(file_name,"r");//讀取文件內容且通過串口監視器打印出來
35   for(int i=0;i<dataFile.size();i++) 36  { 37       Serial.print((char)dataFile.read()); 38  } 39   dataFile.close();//完成文件寫入后關閉
40 
41 } 42 
43 void loop() 44 { 45   // put your main code here, to run repeatedly:
46 
47 }

 

 

 

 圖4

四、查看閃存文件系統的目錄下的文件

代碼:

#include <FS.h> String file_name="/taichi-maker/myFile.txt";//被讀取的文件位置和名稱
String folder_name="/taichi-maker";//被讀取的文件夾名稱
void setup() { Serial.begin(9600); Serial.println(""); Serial.println("SPIFFS format start"); //SPIFFS.format();//格式化SPIFFS //Serial.println("SPIFFS format finish");
  if(SPIFFS.begin()) { Serial.println("SPIFFS Started"); } else { Serial.println("SPIFFS Failed to Start"); } File dataFile=SPIFFS.open(file_name,"w");//建立File對象用於向SPIFFS中的file對象,“w”,就是寫入的意思
  dataFile.println("Hello Taichi-maker");//向myfile.txt寫入數據
  dataFile.close();//完成文件寫入后關閉
  Serial.println("Finished Appended data to SPIFFS"); //顯示目錄中文件內容以及文件大小
  Dir dir =SPIFFS.openDir(folder_name);//建立“目錄對象”dir
  while(dir.next())//dir.next()用於檢查目錄中是否還有”下一個文件“
 { Serial.println(dir.fileName());//輸出文件名
 } } void loop() { // put your main code here, to run repeatedly:
 }

 

 

 

 

 

圖5

 

五、從閃存文件系統中刪除文件

 

 

 

 代碼

#include <FS.h> String file_name="/taichi-maker/notes.txt";//被讀取的文件位置和名稱
void setup() { Serial.begin(9600); Serial.println(""); Serial.println("SPIFFS format start"); //SPIFFS.format();//格式化SPIFFS //Serial.println("SPIFFS format finish");

  if(SPIFFS.begin()) { Serial.println("SPIFFS Started"); } else { Serial.println("SPIFFS Failed to Start"); } if (SPIFFS.remove(file_name))//remove 函數刪除note.txt文件,同時返回值
 { Serial.print(file_name); Serial.println ("remove sucess"); } else { Serial.print(file_name); Serial.println ("remove fail"); } } void loop() { // put your main code here, to run repeatedly:
 }

可以看到我們打印的消息顯示失敗了如圖6,因為在我們讓串口監視器顯示之前,我們是按過一次復位鍵了,也就是說,按復位之前程序已經執行過了,所以我們再次刪除一個不存在的文件是不可能成功的。為了驗證我們是否成功,可以將之前讀取文件夾內容的程序燒進NODEMCU。

 

 圖6

可以看到如圖7所示,這說明我們成功的刪除了note.txt文件了。

 

 

圖7

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM