我正在使用以下代码使用 SPIFFS 将 Config.json 文件存储到 ESP32 闪存
#include <ArduinoJson.h>
#include <FS.h>
#include<SPIFFS.h>
bool loadConfig() {
File configFile = SPIFFS.open("/Config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
Serial.println(buf.get());
StaticJsonBuffer<1024> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success()) {
Serial.println("Failed to parse config file");
return false;
}
const char* ssid = json["ssid"];
const char* password = json["password"];
// Real world application would store these values in some variables for
// later use.
Serial.print("Loaded ssid: ");
Serial.println(ssid);
Serial.print("Loaded password: ");
Serial.println(password);
return true;
}
void setup() {
Serial.begin(115200);
Serial.println("");
delay(1000);
Serial.println("Mounting FS...");
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
if (!loadConfig()) {
Serial.println("Failed to load config");
}
else {
Serial.println("Config loaded");
}
}
void loop() {
yield();
}
但是解析失败,我在串行监视器上收到以下消息:Mounting FS...⸮xV⸮⸮⸮⸮⸮ 无法解析配置文件 无法加载配置
- 我的 Arduino IDE 版本:1.8.13 (Windows)
- 配置文件有 2 个对象:
{
"ssid": "ESP32",
"password": "Softronics"
}
先感谢您