Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse config file from SPIFFS on a ESP32 #1215

Closed
DavidTruyens opened this issue Mar 20, 2020 · 5 comments
Closed

Parse config file from SPIFFS on a ESP32 #1215

DavidTruyens opened this issue Mar 20, 2020 · 5 comments
Labels
v6 ArduinoJson 6

Comments

@DavidTruyens
Copy link

Hi Blachon,

I've been using V5 for quite a while. Now decided to move to V6. I've read a couple of time that I just should remove the "readBytes" (as mentioned [here]).(#1159) .

I've tried a lot of different options, but non is working.

Here's where it goes wrong I guess:

/*
    std::unique_ptr<char[]> buf(new char[size]);

    //credFile.readBytes(buf.get(), size);
    credFile.read(buf,size);
    Serial.println(buf.get());
*/
    StaticJsonDocument<1024> jsonBuf;

    DeserializationError error = deserializeJson(jsonBuf, credFile);

So I'm not sure if I still need the buffer or if I can just read the file directly..

Here's the full code:

bool loadConfig()
{

    if (!SPIFFS.begin())
    {
        Serial.println("Failed to mount file system");
        return false;
    }

    File credFile = SPIFFS.open("/cred.json", "r");
    if (!credFile)
    {
        Serial.println("Failed to open cred file");
        return false;
    }
    size_t size = credFile.size();
    Serial.print("Size cred file: ");
    Serial.println(size);
    if (size > 1024)
    {
        Serial.println("Cred file is bigger than 1024");
        return false;
    }
/*
    std::unique_ptr<char[]> buf(new char[size]);

    //credFile.readBytes(buf.get(), size);
    credFile.read(buf,size);
    Serial.println(buf.get());
*/
    StaticJsonDocument<1024> jsonBuf;

    DeserializationError error = deserializeJson(jsonBuf, credFile);

    if (error)
    {
        Serial.println("Failed to parse cred file");
        return false;
    }
    /*
    const char *J_ssid = json["ssid"];
    const char *J_pass = json["pass"];
    const char *J_deviceId = json["ATTID"];
    const char *J_token = json["ATTToken"];
    const char *J_CONFIG_FIRMWARE_UPGRADE_URL = json["upgrade_URL"];
    const char *J_tennant_URL = json["tennant_URL"];
*/
    strcpy(credentials.ssid, jsonBuf["ssid"]);
    strcpy(credentials.pass, jsonBuf["pass"]);
    strcpy(deviceId, jsonBuf["ATTID"]);
    strcpy(token, jsonBuf["ATTToken"]);
    strcpy(CONFIG_FIRMWARE_UPGRADE_URL, jsonBuf["upgrade_URL"]);
    strcpy(certURL, jsonBuf["cert_URL"]);

    Serial.print("SSID: ");
    Serial.println(ssid);
    Serial.print("pass: ");
    Serial.println(pass);
    Serial.print("ATT settings: ");
    Serial.println(deviceId);
    Serial.println(token);

    return true;
}

I'm using an Adafruit ESP32 feather and Platformio on VS code.

Regards,
David

@DavidTruyens
Copy link
Author

I started over all again with a fresh project and this seems to be working just fine.. Now looking for the issues in the bigger project.

Anyway, here's the code as a reference:

void readcredentials()
{
  if (!SPIFFS.begin())
  {
    Serial.println("Failed to mount SPIFFS");
  }
  else
  {
    File file = SPIFFS.open("/cred.json", FILE_READ);

    if (!file)
    {
      Serial.println("There was an error opening the file");
      return;
    }

    else
    {
      Serial.println("File opened!");

      StaticJsonDocument<512> doc;

      DeserializationError error = deserializeJson(doc, file);
      if (error)
      {
        Serial.println("error...");
      }
      else
      {
        Serial.println("Another winner!");
        strcpy(credits.ssid, doc["ssid"]);
        strcpy(credits.pass, doc["pass"]);
        strcpy(credits.MQTTid, doc["MQTTID"]);
        strcpy(credits.MQTTpass, doc["MQTTPass"]);
      }
      Serial.println("");
    }

    file.close();
  }
}

@bblanchon
Copy link
Owner

Hi @DavidTruyens,

Thanks for the update.

For future reference, you can find an example of a configuration file stored on SD in JsonConfigFile.ino.
In addition, Mastering ArduinoJson contains a case study that shows how to store a complex configuration in SPIFFS.

Best regards,
Benoit

@DavidTruyens
Copy link
Author

DavidTruyens commented Mar 20, 2020 via email

@bblanchon
Copy link
Owner

If you purchase the ebook, you should automatically get the new one for free.
If you have a paperback version, please drop me an email.

@DavidTruyens
Copy link
Author

DavidTruyens commented Mar 20, 2020 via email

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants