Code: Select all
/*
* 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 <[email protected]>
* 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 1.0 - PeteWill
*
* DESCRIPTION
* This sketch is used to control a doorbell ring with a relay as well as send an
* alert when the buttons is pressed. Connect the button to ground and digital
* pin 3. The relay controlling the doorbell is conntected to pin 4.
*
* Watch the How To video here: https://youtu.be/nMIcalwpstc
*
* Version 1.1 - ResentedPoet
*
* Added gw.send command so that if doorbellDetect variable was active, it would send V_TRIPPED
* to the motion sensor controlling the doorbell. Necessary for Domoticz compatibility
*/
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define NODE_ID AUTO // or set to AUTO if you want gw to assign a NODE_ID for you.
#define DOORBELL_PIN A0 // Arduino Digital I/O pin number for the doorbell button
#define RELAY_PIN 4 // Arduino Digital I/O pin number for the relay
#define DOORBELL_CHILD_ID 0 //ID of the doorbell
#define SWITCH_CHILD_ID 1 // Id of the switch that will control doorbell sound
#define RELAY_ON 1
#define RELAY_OFF 0
Bounce debouncer = Bounce();
MySensor gw;
MyMessage switchMsg(SWITCH_CHILD_ID, V_LIGHT);
MyMessage doorbellMsg(DOORBELL_CHILD_ID, V_TRIPPED);
unsigned int doorbellDelay = 1000; // interval at which to keep the doorbell button sensor triggered (milliseconds). This is used to stop people (kids) from pressing it too often
unsigned int ringTime = 400; //How long the doorbell relay is on (in milliseconds)
unsigned long doorbellMillis; //Used to keep track of the last doorbell button press
unsigned long doorbellTimer; //Used to keep track of doorbell ring time
byte doorbellPreviousVal; //Used to keep track of doorbell button pressed state
boolean ringDoorbell; //Used to initiate the ring doorbell if statement
boolean doorbellSound; //Used to keep track if the doorbell should sound or be silent. Value recieved from doorbell on/off switch
boolean doorbellOff = true; //Used to keep track of doorbell ring state
void setup()
{
gw.begin(incomingMessage, NODE_ID, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Doorbell Monitor", "1.0");
// Setup the button and activate internal pull-up
pinMode(DOORBELL_PIN, INPUT_PULLUP);
// After setting up the button, setup debouncer
debouncer.attach(DOORBELL_PIN);
debouncer.interval(5);
// Register all sensors to gw (they will be created as child devices)
gw.present(SWITCH_CHILD_ID, S_LIGHT);
gw.present(DOORBELL_CHILD_ID, S_MOTION);
// Make sure relays are off when starting up
digitalWrite(RELAY_PIN, RELAY_OFF);
// Then set relay pins in output mode
pinMode(RELAY_PIN, OUTPUT);
// Set doorbellSound to last known state (using eeprom storage)
doorbellSound = gw.loadState(SWITCH_CHILD_ID);
}
void loop()
{
gw.process();
unsigned long currentMillis = millis();
//Check to see if doorbell button was pushed.
if (currentMillis - doorbellMillis > doorbellDelay) //used to stop doorbell from being pressed too frequently
{
debouncer.update();
// Read doorbell button value
byte doorbellDetect = !debouncer.read();//read, then reverse the value so it will send correct trigger state to controller
if (doorbellDetect != doorbellPreviousVal)
{
//Serial.print("doorbellDetect Value: ");
//Serial.println(doorbellDetect);
if (doorbellDetect == 1)
{
ringDoorbell = true;
doorbellTimer = currentMillis;
gw.send(doorbellMsg.set(doorbellDetect?"1":"0")); // Send tripped value to gw
}
doorbellMillis = currentMillis;
doorbellPreviousVal = doorbellDetect;
}
}
if (ringDoorbell)
{
if (doorbellSound)
{
if (doorbellOff)
{
digitalWrite(RELAY_PIN, RELAY_ON);
//Serial.println("Doorbell sounded.");
doorbellOff = false;
}
else
{
if (currentMillis - doorbellTimer > ringTime)
{
ringDoorbell = false;
digitalWrite(RELAY_PIN, RELAY_OFF);
//Serial.println("Doorbell off.");
doorbellOff = true;
}
}
}
}
}
void incomingMessage(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
doorbellSound = message.getBool();
// Store state in eeprom
gw.saveState(SWITCH_CHILD_ID, doorbellSound);
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
Any idea what I need to do to make it work?