Skip to main content

Overview

BaseSensV2 is the abstract base class for all sensors in the SensorModuleV2 framework. It provides type-safe value access, metadata management, calibration support, and advanced features for modern sensor implementations. Inheritance: All V2 sensors inherit from BaseSensV2 Header: lib/sensors/SensorModuleV2/SensorModule/SensorModuleV2.h

Class Definition

class BaseSensV2 {
public:
    BaseSensV2();
    virtual ~BaseSensV2();
    
    // Pure virtual methods
    virtual bool init() = 0;
    virtual bool update() = 0;
    virtual bool isUpdated() const { return false; }
    
    // Type-safe value access
    template<typename T>
    T getValue(const char *key) const;
    
    template<typename T>
    void updateValue(const char *key, T value);
    
    // Metadata and configuration
    void addValueInfo(const char *key, const char *label, 
                      const char *unit, uint8_t precision = 3, 
                      bool calibrable = true);
    
    // ... (see sections below for complete API)
};

Core Methods

init()

Initializes the sensor hardware and configuration.
virtual bool init() = 0;
return
bool
true if initialization succeeded, false otherwise
Pure virtual function - must be implemented by derived classes.

update()

Reads current sensor data and updates internal values.
virtual bool update() = 0;
return
bool
true if update succeeded, false otherwise

isUpdated()

Checks if sensor data was updated in the last cycle.
virtual bool isUpdated() const;
return
bool
true if data was updated in the last update() call
Example:
sensorModule.update();
if (sensorModule.isUpdated("temperature")) {
    float temp = sensorModule.getValue<float>("temperature", "value");
    Serial.println(temp);
}

Type-Safe Value Access

getValue()

Retrieves a typed value from the sensor.
template<typename T>
T getValue(const char *key) const;
T
template
Type of the value to retrieve (float, int, bool, const char*, etc.)
key
const char*
required
Key name of the value to retrieve
return
T
The requested value with compile-time type safety
Example:
float temperature = sensor.getValue<float>("temperature");
int counter = sensor.getValue<int>("counter");
const char* status = sensor.getValue<const char*>("status");

updateValue()

Updates a sensor value with type-safe assignment.
template<typename T>
void updateValue(const char *key, T value);

// Specialized overloads
void updateValue(const char *key, float value);
void updateValue(const char *key, int value);
void updateValue(const char *key, const char *value);
key
const char*
required
Key name for the value
value
T
required
Value to store
Example:
updateValue("temperature", 25.5f);
updateValue("humidity", 65);
updateValue("status", "OK");

Convenience Getters

float getFloatValue(const char *key) const;
int getIntValue(const char *key) const;
const char *getStringValue(const char *key) const;
JsonObject getObject(const char *key) const;
JsonArray getArray(const char *key) const;
Example:
float temp = sensor.getFloatValue("temperature");
int count = sensor.getIntValue("counter");
JsonObject config = sensor.getObject("config");

Value Metadata

addValueInfo()

Registers metadata for a sensor value.
void addValueInfo(const char *key, const char *label, 
                  const char *unit, uint8_t precision = 3, 
                  bool calibrable = true);
key
const char*
required
Unique identifier for the value
label
const char*
required
Human-readable label
unit
const char*
required
Unit of measurement (e.g., “°C”, ”%”, “ppm”)
precision
uint8_t
default:"3"
Number of decimal places for display
calibrable
bool
default:"true"
Whether this value supports calibration
Example:
addValueInfo("temperature", "Temperature", "°C", 1, true);
addValueInfo("humidity", "Humidity", "%", 1, true);
addValueInfo("status", "Status", "", 0, false);

getValueInfo()

Retrieves metadata for a value.
const SensorValueInfo *getValueInfo(uint8_t index) const;
const SensorValueInfo *getValueInfoByKey(const char *key) const;
uint8_t getValueCount() const;
SensorValueInfo Structure:
struct SensorValueInfo {
    char *key;           // Value identifier
    char *label;         // Display label
    char *unit;          // Unit of measurement
    uint8_t precision;   // Decimal places
    bool visible;        // Display visibility
};

Value Visibility

void setValueVisibility(const char *key, bool visible);
bool isValueVisible(const char *key) const;
key
const char*
required
Value identifier
visible
bool
required
Visibility state
Example:
// Hide debug values from display
sensor.setValueVisibility("debug_counter", false);

Type Checking

hasValue()

Checks if a value exists.
bool hasValue(const char *key) const;

getValueTypeCode()

Retrieves the type of a stored value.
SensorTypeCode getValueTypeCode(const char *key) const;
static const char *getTypeCodeName(SensorTypeCode typeCode);
static bool isNumericTypeCode(SensorTypeCode typeCode);
SensorTypeCode Enum:
enum SensorTypeCode {
    TYPE_UNKNOWN = 0,
    TYPE_BOOL = 1,
    TYPE_INT = 2,
    TYPE_FLOAT = 3,
    TYPE_DOUBLE = 4,
    TYPE_LONG = 5,
    TYPE_UINT = 6,
    TYPE_ULONG = 7,
    TYPE_STRING = 8,
    TYPE_OBJECT = 9,
    TYPE_ARRAY = 10
};
Example:
if (sensor.isNumericValue("temperature")) {
    float val = sensor.getFloatValue("temperature");
}

Path-Based Access

Access nested JSON values using dot notation.
template<typename T>
T getValueAtPath(const char *path) const;

template<typename T>
void updateValueAtPath(const char *path, T value);

bool hasPath(const char *path) const;
SensorTypeCode getTypeAtPath(const char *path) const;
Example:
// Access nested values
float temp = sensor.getValueAtPath<float>("climate.temperature");
int index = sensor.getValueAtPath<int>("data.readings[0]");

// Update nested values
sensor.updateValueAtPath("config.threshold", 50.0f);

Calibration Support

setValueCalibrable()

Configures whether a value can be calibrated.
void setValueCalibrable(const char *key, bool calibrable = true);
bool isValueCalibrable(const char *key) const;

enableCalibration()

Enables or disables calibration system.
void enableCalibration(bool enable = true);
bool isCalibrationEnabled() const;
Example:
sensor.setValueCalibrable("temperature", true);
sensor.setValueCalibrable("timestamp", false);
sensor.enableCalibration(true);

Document Methods

setDocument() / setDocumentValue()

Internal methods for JSON document management.
void setDocument(const char *objName);
void setDocumentValue(JsonDocument *docBase);
JsonDocument *getDocument() const;
JsonVariant getVariant(const char *key) const;
These methods are typically called by SensorModuleV2 during initialization.

Usage Example

#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_ANALOG_V2
#include "Kinematrix.h"

class CustomSensor : public BaseSensV2 {
private:
    uint8_t _pin;
    
public:
    CustomSensor(uint8_t pin) : _pin(pin) {
        // Register value metadata
        addValueInfo("voltage", "Voltage", "V", 2, true);
        addValueInfo("raw", "Raw ADC", "", 0, false);
    }
    
    bool init() override {
        pinMode(_pin, INPUT);
        return true;
    }
    
    bool update() override {
        int raw = analogRead(_pin);
        float voltage = (raw / 4095.0f) * 3.3f;
        
        updateValue("raw", raw);
        updateValue("voltage", voltage);
        
        return true;
    }
};

void setup() {
    SensorModuleV2 sensors;
    sensors.addSensor("custom", new CustomSensor(A0));
    sensors.init();
}

void loop() {
    sensors.update();
    
    float voltage = sensors.getValue<float>("custom", "voltage");
    Serial.println(voltage);
    
    delay(1000);
}

Inheritance Hierarchy

BaseSensV2 (abstract)
├── AbstractSensV2 (test/example sensor)
├── AnalogSensV2
├── DHTSensV2
├── BME680SensV2
├── INA219SensV2
├── MLX90614SensV2
├── MQSensV2
├── BaseRTCSensV2
│   ├── RTCDS1307SensV2
│   ├── RTCDS3231SensV2
│   ├── RTCPCF8523SensV2
│   └── RTCPCF8563SensV2
└── [Additional V2 sensors]

Key Differences from V1

Type Safety

Template-based getValue/updateValue with compile-time type checking

Metadata

Built-in value info with labels, units, and precision

Path Access

Dot notation for nested JSON navigation

Calibration

Integrated calibration configuration per value

See Also

Build docs developers (and LLMs) love