Skip to main content

Overview

BaseSens is the abstract base class for all sensors in the SensorModuleV1 framework. It provides a unified interface for sensor initialization, updates, and data access through JSON documents. Inheritance: All V1 sensors inherit from BaseSens Header: lib/sensors/SensorModuleV1/base/sensor-module.h

Class Definition

class BaseSens {
public:
    virtual bool init() = 0;
    virtual bool update() = 0;
    
    virtual void getValue(float *output);
    virtual void getValue(int *output);
    virtual float getValueF() const;
    
    virtual void setDocument(const char *objName);
    virtual void setDocumentValue(JsonDocument *docBase);
    virtual JsonDocument getDocument();
    virtual JsonVariant getVariant(const char *searchName);
    
    virtual void process();
};

Core Methods

init()

Initializes the sensor hardware and configuration.
virtual bool init() = 0;
Returns
bool
true if initialization succeeded, false otherwise
This is a pure virtual function that must be implemented by derived sensor classes.

update()

Reads current sensor data and updates internal values.
virtual bool update() = 0;
Returns
bool
true if update succeeded, false otherwise
This is a pure virtual function that must be implemented by derived sensor classes.

getValue()

Retrieves sensor value through a pointer parameter.
virtual void getValue(float *output);
virtual void getValue(int *output);
output
float*
required
Pointer to store the sensor value
Example:
DHTSens dht(2, DHT22);
float temperature;
dht.getValue(&temperature);

getValueF()

Returns sensor value directly as a float.
virtual float getValueF() const;
return
float
Current sensor value
Example:
float temp = dht.getValueF();

Document Methods

setDocument()

Sets the object name for JSON document storage.
virtual void setDocument(const char *objName);
objName
const char*
required
Name identifier for this sensor in the JSON document
Usually called automatically by SensorModule during initialization.

setDocumentValue()

Assigns the shared JSON document for data storage.
virtual void setDocumentValue(JsonDocument *docBase);
docBase
JsonDocument*
required
Pointer to the shared JSON document

getDocument()

Retrieves the entire JSON document.
virtual JsonDocument getDocument();
return
JsonDocument
The complete JSON document containing sensor data

getVariant()

Retrieves a specific value from the JSON document.
virtual JsonVariant getVariant(const char *searchName);
searchName
const char*
required
Key name to search for in the document
return
JsonVariant
JSON variant containing the requested value
Example:
JsonVariant humidity = sensor.getVariant("humidity");
Serial.println(humidity.as<float>());

Additional Methods

process()

Performs additional processing on sensor data.
virtual void process();
Optional method that can be overridden for custom processing logic.

Usage Example

#define ENABLE_SENSOR_MODULE_V1
#define ENABLE_SENSOR_DHT_V1
#include "Kinematrix.h"

DHTSens dht(2, DHT22);

void setup() {
    Serial.begin(115200);
    
    // Initialize sensor
    if (dht.init()) {
        Serial.println("DHT sensor initialized");
    }
}

void loop() {
    // Update sensor data
    dht.update();
    
    // Get values
    float temp = dht.getValueF();
    Serial.print("Temperature: ");
    Serial.println(temp);
    
    delay(2000);
}

Inheritance Hierarchy

BaseSens (abstract)
├── Abstract
├── AnalogSens
├── DigitalSens
├── DHTSens
├── BME280Sens
├── DS18B20Sens
├── MAX31865Sens
├── INA219Sens
├── GPSSens
├── RFIDSens
└── [40+ other sensor implementations]

JSON Document Structure

Sensors store data in a shared JSON document:
{
  "DHT": 25.5,
  "BME280": {
    "temperature": 24.8,
    "humidity": 65.2,
    "pressure": 1013.25
  },
  "Analog": 512
}

See Also

Build docs developers (and LLMs) love