Hey Kcest
Nice work!
Can you help me if I would like two PIR sensors in the same sketches.
thank you in advance, Tom
Hey Kcest
Nice work!
Can you help me if I would like two PIR sensors in the same sketches.
thank you in advance, Tom
Hey Hek
Thank you for your good advice. I have a little vacation here in Easter. so I got it to work with 2 door sensors maybe it's not the best looking program . but it works .
Where can I learn more about these lines, I do not understand 100 % yet :
Bounce debouncer1 = Bounce();
debouncer1.attach(BUTTON_PIN1);
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
Thanks for the help .
Tom Rask
// Sketch control 4 physical relays and 2 Doorsensors.
// This example will remember relay state even after power failure.
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h> // button/reed switch program
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_ON 0 // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay
#define CHILD_ID1 5 // Arduino Digital I/O pin for button/reed switch
#define CHILD_ID2 6 // Arduino Digital I/O pin for button/reed switch
#define BUTTON_PIN1 7 // Arduino Digital I/O pin for button/reed switch
#define BUTTON_PIN2 8 // Arduino Digital I/O pin for button/reed switch
MySensor gw;
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
int oldValue1=-1;
int oldValue2=-1;
// Change to V_LIGHT if you use S_LIGHT in presentation below
MyMessage msg1(CHILD_ID1,V_TRIPPED); // button/reed switch program
MyMessage msg2(CHILD_ID2,V_TRIPPED);
void setup()
{
// Initialize library and add callback for incoming messages
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Relay and Door", "1.0");
// Fetch relay status
// Register all sensors to gw (they will be created as child devices)
gw.present(1, S_LIGHT);
gw.present(2, S_LIGHT);
gw.present(3, S_LIGHT);
gw.present(4, S_LIGHT);
// Then set relay pins in output mode
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
// Setup the button
pinMode(BUTTON_PIN1,INPUT);// button/reed switch program
pinMode(BUTTON_PIN2,INPUT);// button/reed switch program
// Set relay to last known state (using eeprom storage)
digitalWrite(3, gw.loadState(1)?RELAY_ON:RELAY_OFF);
digitalWrite(4, gw.loadState(1)?RELAY_ON:RELAY_OFF);
digitalWrite(5, gw.loadState(1)?RELAY_ON:RELAY_OFF);
digitalWrite(6, gw.loadState(1)?RELAY_ON:RELAY_OFF);
// Activate internal pull-up
digitalWrite(BUTTON_PIN1,HIGH);// button/reed switch program
digitalWrite(BUTTON_PIN2,HIGH);// button/reed switch program
// After setting up the button, setup debouncer
debouncer1.attach(BUTTON_PIN1);// button/reed switch program
debouncer2.attach(BUTTON_PIN2);// button/reed switch program
debouncer1.interval(5);// button/reed switch program
debouncer2.interval(5);// button/reed switch program
// Register binary input sensor to gw (they will be created as child devices)
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
gw.present(CHILD_ID1, S_DOOR); // button/reed switch program
gw.present(CHILD_ID2, S_DOOR); // button/reed switch program
}
void loop()
{
// Alway process incoming messages whenever possible
gw.process();
debouncer1.update();
// Get the update value
int value1 = debouncer1.read();
if (value1 != oldValue1) {
// Send in the new value
gw.send(msg1.set(value1==HIGH ? 1 : 0));
oldValue1 = value1;
}
debouncer2.update();
// Get the update value
int value2 = debouncer2.read();
if (value2 != oldValue2) {
// Send in the new value
gw.send(msg2.set(value2==HIGH ? 1 : 0));
oldValue2 = value2;
}
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
Hey Vladut
This is the seriel output on my Nano
repeater started, id 1
send: 1-1-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:M
send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Relay&Botton
send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
send: 1-1-0-0 s=0,c=0,t=3,pt=0,l=5,st=ok:1.4.1
The nr 0 relay has been presented
send: 1-1-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1
The nr 1 relay has been presented
send: 1-1-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1
The nr 2 switch has been presented
send: 1-1-0-0 s=3,c=0,t=0,pt=0,l=5,st=ok:1.4.1
The nr 3 switch has been presented
send: 1-1-0-0 s=4,c=0,t=0,pt=0,l=5,st=ok:1.4.1
The nr 4 switch has been presented
send: 1-1-0-0 s=4,c=1,t=0,pt=2,l=2,st=ok:0
send: 1-1-0-0 s=3,c=1,t=0,pt=2,l=2,st=ok:0
send: 1-1-0-0 s=4,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=2,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
read: 1-0-1 s=4,c=1,t=0,pt=2,l=2:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
read: 1-0-1 s=4,c=1,t=0,pt=2,l=2:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=fail:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
read: 1-0-1 s=3,c=1,t=0,pt=2,l=2:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=fail:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=ok:1
read: 1-0-1 s=4,c=1,t=0,pt=2,l=2:1
send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
This is what I get from serial output
repeater started, id 1
send: 1-1-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:M
send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Relay&Botton
send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
send: 1-1-0-0 s=0,c=0,t=0,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=1,c=0,t=0,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1
Is there anyone out there who just want to give me a hand by getting this skeleton to work. I will be very happy just a little help. It could be great.
Hey Ferpando
thanks, now the sensor is displayed on vera. but only one contact and a door sensor works .
I have not quite mastered the code yet , help wanted.
// 3 Door 1 virker 2 relays 1 virker
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define NUMBER_OF_SWITCHES 3
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 2 // Total number of attached relays
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
MySensor gw;
Bounce debouncer[NUMBER_OF_SWITCHES];
int oldValue[NUMBER_OF_SWITCHES];
byte switchPin[NUMBER_OF_SWITCHES] = {5,6,7}; //<<<<<<<<<<< set your switch pins here
MyMessage msg(0,V_TRIPPED);
void setup()
{
// Initialize library and add callback for incoming messages
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Relay&Botton", "1.0");
// Fetch relay status
for (int sensor=0, pin=RELAY_1; sensor<NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
}
// Switches setup
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
pinMode(switchPin[i],INPUT_PULLUP);
debouncer[i] = Bounce();
debouncer[i].attach(switchPin[i]);
debouncer[i].interval(5);
}
for (int i = NUMBER_OF_RELAYS; i < (NUMBER_OF_RELAYS+NUMBER_OF_SWITCHES); i++)
{
gw.present(i, S_DOOR);
delay(250);
}
}
//
void loop()
{
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
debouncer[i].update();
int value = debouncer[i].read();
if (value != oldValue[i])
{
gw.send(msg.setSensor(i).set(value == HIGH? true : false), false);
}
oldValue[i] = value;
}
// Alway process incoming messages whenever possible
gw.process();
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
Here
Hey Hek I changed the code, but it will not help. Now I get 3 door sensor and nothing else. hope you will help a little so that I understand the code. thank you
// 1 Door virker 2 relays virker
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define NUMBER_OF_SWITCHES 3
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 2 // Total number of attached relays
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
MySensor gw;
Bounce debouncer[NUMBER_OF_SWITCHES];
int oldValue[NUMBER_OF_SWITCHES];
byte switchPin[NUMBER_OF_SWITCHES] = {5,6,7}; //<<<<<<<<<<< set your switch pins here
MyMessage msg(0,V_TRIPPED);
void setup()
{
// Initialize library and add callback for incoming messages
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Relay&Botton", "1.0");
// Switches setup
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
pinMode(switchPin[i],INPUT_PULLUP);
debouncer[i] = Bounce();
debouncer[i].attach(switchPin[i]);
debouncer[i].interval(5);
}
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
gw.present(i, S_DOOR);
delay(250);
}
// Fetch relay status
for (int sensor=3, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
//
void loop()
{
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
debouncer[i].update();
int value = debouncer[i].read();
if (value != oldValue[i])
{
gw.send(msg.setSensor(i).set(value == HIGH? true : false), false);
}
oldValue[i] = value;
}
// Alway process incoming messages whenever possible
gw.process();
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
Hey Hek
Thanks for a really exciting home page , I really got interested in building a sensor myself just need some start help.
I've tried to build together a relay and door sensor program from Hek one cloned program without success. I have a vera light. but I'm new about building sensors. Are there some who will look at the program and see where errors.
What I see in vera is only one door sensors and 2 relay, where there should have been 3 door sensor. I use a Nano with series connection to vera.
Hope some one can help me, last 3 weeks I have tried unsuccessfully various changes.
// 1 Door virker 2 relays virker
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define NUMBER_OF_SWITCHES 3
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 2 // Total number of attached relays
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
MySensor gw;
Bounce debouncer[NUMBER_OF_SWITCHES];
int oldValue[NUMBER_OF_SWITCHES];
byte switchPin[NUMBER_OF_SWITCHES] = {5,6,7}; //<<<<<<<<<<< set your switch pins here
MyMessage msg(0,V_TRIPPED);
void setup()
{
// Initialize library and add callback for incoming messages
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Relay&Botton", "1.0");
// Fetch relay status
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
}
// Switches setup
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
pinMode(switchPin[i],INPUT_PULLUP);
debouncer[i] = Bounce();
debouncer[i].attach(switchPin[i]);
debouncer[i].interval(5);
}
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
gw.present(i, S_DOOR);
delay(250);
}
}
//
void loop()
{
for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
{
debouncer[i].update();
int value = debouncer[i].read();
if (value != oldValue[i])
{
gw.send(msg.setSensor(i).set(value == HIGH? true : false), false);
}
oldValue[i] = value;
}
// Alway process incoming messages whenever possible
gw.process();
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}