MySensors Library & Examples  2.3.2
BinarySwitchSleepSensor.ino
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2019 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  *******************************
20  *
21  * DESCRIPTION
22  *
23  * Interrupt driven binary switch example with dual interrupts
24  * Author: Patrick 'Anticimex' Fallberg
25  * Connect one button or door/window reed switch between
26  * digital I/O pin 3 (BUTTON_PIN below) and GND and the other
27  * one in similar fashion on digital I/O pin 2.
28  * This example is designed to fit Arduino Nano/Pro Mini
29  *
30  */
31 
32 
33 // Enable debug prints to serial monitor
34 #define MY_DEBUG
35 
36 // Enable and select radio type attached
37 #define MY_RADIO_RF24
38 //#define MY_RADIO_NRF5_ESB
39 //#define MY_RADIO_RFM69
40 //#define MY_RADIO_RFM95
41 
42 #include <MySensors.h>
43 
44 #define SKETCH_NAME "Binary Sensor"
45 #define SKETCH_MAJOR_VER "1"
46 #define SKETCH_MINOR_VER "0"
47 
48 #define PRIMARY_CHILD_ID 3
49 #define SECONDARY_CHILD_ID 4
50 
51 #define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
52 #define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch
53 
54 #if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
55 #error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
56 #endif
57 #if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
58 #error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
59 #endif
60 #if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
61 #error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
62 #endif
63 #if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
64 #error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
65 #endif
66 
67 
68 // Change to V_LIGHT if you use S_LIGHT in presentation below
69 MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
70 MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);
71 
72 void setup()
73 {
74  // Setup the buttons
75  pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
76  pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
77 }
78 
80 {
81  // Send the sketch version information to the gateway and Controller
82  sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
83 
84  // Register binary input sensor to sensor_node (they will be created as child devices)
85  // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
86  // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
87  present(PRIMARY_CHILD_ID, S_DOOR);
88  present(SECONDARY_CHILD_ID, S_DOOR);
89 }
90 
91 // Loop will iterate on changes on the BUTTON_PINs
92 void loop()
93 {
94  uint8_t value;
95  static uint8_t sentValue=2;
96  static uint8_t sentValue2=2;
97 
98  // Short delay to allow buttons to properly settle
99  sleep(5);
100 
101  value = digitalRead(PRIMARY_BUTTON_PIN);
102 
103  if (value != sentValue) {
104  // Value has changed from last transmission, send the updated value
105  send(msg.set(value==HIGH));
106  sentValue = value;
107  }
108 
109  value = digitalRead(SECONDARY_BUTTON_PIN);
110 
111  if (value != sentValue2) {
112  // Value has changed from last transmission, send the updated value
113  send(msg2.set(value==HIGH));
114  sentValue2 = value;
115  }
116 
117  // Sleep until something happens with the sensor
118  sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0);
119 }
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
API declaration for MySensors.
bool send(MyMessage &msg, const bool requestEcho=false)
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
void setup()
Called after node initialises but before main loop.
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
void presentation()
Node presentation.
#define HIGH
Definition: bcm2835.h:572
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
void loop()
Main loop.