Register a rain sensor connected to A0. This will be powered with via pins 4 (ground) and 5 (vcc) just before reading its value at each cycle, it will be presented as S_RAIN. sending V_RAINRATE messages, the output will be a percentage (calculated between 200 and 2014) and the value will be reversed (so that no rain will be 0%):
Register a rain sensor connected to A0. This will be powered with via pins 4 (ground) and 5 (vcc) just before reading its value at each cycle, it will be presented as S_RAIN. sending V_RAINRATE messages, the output will be a percentage (calculated between 200 and 1024) and the value will be reversed (so that no rain will be 0%):
The following sketch can be used to report the temperature and the light level based on a thermistor and LDR sensors attached to two analog pins of the arduino board (A1 and A2). Both the thermistor and the LDR are connected to ground on one side and to vcc via a resistor on the other so to measure the voltage drop across each of them through the analog pins. The sensor will be put to sleep after startup and will report both the measures every 10 minutes. NodeManager will take care of presenting the sensors, managing the sleep cycle, reporting the battery level every 10 cycles and report the measures in the appropriate format. This sketch requires MODULE_ANALOG_INPUT enabled in the global config.h file.
Even if the sensor is sleeping most of the time, it can be potentially woke up by sending a V_CUSTOM message with a WAKEUP payload to NodeManager service child id (200 by default) just after having reported its heartbeat. At this point the node will report AWAKE and the user can interact with it by e.g. sending REQ messages to its child IDs, changing the duration of a sleep cycle with a V_CUSTOM message to the NodeManager service child id, etc.
~~~c
/*
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
NodeManagernodeManager;
// before
voidbefore(){
// setup the serial port baud rate
Serial.begin(MY_BAUD_RATE);
/*
* Register below your sensors
*/
nodeManager.setSleep(SLEEP,10,MINUTES);
nodeManager.registerSensor(SENSOR_THERMISTOR,A1);
nodeManager.registerSensor(SENSOR_LDR,A2);
/*
* Register above your sensors
*/
nodeManager.before();
}
// presentation
voidpresentation(){
// Send the sketch version information to the gateway and Controller
sendSketchInfo(SKETCH_NAME,SKETCH_VERSION);
// call NodeManager presentation routine
nodeManager.presentation();
}
// setup
voidsetup(){
// call NodeManager setup routine
nodeManager.setup();
}
// loop
voidloop(){
// call NodeManager loop routine
nodeManager.loop();
}
// receive
voidreceive(constMyMessage&message){
// call NodeManager receive routine
nodeManager.receive(message);
}
~~~
## Motion Sensor
The following sketch can be used to report back to the controller when a motion sensor attached to the board's pin 3 triggers. In this example, the board will be put to sleep just after startup and will report a heartbeat every hour. NodeManager will take care of configuring an interrupt associated to the provided pin so automatically wake up when a motion is detected and report a V_TRIPPED message back. This sketch requires MODULE_SWITCH to be enabled in the global config.h file.
~~~c
/*
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
NodeManagernodeManager;
// before
voidbefore(){
// setup the serial port baud rate
Serial.begin(MY_BAUD_RATE);
/*
* Register below your sensors
*/
nodeManager.setSleep(SLEEP,60,MINUTES);
nodeManager.registerSensor(SENSOR_MOTION,3);
/*
* Register above your sensors
*/
nodeManager.before();
}
// presentation
voidpresentation(){
// Send the sketch version information to the gateway and Controller
sendSketchInfo(SKETCH_NAME,SKETCH_VERSION);
// call NodeManager presentation routine
nodeManager.presentation();
}
// setup
voidsetup(){
// call NodeManager setup routine
nodeManager.setup();
}
// loop
voidloop(){
// call NodeManager loop routine
nodeManager.loop();
}
// receive
voidreceive(constMyMessage&message){
// call NodeManager receive routine
nodeManager.receive(message);
}
~~~
## Boiler Sensor
The following sketch controls a latching relay connected to a boiler. A latching relay (requiring only a pulse to switch) has been chosen to minimize the power consumption required by a traditional relay to stay on. This relay has normally two pins, one for closing and the other for opening the controlled circuit, connected to pin 6 and 7 of the arduino board. This is why we have to register two sensors against NodeManager so to control the two funtions indipendently. Since using a SENSOR_LATCHING_RELAY type of sensor, NodeManager will take care of just sending out a single pulse only when a REQ command of type V_STATUS is sent to one or the other child id.
In this example, the board also runs at 1Mhz so it can go down to 1.8V: by setting setBatteryMin() and setBatteryMax(), the battery percentage will be calculated and reported (by default, automatically every 10 sleeping cycles) based on these custom boundaries.
The board will be put to sleep just after startup and will report back to the controller every 5 minutes. It is the controller's responsability to catch when the board reports its heartbeat (using smart sleep behind the scene) and send a command back if needed. This sketch requires MODULE_DIGITAL_OUTPUT to be enabled in the config.h file.
~~~c
/*
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
// Send the sketch version information to the gateway and Controller
sendSketchInfo(SKETCH_NAME,SKETCH_VERSION);
// call NodeManager presentation routine
nodeManager.presentation();
}
// setup
voidsetup(){
// call NodeManager setup routine
nodeManager.setup();
}
// loop
voidloop(){
// call NodeManager loop routine
nodeManager.loop();
}
// receive
voidreceive(constMyMessage&message){
// call NodeManager receive routine
nodeManager.receive(message);
}
~~~
## Rain and Soil Moisture Sensor
The following sketch can be used to report the rain level and the soil moisture based on two sensors connected to the board's analog pins (A1 and A2). In this case we are customizing the out-of-the-box SENSOR_ANALOG_INPUT sensor type since we just need to measure an analog input but we also want to provide the correct type and presentation for each sensor.
We register the sensors first with registerSensor() which returns the child id assigned to the sensor. We then retrieve the sensor's reference by calling get() so we can invoke the sensor-specific functions, like setPresentation() and setType().
In this example, the two sensors are not directly connected to the battery's ground and vcc but, to save additional power, are powered through two arduino's pins. By using e.g. setPowerPins(4,5,300), NodeManger will assume pin 4 is ground and pin 5 is vcc for that specific sensor so it will turn on the power just before reading the analog input (and waiting 300ms for the sensor to initialize) and back off before going to sleep.
For both the sensors we want a percentage output and with setRangeMin() and setRangeMax() we define the boundaries for calculating the percentage (if we read e.g. 200 when the rain sensor is completely into the water, we know for sure it will not go below this value which will represent the new lower boundary).
Finally, since both the sensors reports low when wet and high when dry but we need the opposite, we set setReverse() so to have 0% reported when there is no rain/moisture, 100% on the opposite situation.
~~~c
/*
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