0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

wemosでspiffs

Posted at

概要

wemos d1 r1でspiffsやってみた。

サンプルコード

# include <Arduino.h>
# include <FS.h>

void setup() {
	Serial.begin(115200);
	bool res = SPIFFS.begin();
	if (!res) 
	{
		Serial.printf("SPIFFS error \n");
		while (true) {};
	}
	FSInfo fs_info;
	SPIFFS.info(fs_info);
	Serial.println("");
	Serial.println(">SPIFFS info");
	Serial.println("totalBytes: " + String(fs_info.totalBytes));
	Serial.println("usedBytes: " + String(fs_info.usedBytes));
	Serial.println("blockSize: " + String(fs_info.blockSize));
	Serial.println("pageSize: " + String(fs_info.pageSize));
	Serial.println("maxOpenFiles: " + String(fs_info.maxOpenFiles));
	Serial.println("maxPathLength: " + String(fs_info.maxPathLength));
	Serial.println("ok");
	Serial.println(">File list");
	Dir dir = SPIFFS.openDir("/");
	while (dir.next()) 
	{
		String fn, fs;
		fn = dir.fileName();
		fn.remove(0, 1);
		fs = String(dir.fileSize());
		Serial.printf("<%s> size=%s\n", fn.c_str(), fs.c_str());
	}
	Serial.println("ok");
	Serial.println(">File write");
	File fd = SPIFFS.open("/test.txt", "w");
	if (!fd) 
	{
		Serial.println("open error1");
		return;
	}
	fd.println("0123456789 abcdefghi");
	fd.close();
	Serial.println("ok");
	Serial.println(">File read");
	File fd2 = SPIFFS.open("/test.txt", "r");
	if (!fd2) 
	{
		Serial.println("open error2");
		return;
	}
	int len = fd2.available();
	Serial.printf("file size=%d\n", len);
	String str = fd2.readString();
	Serial.println(str);
	fd2.close();
	Serial.println("ok");
	Serial.println(">File write");
	File fileToWrite = SPIFFS.open("/test.txt", "w");
	if (!fileToWrite)
	{
		Serial.println("There was an error opening the file for writing");
		return;
	}
	if (fileToWrite.println("ORIGINAL FILE LINE"))
	{
		Serial.println("File was written");
	} 
	else 
	{
		Serial.println("File write failed");
	}
	fileToWrite.close();
	Serial.println("ok");
	Serial.println(">File append");
	File fileToAppend = SPIFFS.open("/test.txt", "a");
	if (!fileToAppend)
	{
		Serial.println("There was an error opening the file for appending");
		return;
	}
	if (fileToAppend.println("APPENDED FILE LINE"))
	{
		Serial.println("File content was appended");
	} 
	else 
	{
		Serial.println("File append failed");
	}
	fileToAppend.close();
	Serial.println("ok");
	Serial.println(">File read");
	File fileToRead = SPIFFS.open("/test.txt", "r");
	if (!fileToRead)
	{
		Serial.println("Failed to open file for reading");
		return;
	}
	Serial.println("File Content:");
	while (fileToRead.available())
	{
		Serial.write(fileToRead.read());
	}
	fileToRead.close();
	Serial.println("ok");
}
void loop() {
	delay(300);
}





実行結果

>SPIFFS info
totalBytes: 957314
usedBytes: 502
blockSize: 8192
pageSize: 256
maxOpenFiles: 5
maxPathLength: 32
ok
>File list
<test.txt> size=40
                  ok
>File write
ok
>File read
file size=22
            0123456789 abcdefghi

ok
>File write
File was written
ok
>File append
File content was appended
ok
>File read
File Content:
ORIGINAL FILE LINE
APPENDED FILE LINE
ok

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?