在 Arduino ESP8266 上加载配置参数

Load config parameters on Arduino ESP8266

我正在使用 Arduino ESP8266 在 SPIFSS 上存储和加载配置设置。我用这个 ConfigFile.ino 作为参考示例。

https://github.com/esp8266/Arduino/blob/master/libraries/esp8266/examples/ConfigFile/ConfigFile.ino

此函数将配置设置加载到变量 serverNameaccessToken

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);

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& json = jsonBuffer.parseObject(buf.get());

  if (!json.success()) {
    Serial.println("Failed to parse config file");
    return false;
  }

  const char* serverName = json["serverName"];
  const char* accessToken = json["accessToken"];

  // Real world application would store these values in some variables for
  // later use.

  Serial.print("Loaded serverName: ");
  Serial.println(serverName);
  Serial.print("Loaded accessToken: ");
  Serial.println(accessToken);
  return true;
}

我对该函数进行了一些修改,以将配置设置加载到结构中。

struct ConfigSettingsStruct
{
    String ssid;
    String password;
};

ConfigSettingsStruct ConfigSettings;

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);

    StaticJsonBuffer<200> jsonBuffer;
    JsonObject& json = jsonBuffer.parseObject(buf.get());

    if (!json.success()) {
        Serial.println("Failed to parse config file");
        return false;
    }

    //const char* serverName = json["serverName"];
    //const char* accessToken = json["accessToken"];

    char ssid_[30];
    strcpy(ssid_, json["ssid"]);
    ConfigSettings.ssid = String(ssid_);

    char password_[30];
    strcpy(password_, json["password"]);
    ConfigSettings.password = String(password_);

    // Real world application would store these values in some variables for
    // later use.

    Serial.print("Loaded ssid: ");
    Serial.println(ConfigSettings.ssid);
    Serial.print("Loaded password: ");
    Serial.println(ConfigSettings.password);

    return true;
}

在我下载代码和 运行 ESP8266 后,WiFi 芯片因一些堆栈错误而重置。我的代码有什么问题?如何将配置设置正确加载到 ConfigSettings?

问题中你的代码没有问题。它应该工作。我强烈怀疑堆栈错误的原因在别处。请再次仔细检查您的代码。

这不算作答案,但可以提醒您去别处看看。您可能看错地方了。

请注意;

之后你可能有内存泄漏
std::unique_ptr<char[]> buf(new char[size]);

我建议你使用 malloc 分配一些内存(这不是时尚但经典)并最终释放它。您还需要在 returns.

之前关闭文件

另外,您的 ssid 和密码缓冲区长度不够。最大 ssid 长度必须为 32。假设你有一个基于 psk 的加密,你需要将传递缓冲区长度增加到 64。

微小但是;也许你可以考虑在 struct define 之前添加一个 typedef,尽管 C++ 线程可以在命名空间中定义它们。