Storing Json Configuration Information in devices

I’m a big fan of Json. It is a great way of expressing values in a meaningful way. I’m going to use it to store settings information in our Air Quality sensors. This will make it easy to understand, and extensible. It turns out that it is also very easy to do. I started with an online json editor at https://jsoneditoronline.org/ That helped me come up with this:

{
  "ver":1,
  "wifi": [
    {"ssid":"ssid", "password":"pass"},
    {"ssid":"", "password":""},
    {"ssid":"", "password":""},
    {"ssid":"", "password":""},
    {"ssid":"", "password":""}    
    ],
  "mqtt":{
    "mqttID":"robert01",
    "mqttHost":"mqtt.connectedhumber.org",
    "mqttUser":"connectedHumber",
    "mqttPassword":"pass",
    "mqttPublish":"airquality/data",
    "mqttSubscribe":"airquality/commands",
    "mqttIntervalSecs":60,
    "mqttRetrySecs":20
  },
  "node":{
    "nodeID":"sensor01",
    "noOfPixels":12,
    "pixelColour":{"r":0,"g":255,"b":0},
    "airqSensorType":1,
    "airQLowLimit":1,
    "airqLowWarnLimit":2,
      "airqMidWarnLimit":3,
      "airqHighWarnLimit":4,
      "airQHighAlertLimit":5
  }
}

The json design provides all the information that a sensor needs, including the WiFi settings for 5 different networks. MQTT connection settings and the limits for my warning displays on the coloured pixel.

Next, I needed the C++ to convert the Json into settings that my code inside the device can use to load and store the values when the device runs. It turns out that the Arduino json library has an awesome web page where you can just paste your json and out drops C++ to read and write the values.

I just went to https://arduinojson.org/v5/assistant/ , dropped my Json design into the pate and out came the code. I’ve got to map the settings values onto the variables I’m using in the program, but that is much easier than writing everything from scratch.

This won’t work with the very small Arduino devices because they haven’t really got enough memory to run such large libraries. However, if you’re using an esp8266 or esp32 this really is an easy way to manage internal settings. I’m going to store the json itself using the internal filestore. I’ll post how to do this in a little while.