Sensora library overview
Sensora library is based on Arduino framework. The functionality and characteristics of your devices are defined by properties. When you add a new device to your Sensora project, you will define its properties based on the device's capabilities and your project needs.
Property
Each device contains properties, which are individual pieces of data or control points that can be monitored or manipulated.
For instance, a smart light bulb might expose these properties:
Power
power
- indicates whether the light is turned on/off
cpp
#include <Arduino.h>
#include <EspWifi.h>
// declare properties outside setup() method
Property lightSwitch("power");
void setup() {
lightSwitch.setDataType(DataType::Boolean).setAccessMode(AccessMode::Write);
Sensora.setup();
}
void loop() {
// write your sensor logic here
Sensora.loop();
}
Brightness
brightness
- controls the light intensity, typically represented as a percentage (0-100)
cpp
#include <Arduino.h>
#include <EspWifi.h>
// declare properties outside setup() method
Property lightSwitch("power");
Property brightness("brightness");
void setup() {
lightSwitch.setDataType(DataType::Boolean).setAccessMode(AccessMode::Write);
brightness.setDataType(DataType::Integer).setAccessMode(AccessMode::ReadWrite);
Sensora.setup();
}
void loop() {
// write your sensor logic here
Sensora.loop();
}
Subscribe for new data
By default, the properties can only send data to Sensora Cloud. You can listen for new data by using subscribe
method
For example, you can subscribe to power
property by creating a callback function outside setup()
.
cpp
#include <Arduino.h>
#include <EspWifi.h>
// declare properties outside setup() method
Property lightSwitch("power");
Property brightness("brightness");
void handlePowerChange(PropertyValue& val) {
if (val.Bool()) {
SENSORA_LOGI("Light is powered on");
} else {
SENSORA_LOGI("Light is powered off");
}
}
void setup() {
lightSwitch
.setDataType(DataType::Boolean)
.setAccessMode(AccessMode::Write)
.subscribe(handlePowerChange);
brightness.setDataType(DataType::Integer).setAccessMode(AccessMode::ReadWrite);
Sensora.setup();
}
void loop() {
// write your sensor logic here
Sensora.loop();
}