Well well well...
I've got it working with 2 relays and 2 door sensors.
See my sketch below!
The problem is the relays which are configured by counting and adding up numbers and the child_id's for 1 button and the debouncer missing config in the skecth.
With this sketch you can monitor 2 doors and 2 relays
// Example sketch showing how to control physical relays.
// This example will remember relay state even after power failure.
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define RELAY_1 4 // 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 0 // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay
#define BUTTON_PIN_1 3 // Arduino Digital I/O pin for button/reed switch
#define BUTTON_PIN_2 8 // Arduino Digital I/O pin for button/reed switch
MySensor gw;
Bounce debouncer_1 = Bounce();
Bounce debouncer_2 = Bounce();
int oldValue_1=-1;
int oldValue_2=-1;
MyMessage msgGK(BUTTON_PIN_1,V_TRIPPED);
MyMessage msgGD(BUTTON_PIN_2,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("Garagebox", "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);
}
// Setup the doorsensor
//pinMode(BUTTON_PIN,INPUT);
// Activate internal pull-up
//digitalWrite(BUTTON_PIN,HIGH);
pinMode(BUTTON_PIN_1,INPUT);
// Activate internal pull-up
digitalWrite(BUTTON_PIN_1,HIGH);
pinMode(BUTTON_PIN_2,INPUT);
// Activate internal pull-up
digitalWrite(BUTTON_PIN_2,HIGH);
// After setting up the button, setup debouncer
//debouncer.attach(BUTTON_PIN);
//debouncer.interval(5);
debouncer_1.attach(BUTTON_PIN_1);
debouncer_1.interval(5);
debouncer_2.attach(BUTTON_PIN_2);
debouncer_2.interval(5);
// 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(BUTTON_PIN_1, S_DOOR);
gw.present(BUTTON_PIN_2, S_DOOR);
}
// Check if digital input has changed and send in new value
void loop()
{
// Alway process incoming messages whenever possible
gw.process();
debouncer_1.update();
// Get the update value
int value_1 = debouncer_1.read();
if (value_1 != oldValue_1) {
// Send in the new value
gw.send(msgGK.set(value_1==HIGH ? 1 : 0));
oldValue_1 = value_1;
}
debouncer_2.update();
// Get the update value
int value_2 = debouncer_2.read();
if (value_2 != oldValue_2) {
// Send in the new value
gw.send(msgGD.set(value_2==HIGH ? 1 : 0));
oldValue_2 = value_2;
}
}
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());
}
}