|
| 1 | +## How to create a Sensor |
| 2 | + |
| 3 | +*We will take the VL53L0X ToF sensor as exemple.* |
| 4 | +1. Make sure you include the required libraries in the `platformio.ini` file: |
| 5 | + |
| 6 | +```ini |
| 7 | +lib_deps = |
| 8 | + Adafruit_VL53L0X |
| 9 | +``` |
| 10 | + |
| 11 | +2. Start from a simple example file from your librairy. Here, `Adafruit_VL53L0X/examples/vl53l0x/vl53l0x.ino` |
| 12 | + |
| 13 | +3. Copy the code from `src\sensor\TestSensor.h` to your sensor name. Here `src\sensor\vl5310x.ino` |
| 14 | + |
| 15 | +4. Replace everywhere `TestSensor` with your sensor name. Here `Vlx53l0x`. And include the required libraries. Here: |
| 16 | +```cpp |
| 17 | +//Keep them |
| 18 | +#include "CSensor.h" |
| 19 | +#include <Arduino.h> |
| 20 | +//And add yours: |
| 21 | +#include "Adafruit_VL53L0X.h" |
| 22 | +``` |
| 23 | + |
| 24 | +5. Your example might have global variable. Place them in the class as private. Here: |
| 25 | +```cpp |
| 26 | +class Vlx53l0x : public CSensor |
| 27 | +{ |
| 28 | +private: |
| 29 | + Adafruit_VL53L0X lox = Adafruit_VL53L0X(); |
| 30 | +``` |
| 31 | +
|
| 32 | +
|
| 33 | +
|
| 34 | +5. Your sensor will probably need some variables to be configured by the user (eg GPIO ports numbers, min, max etc...) |
| 35 | +> Place your requiered variables in the private section of the class of your sensor. |
| 36 | +
|
| 37 | +```cpp |
| 38 | +class Vlx53l0x : public CSensor |
| 39 | +{ |
| 40 | +private: |
| 41 | + Adafruit_VL53L0X lox = Adafruit_VL53L0X(); |
| 42 | + int scl; |
| 43 | + int sda; |
| 44 | +
|
| 45 | +public: |
| 46 | +... |
| 47 | +``` |
| 48 | + |
| 49 | +6. Adjust the constructor of your sensor to initialize the values from the user interface/config file: |
| 50 | + |
| 51 | +```cpp |
| 52 | + Vlx53l0x( JsonObject& sensorConf): CSensor(sensorConf){ |
| 53 | + sda = sensorConf["driver"]["config"]["sda"].as<int>(); |
| 54 | + scl = sensorConf["driver"]["config"]["scl"].as<int>(); |
| 55 | + }; |
| 56 | +``` |
| 57 | +
|
| 58 | +7. From your example, put the `setup()` content in the `begin()` method. |
| 59 | +```cpp |
| 60 | + void begin() |
| 61 | + { |
| 62 | + lox.begin(); |
| 63 | + Serial.printf("we are starting the random sensor with min: %d and max:%d\n",minVal,maxVal); |
| 64 | + }; |
| 65 | +``` |
| 66 | +8. Now integrate your sensor in `src\SensorSettingsService.h`: |
| 67 | +> 1. Include your sensor file: |
| 68 | +```cpp |
| 69 | +#include "sensor/TestSensor.h" |
| 70 | +#include "sensor/vl53l0x.h"//Here |
| 71 | +#define SENSOR_SETTINGS_FILE "/config/sensorSettings.json" |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +> 2. Include your description: |
| 76 | +```cpp |
| 77 | + static constexpr const char* driverList[] = { |
| 78 | + Vlx53l0x::description, // Here |
| 79 | + BMP180Sensor::description, |
| 80 | +... |
| 81 | +``` |
| 82 | +
|
| 83 | +> 3. Include the name correspondance: The name must match the one on your `description` property. |
| 84 | +```cpp |
| 85 | + // Add here your custom sensors |
| 86 | + if (strcmp(sensorConf["driver"]["name"], "ToF") == 0) { |
| 87 | + return new Vlx53l0x(sensorConf); |
| 88 | + } |
| 89 | +
|
| 90 | +``` |
0 commit comments