Skip to content
Snippets Groups Projects
Commit 6e9a4d92 authored by DV's avatar DV
Browse files

Initial commit

parents
Branches
Tags
No related merge requests found
.DS_Store
*.autosave
*.save
*~
*.tmp
*.part
This diff is collapsed.
/*
* NodeManager
*/
#ifndef NodeManager_h
#define NodeManager_h
#include <Arduino.h>
/***********************************
Sensors types
*/
// Generic analog sensor, return a pin's analog value or its percentage
#define SENSOR_ANALOG_INPUT 0
// LDR sensor, return the light level of an attached light resistor in percentage
#define SENSOR_LDR 1
// Thermistor sensor, return the temperature based on the attached thermistor
#define SENSOR_THERMISTOR 2
// Generic digital sensor, return a pin's digital value
#define SENSOR_DIGITAL_INPUT 3
// Generic digital output sensor, allows setting the digital output of a pin to the requested value
#define SENSOR_DIGITAL_OUTPUT 4
// Relay sensor, allows activating the relay
#define SENSOR_RELAY 5
// Latching Relay sensor, allows activating the relay with a pulse
#define SENSOR_LATCHING_RELAY 6
// DHT11/DHT22 sensors, return temperature/humidity based on the attached DHT sensor
#define SENSOR_DHT11 7
#define SENSOR_DHT22 8
// SHT21 sensor, return temperature/humidity based on the attached SHT21 sensor
#define SENSOR_SHT21 9
// Generic switch, wake up the board when a pin changes status
#define SENSOR_SWITCH 10
// Door sensor, wake up the board and report when an attached magnetic sensor has been opened/closed
#define SENSOR_DOOR 11
// Motion sensor, wake up the board and report when an attached PIR has triggered
#define SENSOR_MOTION 12
// DS18B20 sensor, return the temperature based on the attached sensor
#define SENSOR_DS18B20 13
/***********************************
Constants
*/
// define sleep mode
#define IDLE 0
#define SLEEP 1
#define WAIT 2
// define time unit
#define SECONDS 0
#define MINUTES 1
#define HOURS 2
#define DAYS 3
// define value type
#define TYPE_INTEGER 0
#define TYPE_FLOAT 1
#define TYPE_STRING 2
// define interrupt pins
#define INTERRUPT_PIN_1 3
#define INTERRUPT_PIN_2 2
// define eeprom addresses
#define EEPROM_LAST_ID 4
#define EEPROM_SLEEP_SAVED 0
#define EEPROM_SLEEP_MODE 1
#define EEPROM_SLEEP_TIME_MAJOR 2
#define EEPROM_SLEEP_TIME_MINOR 3
#define EEPROM_SLEEP_UNIT 4
// define NodeManager version
#define VERSION 1.0
/***********************************
Configuration settings
*/
// default configuration settings
// if enabled, will load the sleep manager library. Sleep mode and sleep interval have to be configured to make the board sleeping/waiting
#define SLEEP_MANAGER 1
// if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
#define POWER_MANAGER 1
// if enabled, will load the battery manager library to allow the battery level to be reported automatically or on demand
#define BATTERY_MANAGER 1
// if enabled, allow modifying the configuration remotely by interacting with the configuration child id
#define REMOTE_CONFIGURATION 1
// if enabled, persist the configuration settings on EEPROM
#define PERSIST 0
// if enabled, enable debug messages on serial port
#define DEBUG 1
// if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle
#define SERVICE_MESSAGES 1
// if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage
#define BATTERY_SENSOR 1
// the child id used to allow remote configuration
#define CONFIGURATION_CHILD_ID 200
// the child id used to report the battery voltage to the controller
#define BATTERY_CHILD_ID 201
// Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR
#define MODULE_ANALOG_INPUT 1
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_INPUT
#define MODULE_DIGITAL_INPUT 1
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_OUTPUT, SENSOR_RELAY, SENSOR_LATCHING_RELAY
#define MODULE_DIGITAL_OUTPUT 1
// Enable this module to use one of the following sensors: SENSOR_SHT21
#define MODULE_SHT21 0
// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
#define MODULE_DHT 0
// Enable this module to use one of the following sensors: SENSOR_SWITCH, SENSOR_DOOR, SENSOR_MOTION
#define MODULE_SWITCH 0
// Enable this module to use one of the following sensors: SENSOR_DS18B20
#define MODULE_DS18B20 0
// include user defined configuration difrectives
#include "config.h"
/***********************************
Libraries
*/
// include MySensors libraries
#include <core/MySensorsCore.h>
#include <core/MyHwAVR.h>
// include third party libraries
#if MODULE_DHT == 1
#include <DHT.h>
#endif
#if MODULE_SHT21 == 1
#include <Wire.h>
#include <Sodaq_SHT2x.h>
#endif
#if MODULE_SHT21 == 1
#include <Wire.h>
#include <Sodaq_SHT2x.h>
#endif
#if MODULE_DS18B20 == 1
#include <OneWire.h>
#include <DallasTemperature.h>
#endif
/**************************************
Classes
*/
/*
PowerManager
*/
class PowerManager {
public:
PowerManager() {};
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, long wait = 0);
void powerOn();
void powerOff();
private:
int _vcc_pin = -1;
int _ground_pin = -1;
long _wait = 0;
bool _hasPowerManager();
};
/***************************************
Sensor: generic sensor class
*/
class Sensor {
public:
Sensor(int child_id, int pin);
// where the sensor is attached to (default: not set)
void setPin(int value);
int getPin();
// child_id of this sensor (default: not set)
void setChildId(int value);
int getChildId();
// presentation of this sensor (default: S_CUSTOM)
void setPresentation(int value);
int getPresentation();
// type of this sensor (default: V_CUSTOM)
void setType(int value);
int getType();
// when queried, send the message multiple times (default: 1)
void setRetries(int value);
// For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
void setSamples(int value);
// If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
void setSamplesInterval(int value);
// if true will report the measure only if different then the previous one (default: false)
void setTackLastValue(bool value);
// if track last value is enabled, force to send an update after the configured number of cycles (default: -1)
void setForceUpdate(int value);
// the value type of this sensor (default: TYPE_INTEGER)
void setValueType(int value);
// for float values, set the float precision (default: 2)
void setFloatPrecision(int value);
#if POWER_MANAGER == 1
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, long wait = 0);
void powerOn();
void powerOff();
#endif
// define what to do at each stage of the sketch
virtual void before();
virtual void presentation();
virtual void loop(const MyMessage & message);
virtual void receive(const MyMessage & message);
// abstract functions, subclasses need to implement
virtual void onBefore() = 0;
virtual void onLoop() = 0;
virtual void onReceive(const MyMessage & message) = 0;
protected:
MyMessage _msg;
int _pin = -1;
int _child_id;
int _presentation = S_CUSTOM;
int _type = V_CUSTOM;
int _retries = 1;
int _samples = 1;
int _samples_interval = 0;
bool _track_last_value = false;
int _cycles = 0;
int _force_update = -1;
void _send(MyMessage & msg);
#if POWER_MANAGER == 1
PowerManager _powerManager;
#endif
int _value_type = TYPE_INTEGER;
int _float_precision = 2;
int _value_int = -1;
float _value_float = -1;
char * _value_string = "";
int _last_value_int = -1;
float _last_value_float = -1;
char * _last_value_string = "";
};
/*
SensorAnalogInput: read the analog input of a configured pin
*/
class SensorAnalogInput: public Sensor {
public:
SensorAnalogInput(int child_id, int pin);
// the analog reference to use (default: not set, can be either INTERNAL or DEFAULT)
void setReference(int value);
// reverse the value or the percentage (e.g. 70% -> 30%) (default: false)
void setReverse(bool value);
// when true returns the value as a percentage (default: true)
void setOutputPercentage(bool value);
// minimum value for calculating the percentage (default: 0)
void setRangeMin(int value);
// maximum value for calculating the percentage (default: 1024)
void setRangeMax(int value);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
protected:
int _reference = -1;
bool _reverse = false;
bool _output_percentage = true;
int _range_min = 0;
int _range_max = 1024;
int _getPercentage(int value);
int _getAnalogRead();
};
/*
SensorLDR: return the percentage of light from a Light dependent resistor
*/
class SensorLDR: public SensorAnalogInput {
public:
SensorLDR(int child_id, int pin);
};
/*
SensorThermistor: read the temperature from a thermistor
*/
class SensorThermistor: public Sensor {
public:
SensorThermistor(int child_id, int pin);
// resistance at 25 degrees C (default: 10000)
void setNominalResistor(int value);
// temperature for nominal resistance (default: 25)
void setNominalTemperature(int value);
// The beta coefficient of the thermistor (default: 3950)
void setBCoefficient(int value);
// the value of the resistor in series with the thermistor (default: 10000)
void setSeriesResistor(int value);
// set a temperature offset
void setOffset(float value);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
protected:
int _nominal_resistor = 10000;
int _nominal_temperature = 25;
int _b_coefficient = 3950;
int _series_resistor = 10000;
float _offset = 0;
};
/*
SensorDigitalInput: read the digital input of the configured pin
*/
class SensorDigitalInput: public Sensor {
public:
SensorDigitalInput(int child_id, int pin);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
};
/*
SensorDigitalOutput: control a digital output of the configured pin
*/
class SensorDigitalOutput: public Sensor {
public:
SensorDigitalOutput(int child_id, int pin);
// set how to initialize the output (default: LOW)
void setInitialValue(int value);
// if greater than 0, send a pulse of the given duration in ms and then restore the output back to the original value (default: 0)
void setPulseWidth(int value);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
protected:
int _initial_value = LOW;
int _pulse_width = 0;
};
/*
SensorRelay
*/
class SensorRelay: public SensorDigitalOutput {
public:
SensorRelay(int child_id, int pin);
};
/*
SensorLatchingRelay
*/
class SensorLatchingRelay: public SensorRelay {
public:
SensorLatchingRelay(int child_id, int pin);
};
/*
SensorDHT
*/
#if MODULE_DHT == 1
class SensorDHT: public Sensor {
public:
SensorDHT(int child_id, int pin, DHT* dht, int sensor_type, int dht_type);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
protected:
DHT* _dht;
int _dht_type = DHT11;
float _offset = 0;
int _sensor_type = 0;
};
#endif
/*
SensorSHT21
*/
#if MODULE_SHT21 == 1
class SensorSHT21: public Sensor {
public:
SensorSHT21(int child_id, int sensor_type);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
protected:
float _offset = 0;
int _sensor_type = 0;
};
#endif
/*
* SensorSwitch
*/
class SensorSwitch: public Sensor {
public:
SensorSwitch(int child_id, int pin);
// set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
void setMode(int value);
int getMode();
// milliseconds to wait before reading the input (default: 0)
void setDebounce(int value);
// time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
void setTriggerTime(int value);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
protected:
int _debounce = 0;
int _trigger_time = 0;
int _mode = CHANGE;
};
/*
* SensorDoor
*/
class SensorDoor: public SensorSwitch {
public:
SensorDoor(int child_id, int pin);
};
/*
* SensorMotion
*/
class SensorMotion: public SensorSwitch {
public:
SensorMotion(int child_id, int pin);
};
/*
SensorDs18b20
*/
#if MODULE_DS18B20 == 1
class SensorDs18b20: public Sensor {
public:
SensorDs18b20(int child_id, int pin, DallasTemperature* sensors, int index);
// define what to do at each stage of the sketch
void onBefore();
void onLoop();
void onReceive(const MyMessage & message);
protected:
float _offset = 0;
int _index;
DallasTemperature* _sensors;
};
#endif
/***************************************
NodeManager: manages all the aspects of the node
*/
class NodeManager {
public:
NodeManager();
// the pin to connect to the RST pin to reboot the board (default: 4)
void setRebootPin(int value);
#if BATTERY_MANAGER == 1
// the expected vcc when the batter is fully discharged, used to calculate the percentage (default: 2.7)
void setBatteryMin(float value);
// the expected vcc when the batter is fully charged, used to calculate the percentage (default: 3.3)
void setBatteryMax(float value);
// how frequently (in hours) to report the battery level to the controller. When reset the battery is always reported (default: 1)
void setBatteryReportCycles(int value);
#endif
#if SLEEP_MANAGER == 1
// define if the board has to sleep every time entering loop (default: IDLE). It can be IDLE (no sleep), SLEEP (sleep at every cycle), WAIT (wait at every cycle
void setSleepMode(int value);
// define for how long the board will sleep (default: 0)
void setSleepTime(int value);
// define the unit of SLEEP_TIME. It can be SECONDS, MINUTES, HOURS or DAYS (default: MINUTES)
void setSleep(int value1, int value2, int value3);
void setSleepUnit(int value);
// if enabled, when waking up from the interrupt, the board stops sleeping. Disable it when attaching e.g. a motion sensor (default: true)
void setSleepInterruptPin(int value);
#endif
// configure the interrupt pin and mode. Mode can be CHANGE, RISING, FALLING (default: MODE_NOT_DEFINED)
void setInterrupt(int pin, int mode, int pull = -1);
// register a built-in sensor
int registerSensor(int sensor_type, int pin = -1, int child_id = -1);
// register a custom sensor
int registerSensor(Sensor* sensor);
// return a sensor by its index
Sensor* get(int sensor_index);
#if POWER_MANAGER == 1
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, long wait = 10);
void powerOn();
void powerOff();
#endif
// hook into the main sketch functions
void before();
void presentation();
void loop();
void receive(const MyMessage & msg);
private:
#if SLEEP_MANAGER == 1
int _sleep_mode = IDLE;
int _sleep_time = 0;
int _sleep_unit = MINUTES;
int _sleep_interrupt_pin = -1;
#endif
#if BATTERY_MANAGER == 1
float _battery_min = 2.6;
float _battery_max = 3.3;
int _battery_report_cycles = 10;
int _cycles = 0;
float _getVcc();
#endif
#if POWER_MANAGER == 1
// to optionally controller power pins
PowerManager _powerManager;
#endif
MyMessage _msg;
int _interrupt_1_mode = MODE_NOT_DEFINED;
int _interrupt_2_mode = MODE_NOT_DEFINED;
int _interrupt_1_pull = -1;
int _interrupt_2_pull = -1;
int _reboot_pin = -1;
Sensor* _sensors[255] = {0};
void _process(const char * message);
void _sleep();
int _getAvailableChildId();
int _getInterruptInitialValue(int mode);
bool _startup = true;
};
#endif
/*
NodeManager is intended to take care on your behalf of all those common tasks a MySensors node has to accomplish, speeding up the development cycle of your projects.
NodeManager includes the following main components:
- Sleep manager: allows managing automatically the complexity behind battery-powered sensors spending most of their time sleeping
- Power manager: allows powering on your sensors only while the node is awake
- Battery manager: provides common functionalities to read and report the battery level
- Remote configuration: allows configuring remotely the node without the need to have physical access to it
- Built-in personalities: for the most common sensors, provide embedded code so to allow their configuration with a single line
Documentation available on: https://mynodemanager.sourceforge.io
*/
// load user settings
#include "config.h"
// load MySensors library
#include <MySensors.h>
// load NodeManager library
#include "NodeManager.h"
// create a NodeManager instance
NodeManager nodeManager;
// before
void before() {
// setup the serial port baud rate
Serial.begin(9600);
// connect pin 4 to RST to enable rebooting the board with a message
nodeManager.setRebootPin(4);
// set battery minimum voltage. This will be used to calculate the level percentage
//nodeManager.setBatteryMin(1.8);
// instruct the board to sleep for 10 minutes for each cycle
//nodeManager.setSleep(SLEEP,10,MINUTES);
// When pin 3 is connected to ground, the board will stop sleeping
//nodeManager.setSleepInterruptPin(3)
// all the sensors' vcc and ground are connected to pin 6 (vcc) and 7 (ground). NodeManager will enable the vcc pin every time just before loop() and wait for 100ms for the sensors to settle
//nodeManager.setPowerPins(6,7,100);
// register a thermistor sensor attached to pin A2
//nodeManager.registerSensor(SENSOR_THERMISTOR,A2);
// register a LDR sensor attached to pin A1 and average 3 samples
//int sensor_ldr = nodeManager.registerSensor(SENSOR_LDR,A1);
//((SensorLDR*)nodeManager.get(sensor_ldr))->setSamples(3);
nodeManager.before();
}
// presentation
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("NodeManager", "1.0");
// call NodeManager presentation routine
nodeManager.presentation();
}
// setup
void setup() {
}
// loop
void loop() {
// call NodeManager loop routine
nodeManager.loop();
}
// receive
void receive(const MyMessage &message) {
// call NodeManager receive routine
nodeManager.receive(message);
}
NodeManager
# Introduction
MySensors (<https://www.mysensors.org>) is an open source hardware and software community focusing on do-it-yourself home automation and Internet of Things which allows creating original and affordable sensors.
NodeManager is intended to take care on your behalf of all those common tasks a MySensors node has to accomplish, speeding up the development cycle of your projects.
NodeManager includes the following main components:
* Sleep manager: allows managing automatically the complexity behind battery-powered sensors spending most of their time sleeping
* Power manager: allows powering on your sensors only while the node is awake
* Battery manager: provides common functionalities to read and report the battery level
* Remote configuration: allows configuring remotely the node without the need to have physical access to it
* Built-in sensors: for the most common sensors, provide embedded code so to allow their configuration with a single line
## Features
* Manage all the aspects of a sleeping cycle by leveraging smart sleep
* Allow configuring the sleep mode and the sleep duration remotely
* Allow waking up a sleeping node remotely at the end of a sleeping cycle
* Allow powering on each connected sensor only while the node is awake to save battery
* Report battery level periodically and automatically
* Calculate battery level without requiring an additional pin and the resistors
* Report battery voltage through a built-in sensor
* Can report battery level on demand
* Allow rebooting the board remotely
* Provide out-of-the-box sensors personalities and automatically execute their main task at each cycle
# Installation
* Download the package from https://mynodemanager.sourceforge.io
* Open the provided sketch template and save it under a different name
* Open `config.h` and customize both MySensors configuration and NodeManager global settings
* Register your sensors in the sketch file
* Upload the sketch to your arduino board
Please note NodeManager cannot be used as an arduino library since requires access to your MySensors configuration directives, hence its files have to be placed into the same directory of your sketch.
# Configuration
NodeManager configuration includes compile-time configuration directives (which can be set in config.h), runtime global and per-sensor configuration settings (which can be set in your sketch) and settings that can be customized remotely (via a special child id).
## Setup MySensors
Since NodeManager has to communicate with the MySensors gateway on your behalf, it has to know how to do it. Place on top of the `config.h` file all the MySensors typical directives you are used to set on top of your sketch so both your sketch AND NodeManager will be able to share the same configuration.
## Enable/Disable NodeManager's modules
Those NodeManager's directives in the `config.h` file control which module/library/functionality will be made available to your sketch. Enable (e.g. set to 1) only what you need to ensure enough space is left to your custom code.
~~~c
// if enabled, will load the sleep manager library. Sleep mode and sleep interval have to be configured to make the board sleeping/waiting
#define SLEEP_MANAGER 1
// if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
#define POWER_MANAGER 1
// if enabled, will load the battery manager library to allow the battery level to be reported automatically or on demand
#define BATTERY_MANAGER 1
// if enabled, allow modifying the configuration remotely by interacting with the configuration child id
#define REMOTE_CONFIGURATION 1
// if enabled, persist the configuration settings on EEPROM
#define PERSIST 0
// if enabled, enable debug messages on serial port
#define DEBUG 1
// if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle
#define SERVICE_MESSAGES 1
// if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage
#define BATTERY_SENSOR 1
// Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR
#define MODULE_ANALOG_INPUT 1
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_INPUT
#define MODULE_DIGITAL_INPUT 1
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_OUTPUT, SENSOR_RELAY, SENSOR_LATCHING_RELAY
#define MODULE_DIGITAL_OUTPUT 1
// Enable this module to use one of the following sensors: SENSOR_SHT21
#define MODULE_SHT21 0
// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
#define MODULE_DHT 0
// Enable this module to use one of the following sensors: SENSOR_SWITCH, SENSOR_DOOR, SENSOR_MOTION
#define MODULE_SWITCH 0
// Enable this module to use one of the following sensors: SENSOR_DS18B20
#define MODULE_DS18B20 0
~~~
## Configure NodeManager
Node Manager comes with a reasonable default configuration. If you want/need to change its settings, this can be done in your sketch, inside the `before()` function and just before registering your sensors. The following methods are exposed for your convenience:
~~~c
// the pin to connect to the RST pin to reboot the board (default: 4)
void setRebootPin(int value);
#if BATTERY_MANAGER == 1
// the expected vcc when the batter is fully discharged, used to calculate the percentage (default: 2.7)
void setBatteryMin(float value);
// the expected vcc when the batter is fully charged, used to calculate the percentage (default: 3.3)
void setBatteryMax(float value);
// how frequently (in hours) to report the battery level to the controller. When reset the battery is always reported (default: 1)
void setBatteryReportCycles(int value);
#endif
#if SLEEP_MANAGER == 1
// define if the board has to sleep every time entering loop (default: IDLE). It can be IDLE (no sleep), SLEEP (sleep at every cycle), WAIT (wait at every cycle
void setSleepMode(int value);
// define for how long the board will sleep (default: 0)
void setSleepTime(int value);
// define the unit of SLEEP_TIME. It can be SECONDS, MINUTES, HOURS or DAYS (default: MINUTES)
void setSleep(int value1, int value2, int value3);
void setSleepUnit(int value);
// if enabled, when waking up from the interrupt, the board stops sleeping. Disable it when attaching e.g. a motion sensor (default: true)
void setSleepInterruptPin(int value);
#endif
// configure the interrupt pin and mode. Mode can be CHANGE, RISING, FALLING (default: MODE_NOT_DEFINED)
void setInterrupt(int pin, int mode, int pull = -1);
// register a built-in sensor
int registerSensor(int sensor_type, int pin = -1, int child_id = -1);
// register a custom sensor
int registerSensor(Sensor* sensor);
// return a sensor by its index
Sensor* get(int sensor_index);
#if POWER_MANAGER == 1
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, long wait = 10);
void powerOn();
void powerOff();
#endif
~~~
For example
~~~c
nodeManager.setBatteryMin(1.8);
~~~
## Register your sensors
In your sketch, inside the `before()` function and just before calling `nodeManager.before()`, you can register your sensors against NodeManager. The following built-in sensor types are available:
Sensor type | Description
------------- | -------------
SENSOR_ANALOG_INPUT | Generic analog sensor, return a pin's analog value or its percentage
SENSOR_LDR | LDR sensor, return the light level of an attached light resistor in percentage
SENSOR_THERMISTOR | Thermistor sensor, return the temperature based on the attached thermistor
SENSOR_DIGITAL_INPUT | Generic digital sensor, return a pin's digital value
SENSOR_DIGITAL_OUTPUT | Generic digital output sensor, allows setting the digital output of a pin to the requested value
SENSOR_RELAY | Relay sensor, allows activating the relay
SENSOR_LATCHING_RELAY| Latching Relay sensor, allows activating the relay with a pulse
SENSOR_DHT11 | DHT11 sensor, return temperature/humidity based on the attached DHT sensor
SENSOR_DHT22 | DHT22 sensor, return temperature/humidity based on the attached DHT sensor
SENSOR_SHT21 | SHT21 sensor, return temperature/humidity based on the attached SHT21 sensor
SENSOR_SWITCH | Generic switch, wake up the board when a pin changes status
SENSOR_DOOR | Door sensor, wake up the board and report when an attached magnetic sensor has been opened/closed
SENSOR_MOTION | Motion sensor, wake up the board and report when an attached PIR has triggered
SENSOR_DS18B20 | DS18B20 sensor, return the temperature based on the attached sensor
To register a sensor simply call the NodeManager instance with the sensory type and the pin the sensor is conncted to. For example:
~~~c
nodeManager.registerSensor(SENSOR_THERMISTOR,A2);
nodeManager.registerSensor(SENSOR_DOOR,3);
~~~
Once registered, your job is done. NodeManager will assign a child id automatically, present each sensor for you to the controller, query each sensor and report the value back to the gateway/controller at at the end of each sleep cycle . For actuators (e.g. relays) those can be triggered by sending a `REQ` message to their assigned child id.
When called, registerSensor returns the child_id of the sensor so you will be able to retrieve it later if needed. If you want to set a child_id manually, this can be passed as third argument to the function.
### Creating a custom sensor
If you want to create a custom sensor and register it with NodeManager so it can take care of all the common tasks, you can create a class inheriting from `Sensor` and implement the following methods:
~~~c
// define what to do during before() to setup the sensor
void onBefore();
// define what to do during loop() by executing the sensor's main task
void onLoop();
// define what to do during receive() when the sensor receives a message
void onReceive(const MyMessage & message);
~~~
You can then instantiate your newly created class and register with NodeManager:
~~~c
nodeManager.registerSensor(new SensorCustom(child_id, pin));
~~~
## Configuring the sensors
Each built-in sensor class comes with reasonable default settings. In case you want/need to customize any of those settings, after having registered the sensor, you can retrieve it back and call set functions common to all the sensors or specific for a given class.
To do so, use `nodeManager.get(child_id)` which will return a pointer to the sensor. Remeber to cast it to the right class before calling their functions. For example:
~~~c
((SensorLatchingRelay*)nodeManager.get(2))->setPulseWidth(50);
~~~
### Sensor's general configuration
The following methods are available for all the sensors:
~~~c
// where the sensor is attached to (default: not set)
void setPin(int value);
// child_id of this sensor (default: not set)
void setChildId(int value);
// presentation of this sensor (default: S_CUSTOM)
void setPresentation(int value);
// type of this sensor (default: V_CUSTOM)
void setType(int value);
// when queried, send the message multiple times (default: 1)
void setRetries(int value);
// For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
void setSamples(int value);
// If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
void setSamplesInterval(int value);
// if true will report the measure only if different then the previous one (default: false)
void setTackLastValue(bool value);
// if track last value is enabled, force to send an update after the configured number of cycles (default: -1)
void setForceUpdate(int value);
// the value type of this sensor (default: TYPE_INTEGER)
void setValueType(int value);
// for float values, set the float precision (default: 2)
void setFloatPrecision(int value);
#if POWER_MANAGER == 1
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, long wait = 0);
void powerOn();
void powerOff();
#endif
~~~
### Sensor's specific configuration
Each sensor class can expose additional methods.
#### SensorAnalogInput / SensorLDR
~~~c
// the analog reference to use (default: not set, can be either INTERNAL or DEFAULT)
void setReference(int value);
// reverse the value or the percentage (e.g. 70% -> 30%) (default: false)
void setReverse(bool value);
// when true returns the value as a percentage (default: true)
void setOutputPercentage(bool value);
// minimum value for calculating the percentage (default: 0)
void setRangeMin(int value);
// maximum value for calculating the percentage (default: 1024)
void setRangeMax(int value);
~~~
#### SensorThermistor
~~~c
// resistance at 25 degrees C (default: 10000)
void setNominalResistor(int value);
// temperature for nominal resistance (default: 25)
void setNominalTemperature(int value);
// The beta coefficient of the thermistor (default: 3950)
void setBCoefficient(int value);
// the value of the resistor in series with the thermistor (default: 10000)
void setSeriesResistor(int value);
// set a temperature offset
void setOffset(float value);
~~~
#### SensorDigitalOutput / SensorRelay / SensorLatchingRelay
~~~c
// set how to initialize the output (default: LOW)
void setInitialValue(int value);
// if greater than 0, send a pulse of the given duration in ms and then restore the output back to the original value (default: 0)
void setPulseWidth(int value);
~~~
#### SensorSwitch / SensorDoor / SensorMotion
~~~c
// set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
void setMode(int value);
// milliseconds to wait before reading the input (default: 0)
void setDebounce(int value);
// time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
void setTriggerTime(int value);
~~~
## Upload your sketch
Upload your sketch to your arduino board as you are used to.
## Verify if everything works fine
Check your gateway's logs to ensure the node is working as expected. You should see the node presenting itself, reporting battery level, presenting all the registered sensors and the configuration child id service.
When `DEBUG` is enabled, detailed information is available through the serial port. Remember to disable debug once the tests have been completed.
## Communicate with each sensor
You can interact with each registered sensor asking to execute their main tasks by sending to the child id a `REQ` command. For example to request the temperature to node_id 254 and child_id 1:
`254;1;2;0;0;`
To activate a relay connected to the same node, child_id 100:
`254;100;2;0;2;1`
No need to implement anything on your side since for built-in sensor types this is handled automatically.
Once the node will be sleeping, it will report automatically each measure at the end of every sleep cycle.
## Communicate with the node
NodeManager exposes a configuration service by default on child_id 200 so you can interact with it by sending `V_CUSTOM` type of messages and commands within the payload. For each `REQ` message, the node will respond with a `SET` message.
The following custom commands are available:
NodeManager command | Description
------------- | -------------
BATTERY | Report the battery level back to the gateway/controller
HELLO | Hello request
REBOOT | Reboot the board
CLEAR | Wipe from the EEPROM NodeManager's settings
VERSION | Respond with NodeManager's version
IDxxx | Change the node id to the provided one. E.g. ID025: change the node id to 25. Requires a reboot to take effect
INTVLnnnX | Set the wait/sleep interval to nnn where X is S=Seconds, M=mins, H=Hours, D=Days. E.g. INTVL010M would be 10 minutes
MODEx | Change the way the board behaves (e.g. MODE1). 0: stay awake, 1: go to sleep for the configured interval, 2: wait for the configured interval
AWAKE | When received after a sleeping cycle or during wait, abort the cycle and stay awake
For example, to request the battery level to node id 254:
`254;200;2;0;48;BATTERY`
To set the sleeping cycle to 1 hour:
`254;200;2;0;48;INTVL001H`
To ask the node to start sleeping (and waking up based on the previously configured interval):
`254;200;2;0;48;MODE1`
To wake up a node previously configured with `MODE1`, send the following just after reporting `AWAKE`:
`254;200;2;0;48;WAKEUP`
In addition, NodeManager will report with custom messages every time the board is going to sleep (`SLEEPING`) or it is awake (`AWAKE`).
If `PERSIST` is enabled, the settings provided with `INTVLnnnX` and `MODEx` are saved to the EEPROM to be persistent even after rebooting the board.
# How it works
A NodeManager object must be created and called from within your sketch during `before()`, `presentation()`, `loop()` and `receive()` to work properly. NodeManager will do the following during each phase:
## NodeManager::before()
* Configure the reboot pin so to allow rebooting the board
* Setup the interrupt pins to wake up the board based on the configured interrupts (e.g. stop sleeping when the pin is connected to ground or wake up and notify when a motion sensor has trigger)
* If persistance is enabled, restore from the EEPROM the latest sleeping settings
* Call `before()` of each registered sensor
### Sensor::before()
* Call sensor-specific implementation of before by invoking `onBefore()` to initialize the sensor
## NodeManager::loop()
* If all the sensors are powered by an arduino pin, this is set to HIGH
* Call `loop()` of each registered sensor
* If all the sensors are powered by an arduino pin, this is set to LOW
### Sensor::loop()
* If the sensor is powered by an arduino pin, this is set to HIGH
* For each registered sensor, the sensor-specific `onLoop()` is called. If multiple samples are requested, this is run multiple times.
* In case multiple samples have been collected, the average is calculated
* A message is sent to the gateway with the calculated value. Depending on the configuration, this is not sent if it is the same as the previous value or sent anyway after a given number of cycles. These functionalies are not sensor-specific and common to all the sensors inheriting from the `Sensor` class.
* If the sensor is powered by an arduino pin, this is set to LOW
## NodeManager::receive()
* Receive a message from the radio network
* If the destination child id is the configuration node, it will handle the incoming message, otherwise will dispatch the message to the recipient sensor
### Sensor::receive()
* Invoke `Sensor::loop()` which will execute the sensor main taks and eventually call `Sensor::onReceive()`
\ No newline at end of file
#ifndef config_h
#define config_h
/**********************************
* MySensors configuration
*/
//#define MY_DEBUG
#define MY_RADIO_NRF24
/***********************************
* NodeManager configuration
*/
// if enabled, will load the sleep manager library. Sleep mode and sleep interval have to be configured to make the board sleeping/waiting
#define SLEEP_MANAGER 1
// if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
#define POWER_MANAGER 1
// if enabled, will load the battery manager library to allow the battery level to be reported automatically or on demand
#define BATTERY_MANAGER 1
// if enabled, allow modifying the configuration remotely by interacting with the configuration child id
#define REMOTE_CONFIGURATION 1
// if enabled, persist the configuration settings on EEPROM
#define PERSIST 0
// if enabled, enable debug messages on serial port
#define DEBUG 1
// if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle
#define SERVICE_MESSAGES 1
// if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage
#define BATTERY_SENSOR 1
// Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR
#define MODULE_ANALOG_INPUT 1
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_INPUT
#define MODULE_DIGITAL_INPUT 1
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_OUTPUT, SENSOR_RELAY, SENSOR_LATCHING_RELAY
#define MODULE_DIGITAL_OUTPUT 1
// Enable this module to use one of the following sensors: SENSOR_SHT21
#define MODULE_SHT21 0
// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
#define MODULE_DHT 0
// Enable this module to use one of the following sensors: SENSOR_SWITCH, SENSOR_DOOR, SENSOR_MOTION
#define MODULE_SWITCH 0
// Enable this module to use one of the following sensors: SENSOR_DS18B20
#define MODULE_DS18B20 0
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment