1. 程式人生 > 實用技巧 >Arduino ESP8266 SPIFFS學習 FS.h

Arduino ESP8266 SPIFFS學習 FS.h

安裝檔案系統

  • 下載檔案系統上傳工具工具地址
  • 匯入工具參考目錄 ******/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

ESP8266FS外掛其實只是在當前專案目錄下建立了一個data目錄(如果沒有這個data目錄可以自己建立),我們只要將需要上傳到晶片檔案系統的內容放置在這個 data目錄中就可以了,然後點選ESP8266 Skech Data Upload Arduino IDE就會將這個目錄的檔案寫入到SPIFFS中了。要注意的是檔案的大小不能超過板子SPIFFS的大小,否則會上傳失敗。

我們就嘗試將一個index.html網頁檔案放到data目錄,然後將其上傳到ESP8266中,接下來用以下的程式碼將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\r\n", 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之前就呼叫,一般都會在setup()過程呼叫。該方法如果呼叫成功將會返回true,否則返回false

format

SPIFFS.format()

  格式化檔案系統。返回true表示格式化成功。

open

SPIFFS.open(path, mode)

開啟指定位置上的一個檔案並返回File物件。

  • path - 檔案的路徑(如:/test.text)
  • mode - 檔案的讀寫模式,可以為 "r", "w", "a", "r+", "w+", "a+"中的任意一個,這個與C言語中訪問檔案系統的方式是一樣的。

該方法返用成功後會返回一個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)

目錄物件常用於列舉,它會提供三個方法:next(),fileName(), 和 openFile(mode)

以下例子用於列舉指定目錄下的子目錄、檔名和檔案大小:

Dir dir = SPIFFS.openDir("/data");
while (dir.next()) {
    Serial.print(dir.fileName());
    File f = dir.openFile("r");
    Serial.println(f.size());
}

dir.next()返回真時就表示目錄列舉完成。它的呼叫必須早於fileNameopenFile函式。

檔案物件

SPIFFS.opendir.openFile 函式都可以返回一個File檔案物件例項。這個物件用於處理所有的檔案流,例如:readBytes, findUntil, parseInt, 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
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。