Skip to content

Parse config file from SPIFFS on a ESP32 #1215

Closed
@DavidTruyens

Description

@DavidTruyens

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    v6ArduinoJson 6

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions