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

How to use this library? #1

Closed
renerlemes opened this issue May 6, 2019 · 3 comments
Closed

How to use this library? #1

renerlemes opened this issue May 6, 2019 · 3 comments
Labels
question Further information is requested

Comments

@renerlemes
Copy link

renerlemes commented May 6, 2019

Do you have a example of how to use this library @BauerPh?

@BauerPh
Copy link
Owner

BauerPh commented May 7, 2019

I've used it in this project: click

Quick untested example:

#include <JSONtoSPIFFS.h>

#define CONFIGFILE "config.json"

JSONtoSPIFFS Config;

// Define some variables
bool setting1;
int setting2;
String setting3;

void setup() {
  Serial.begin(115200);

  // init library
  SPIFFS.begin();
  Config.begin(&SPIFFS);

  // set variables
  setting1 = false;
  setting2 = 343;
  setting3 = "hello world";

  // save config
  if (Config.loadConfigFile(CONFIGFILE)) {   // this will also create the configfile if it doesn't exists
    Config.setValue("mySetting1", setting1); // returns true on success
    Config.setValue("mySetting2", setting2);
    Config.setValue("mySetting3", setting3);
    Config.saveConfigFile(); // Commit changes
  }
  
  // change variables
  setting1 = true;
  setting2 = 100;
  setting3 = "no hello world";

  // load config
  if (Config.loadConfigFile(CONFIGFILE)) {
    Config.getValue("mySetting1", setting1);
    Config.getValue("mySetting2", setting2);
    Config.getValue("mySetting3", setting3);
    Config.closeConfigFile(); // dont forget to close the file!
  }

  // check variables
   Serial.println(setting1);
   Serial.println(setting2);
   Serial.println(setting3);

  // Remove "mySetting1"
  if (Config.loadConfigFile(CONFIGFILE)) {
    Config.removeKey("mySetting1");
    Config.saveConfigFile(); // Commit changes
  }

  // Delete configfile
  Config.deleteConfigFile(CONFIGFILE); // file has to be closed!
}

Not that ALL methods return true on success and false when failed!

Also have a look at JSONtoSPIFFS.h to enable Debug-Logging.

@BauerPh BauerPh added the question Further information is requested label May 7, 2019
@renerlemes
Copy link
Author

If I have the following json:

{ "Debug": true, "Device": [ { "Serial": "AUT010001" } ], }

How to update the Serial field?

@BauerPh
Copy link
Owner

BauerPh commented May 7, 2019

afaik nested json data is not supported. Can you modify this to:

{ "Debug": true, "Device": "", "Serial": "AUT010001"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants