How to handle JSON config of ESP32-based device

_void_
Posts: 1
Joined: Sun Sep 12, 2021 3:58 pm

How to handle JSON config of ESP32-based device

Postby _void_ » Sun Sep 12, 2021 4:25 pm

Dear forum users!
I am newbie in ESP32 programming and in embedded systems themselves. So sorry in advance if I ask something widely knwon or stupid.
I have a question regarding storage and handling of global config for my device. I have a ESP32 board with two sensors attached to it. I need to be able to change individual sensor config via BLE.
Each sensor is being represented as a instance of Sensor class.
I want to implement JSON config like this:

Code: Select all

{
  "golbal_param1":"value"
  "golbal_param2":"value"
  "golbal_param3":"value"
 
  "sensors":[
    {
      "id":"sensor0",
      "pin": 0,
      "calibrated": false,
      "threshold": 0,
       "jsonParam1": { }
    },
    {
      "id":"sensor1",
      "pin": 1,
      "calibrated": false,
      "threshold": 0,
       "jsonParam1": { }
    }
  ]
}
and sensor class itself looks like this:

Code: Select all

#ifndef SENSOR_H
#define SENSOR_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif


#include <ArduinoJson.h>
#include "constants.h"

#include "ADS1X15.h"
#include  "SignalSequence.h"

using namespace B;
class sensor {
  public:
    String id;
    
  private:
    struct SensorDataHelper
    {
      float param0 = 0;
      float param1 = 0;
      bool param2  = false;
      unsigned long param3 = 0;
      unsigned long param4 = 0;
      int  param5 = 200;
    };  

    ADS1115 m_ads;
    float m_threshold;
    bool m_calibrated;
    int m_pin;

  public:

    // calibrates device and sets calibration flag to true if success
    bool calibrate();
    // initializes ADS and sets intial variables
    void init();
    // this function is being called continously in void loop() and some decisions are being maed based on it's return value
    int has_data();
    float get_thresh();
    bool isCalibrated();

    void resetCalibrationFlag();
    sensor(JsonObject t_json_conf, ADS1115 t_ads);

};
#endif


And I want to make this config changeable by BLE callbacks and accessible in my entire program. I am thinking of having a Config class or a Singleton that will read my data from SPIFFS , parse them and initialize sensor instances.

So far I have 2 questions:
1. Are my thoughts on handling and storing config correct?
2. How should I keep changes in sync Between BLE callbacks , global config instance and config.json content
Thanks in advance!

Who is online

Users browsing this forum: No registered users and 40 guests