MySensors Library & Examples  2.3.2-62-ge298769
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-2022 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 //#define MY_PJON
42 
43 #include <MySensors.h>
44 
45 #define SKETCH_NAME "Binary Sensor"
46 #define SKETCH_MAJOR_VER "1"
47 #define SKETCH_MINOR_VER "0"
48 
49 #define PRIMARY_CHILD_ID 3
50 #define SECONDARY_CHILD_ID 4
51 
52 #define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
53 #define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch
54 
55 #if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
56 #error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
57 #endif
58 #if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
59 #error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
60 #endif
61 #if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
62 #error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
63 #endif
64 #if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
65 #error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
66 #endif
67 
68 
69 // Change to V_LIGHT if you use S_LIGHT in presentation below
70 MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
71 MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);
72 
73 void setup()
74 {
75  // Setup the buttons
76  pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
77  pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
78 }
79 
81 {
82  // Send the sketch version information to the gateway and Controller
83  sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
84 
85  // Register binary input sensor to sensor_node (they will be created as child devices)
86  // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
87  // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
88  present(PRIMARY_CHILD_ID, S_DOOR);
89  present(SECONDARY_CHILD_ID, S_DOOR);
90 }
91 
92 // Loop will iterate on changes on the BUTTON_PINs
93 void loop()
94 {
95  uint8_t value;
96  static uint8_t sentValue=2;
97  static uint8_t sentValue2=2;
98 
99  // Short delay to allow buttons to properly settle
100  sleep(5);
101 
102  value = digitalRead(PRIMARY_BUTTON_PIN);
103 
104  if (value != sentValue) {
105  // Value has changed from last transmission, send the updated value
106  send(msg.set(value==HIGH));
107  sentValue = value;
108  }
109 
110  value = digitalRead(SECONDARY_BUTTON_PIN);
111 
112  if (value != sentValue2) {
113  // Value has changed from last transmission, send the updated value
114  send(msg2.set(value==HIGH));
115  sentValue2 = value;
116  }
117 
118  // Sleep until something happens with the sensor
119  sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0);
120 }
HIGH
#define HIGH
Definition: bcm2835.h:572
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: BinarySwitchSleepSensor.ino:93
presentation
void presentation()
Node presentation.
Definition: BinarySwitchSleepSensor.ino:80
MyMessage::set
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
send
bool send(MyMessage &msg, const bool requestEcho=false)
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
setup
void setup()
Called after node initialises but before main loop.
Definition: BinarySwitchSleepSensor.ino:73
sleep
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:290