Update 17-July-2016 Sensor v2.2 MySensors 2.0 Update
I have updated the code in the latest post for the release of MySensors 2.0! Not much has changed, just the calls. All of the same functionality is kept. Since MySensors 2.0 does not include any external libraries, you will need to manually add the debounce 2 library to your Arduino IDE/MySensors2 setup. The official GitHub repo is located here. Install it the same as any Arduino Library.
Any comments or questions please ask.
V2.2 Sketch:
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 2.2 - Removed MySensors 1.5 code and replaced with MySensors 2.0
* Version 2.1 - Derrick Rockwell (Drock1985)
* - Complete Re-write of code to provide better flow/structure.
* Version 2.0 - ResentedPoet
*
* Based on original concept/code by @petewill for 1 Door bell chime. See original thread
* http://forum.mysensors.org/topic/2064/how-to-doorbell-automation-hack
* This sketch is used to control a front and back doorbell ring with relays as well as send an
* alert when the buttons are pressed. For front door, Connect the button to ground and digital
* pin 3. The relay controlling the doorbell is conntected to pin 4. For rear door bell
* connect second button to ground and digital pin 5. The relay controlling that pin goes
* to pin 6.
*/
// Enable debug prints
// #define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#define MY_NODE_ID 2 //or use AUTO to have your home controller assign it for you.
#define MY_REPEATER_FEATURE // Enabled repeater feature for this node. Comment out if you do not want repeater function
#include <MySensors.h>
#include <SPI.h>
#include <Bounce2.h>
#define FRONT_DOOR_BUTTON 3 // Arduino Digital I/O pin number for the front doorbell button
#define FRONT_CHIME_RELAY 4 // Aduino Digital I/O pin number for the front door chime relay
#define CHILD_ID_FRONT 0 // Child ID for the front doorbell sensor
#define BACK_DOOR_BUTTON 5 // Arduino Digital I/O pin number for the back doorbell button
#define BACK_CHIME_RELAY 6 // Aduino Digital I/O pin number for the back door chime relay
#define CHILD_ID_BACK 1 // Child ID for the back doorbell sensor
#define CHILD_ID_MUTE 2 // Child ID for the mute option
#define CHIME_MUTE_STATE // Used to hold value of door chime mute funtion.
#define CHIME_OFF 0 // Variable to define ring the chime
#define CHIME_ON 1
#define FRONT_CHIME_RELAY_ON LOW // Variable for front chime. Reverse low/high if relay pin set different
#define FRONT_CHIME_RELAY_OFF HIGH // Variable for front chime. Reverse low/high if relay pin set different
#define BACK_CHIME_RELAY_ON LOW // Variable for front chime. Reverse low/high if relay pin set different
#define BACK_CHIME_RELAY_OFF HIGH // Variable for front chime. Reverse low/high if relay pin set different
Bounce debouncerF = Bounce();
Bounce debouncerB = Bounce();
boolean CHIME_MUTE = 1;
MyMessage msg(CHILD_ID_MUTE, CHIME_MUTE);
MyMessage frontDoorbell(CHILD_ID_FRONT, V_TRIPPED);
MyMessage backDoorbell(CHILD_ID_BACK, V_TRIPPED);
MyMessage chimeMute(CHILD_ID_MUTE, V_LIGHT);
int pestTimeout = 4000; // Delay between registered button presses. Stops button mashers ;) Set to whatever delay you want.
unsigned long previousMillis=0; // Tracks time since last door chime button press
void setup() {
// Setup the button and activate internal pull-up
pinMode(FRONT_DOOR_BUTTON, INPUT);
pinMode(BACK_DOOR_BUTTON, INPUT);
digitalWrite(FRONT_DOOR_BUTTON, HIGH);
digitalWrite(BACK_DOOR_BUTTON, HIGH);
// After setting up the button, setup debouncer
debouncerF.attach(FRONT_DOOR_BUTTON);
debouncerB.attach(BACK_DOOR_BUTTON);
debouncerF.interval(5);
debouncerB.interval(5);
// Make sure relays are off when starting up
digitalWrite(FRONT_CHIME_RELAY, FRONT_CHIME_RELAY_OFF);
digitalWrite(BACK_CHIME_RELAY, BACK_CHIME_RELAY_OFF);
// Then set relay pins in output mode
pinMode(FRONT_CHIME_RELAY, OUTPUT);
pinMode(BACK_CHIME_RELAY, OUTPUT);
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("2 Door Chime Sensor", "2.2");
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_FRONT, S_MOTION);
present(CHILD_ID_BACK, S_MOTION);
present(CHILD_ID_MUTE, S_LIGHT);
send(chimeMute.set(1));
}
void loop() {
unsigned long currentMillis = millis();
debouncerF.update();
debouncerB.update();
int valueF = debouncerF.read();
int valueB = debouncerB.read();
//Front Doorbell
if ( ((unsigned long)(currentMillis - previousMillis) >= pestTimeout) && valueF != HIGH && CHIME_MUTE == 1) {
digitalWrite(FRONT_CHIME_RELAY, FRONT_CHIME_RELAY_ON);
send(frontDoorbell.set(1));
wait(50);
digitalWrite(FRONT_CHIME_RELAY, FRONT_CHIME_RELAY_OFF);
send(frontDoorbell.set(0));
previousMillis = currentMillis;
}
if ( ((unsigned long)(currentMillis - previousMillis) >= pestTimeout) && valueF != HIGH && CHIME_MUTE == 0) {
send(frontDoorbell.set(1));
wait(50);;
send(frontDoorbell.set(0));
previousMillis = currentMillis;
}
//Back Doorbell
if ( ((unsigned long)(currentMillis - previousMillis) >= pestTimeout) && valueB != HIGH && CHIME_MUTE == 1) {
digitalWrite(BACK_CHIME_RELAY, BACK_CHIME_RELAY_ON);
send(backDoorbell.set(1));
wait(450);
digitalWrite(BACK_CHIME_RELAY, BACK_CHIME_RELAY_OFF);
send(backDoorbell.set(0));
previousMillis = currentMillis;
}
if ( ((unsigned long)(currentMillis - previousMillis) >= pestTimeout) && valueB != HIGH && CHIME_MUTE == 0) {
send(backDoorbell.set(1));
wait(50);
send(backDoorbell.set(0));
previousMillis = currentMillis;
}
}
void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.isAck()) {
Serial.println("This is an ack from gateway");
}
if (message.type == V_LIGHT) {
// Change relay state
CHIME_MUTE = message.getBool();
send(msg.set(CHILD_ID_MUTE, CHIME_MUTE?CHIME_ON:CHIME_OFF));
// Store state in eeprom
saveState(CHILD_ID_MUTE, CHIME_MUTE);
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}