我正在尝试利用esp8266的spi文件系统来保存一些配置数据。我正在使用以下代码对此进行测试。
#include <ESP8266WiFi.h>
#include <FS.h> //Include File System Headers
const char* filename = "/samplefile.txt";
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
//Initialize File System
if(SPIFFS.begin())
{
Serial.println("SPIFFS Initialize....ok");
}
else
{
Serial.println("SPIFFS Initialization...failed");
}
//Format File System
if(SPIFFS.format())
{
Serial.println("File System Formated");
}
else
{
Serial.println("File System Formatting Error");
}
//Create New File And Write Data to It
//w=Write Open file for writing
File f = SPIFFS.open(filename, "w");
if (!f) {
Serial.println("file open failed");
}
else
{
//Write data to file
Serial.println("Writing Data to File");
f.print("This is sample data which is written in file");
f.close(); //Close file
}
}
void loop() {
int i;
//Read File data
File f = SPIFFS.open(filename, "r");
if (!f) {
Serial.println("file open failed");
}
else
{
Serial.println("Reading Data from File:");
//Data from file
for(i=0;i<f.size();i++) //Read upto complete file size
{
Serial.print((char)f.read());
}
f.close(); //Close file
Serial.println("File Closed");
}
delay(5000);
}
当我使用Arduino IDE串行监视器运行草图时,一切似乎都正常,但是当将数据打印回去时,它只会打印垃圾。 我尝试从ide中选择spiffs的大小,但是每次失败时都会选择。我使用了另一个模块,该模块可以很好地打印文件内容
将垃圾打印为文件内容的模块可能有问题。
请注意,在两个模块上使用EEPROM都可以。