💬 Door, Window and Push-button Sensor
-
Hi,
I rewrite sketch for doors/window/buttons to work with multiple buttons (no rellays) .It shows up in Domoticz but it did not change status buttons if pressed.
Can someone look for the skech what I am doing wrong/** 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. ******************************* DESCRIPTION Simple binary switch example updated for 2 switches Connect button or door/window reed switch between digitial I/O pin 3 (BUTTON_PIN below) and GND. http://www.mysensors.org/build/binary */ // Enable debug prints to serial monitor //#define MY_DEBUG // Enable and select radio type attached //#define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #define MY_GATEWAY_SERIAL //#include <SPI.h> #include <MySensors.h> #include <Bounce2.h> #define FIRST_PIN 8 // Arduino Digital I/O pin for button/reed switch #define noButtons 4 const int buttonPin[noButtons]; Bounce debouncer[noButtons] = Bounce(); int oldValue[noButtons]; MyMessage msg[noButtons]; void setup() { for (int i = 0; i < noButtons; i++) { msg[i].sensor = i; // initialize messages msg[i].type = V_TRIPPED; // pinMode(buttonPin[i + FIRST_PIN], INPUT_PULLUP); digitalWrite(buttonPin[i + FIRST_PIN], HIGH); debouncer[i] = Bounce(); // initialize debouncer debouncer[i].attach(buttonPin[i + FIRST_PIN]); debouncer[i].interval(30); } } void presentation() { sendSketchInfo("Doors", "1.0"); for (int i = 0; i < noButtons; i++) present(i, S_DOOR); // present sensor to gateway } // Check if digital input has changed and send in new value void loop() { for (int i = 0; i < noButtons; i++) { debouncer[i].update(); int value = debouncer[i].read(); if (value != oldValue[i]) { // Send in the new value send(msg[i].set(value == HIGH ? 1 : 0)); oldValue[i] = value; } } } -
Hi,
I rewrite sketch for doors/window/buttons to work with multiple buttons (no rellays) .It shows up in Domoticz but it did not change status buttons if pressed.
Can someone look for the skech what I am doing wrong/** 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. ******************************* DESCRIPTION Simple binary switch example updated for 2 switches Connect button or door/window reed switch between digitial I/O pin 3 (BUTTON_PIN below) and GND. http://www.mysensors.org/build/binary */ // Enable debug prints to serial monitor //#define MY_DEBUG // Enable and select radio type attached //#define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #define MY_GATEWAY_SERIAL //#include <SPI.h> #include <MySensors.h> #include <Bounce2.h> #define FIRST_PIN 8 // Arduino Digital I/O pin for button/reed switch #define noButtons 4 const int buttonPin[noButtons]; Bounce debouncer[noButtons] = Bounce(); int oldValue[noButtons]; MyMessage msg[noButtons]; void setup() { for (int i = 0; i < noButtons; i++) { msg[i].sensor = i; // initialize messages msg[i].type = V_TRIPPED; // pinMode(buttonPin[i + FIRST_PIN], INPUT_PULLUP); digitalWrite(buttonPin[i + FIRST_PIN], HIGH); debouncer[i] = Bounce(); // initialize debouncer debouncer[i].attach(buttonPin[i + FIRST_PIN]); debouncer[i].interval(30); } } void presentation() { sendSketchInfo("Doors", "1.0"); for (int i = 0; i < noButtons; i++) present(i, S_DOOR); // present sensor to gateway } // Check if digital input has changed and send in new value void loop() { for (int i = 0; i < noButtons; i++) { debouncer[i].update(); int value = debouncer[i].read(); if (value != oldValue[i]) { // Send in the new value send(msg[i].set(value == HIGH ? 1 : 0)); oldValue[i] = value; } } } -
@mfalkvidd
thank you, i hope will be good soul who fix my sketch itself to work... :-) -
@rodaman said in 💬 Door, Window and Push-button Sensor:
What is the purpose of the buttonPin-array?
E.g.
pinMode(buttonPin[i + FIRST_PIN], INPUT_PULLUP);
This will fetch a random (probably 0-initialised) value from the array and set pinmode on that value...
-
@rodaman said in 💬 Door, Window and Push-button Sensor:
What is the purpose of the buttonPin-array?
E.g.
pinMode(buttonPin[i + FIRST_PIN], INPUT_PULLUP);
This will fetch a random (probably 0-initialised) value from the array and set pinmode on that value...
-
@rodaman said in 💬 Door, Window and Push-button Sensor:
What is the purpose of the buttonPin-array?
E.g.
pinMode(buttonPin[i + FIRST_PIN], INPUT_PULLUP);
This will fetch a random (probably 0-initialised) value from the array and set pinmode on that value...
-
@rodaman said in 💬 Door, Window and Push-button Sensor:
I don't get the buttonPin array thing at all?
Why not just:
pinMode(i + FIRST_PIN, INPUT_PULLUP); -
@rodaman said in 💬 Door, Window and Push-button Sensor:
I don't get the buttonPin array thing at all?
Why not just:
pinMode(i + FIRST_PIN, INPUT_PULLUP);@hek if the array was initialized, it could be used to set arbitrary pins. So pin 4,5,6,7,8,A0,A1,A2,A3 could be used for example (to avoid conflicts with the standard nrf24 wiring but still allow a lot of buttons). But as you mentioned before, without initializing the array the behavior will be strange.
-
@rodaman said in 💬 Door, Window and Push-button Sensor:
I don't get the buttonPin array thing at all?
Why not just:
pinMode(i + FIRST_PIN, INPUT_PULLUP); -
@hek said in 💬 Door, Window and Push-button Sensor:
pinMode(i + FIRST_PIN, INPUT_PULLUP);
Big Thanks!
I have changed all array parts in setup in the same manner like above and it works :-)@rodaman
Hi,
Here is a sketch for multi buttons (no relays, no repeated parts of code for each buttons) just enter number of buttons and first digital pin where buttons are attached.
Thanks to @hek for help.../** 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. ******************************* DESCRIPTION Simple binary switch example updated for multi switches Connect buttons or door/window reed switches between digitial I/O choosen pin (FIRST_PIN below) and GND. http://www.mysensors.org/build/binary */ // Enable debug prints to serial monitor //#define MY_DEBUG // Enable and select radio type attached //#define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #define MY_GATEWAY_SERIAL //#include <SPI.h> #include <MySensors.h> #include <Bounce2.h> #define FIRST_PIN 2 // Arduino Digital I/O pin for button/reed switch #define noButtons 6 Bounce debouncer[noButtons] = Bounce(); int oldValue[noButtons]; int value; MyMessage msg[noButtons]; void setup() { for (int i = 0; i < noButtons; i++) { oldValue[i] = -1; msg[i].sensor = i; // initialize messages msg[i].type = V_TRIPPED; // pinMode(i + FIRST_PIN, INPUT_PULLUP); digitalWrite(i + FIRST_PIN, HIGH); debouncer[i] = Bounce(); // initialize debouncer debouncer[i].attach(i + FIRST_PIN); debouncer[i].interval(3); } } void presentation() { sendSketchInfo("Doors", "1.0"); for (int i = 0; i < noButtons; i++) present(i, S_DOOR); // present sensor to gateway } // Check if digital input has changed and send in new value void loop() { for (int i = 0; i < noButtons; i++) { debouncer[i].update(); value = debouncer[i].read(); if (value != oldValue[i]) { // Send in the new value send(msg[i].set(value == HIGH ? 1 : 0)); oldValue[i] = value; } } } -
yes no-nc reed, connect them to IOs of your mcu and switch between input and output
-
@gohan said in 💬 Door, Window and Push-button Sensor:
did you use those with 3 pins?
I use them, with one wire connected to each interrupt pin (I only activate the one that's not connected) and they work fine.
Else you can cheat with a basic reed switch, you just need an additional magnet on the other side of your switch that's weaker than the door magnet, and turn your reed 180°.
You make the weak magnet close the reed when door is opened and far from the strong door magnet. When you close the door the strong magnet will open the reed switch. -
I'd like to go even simpler:
- Window is closed - magnet pulls reed switch so that no power flows to the NRF5.
- Window is opened - The NRF5 now gets power, boots, connects to the MySensors network, and sends a "window is open" message every 10 minutes.
- Window is closed again - NRF5 loses power.
Seems to me that this would be quite energy efficient?
Perhaps build it into this case:
https://www.aliexpress.com/item/Home-Burglar-Alarm-Wireless-Home-Security-Door-Window-Entry-Burglar-Alarm-System-Magnetic-Sensor-drop-shipping/32876055564.html
(runs on 2 AAA batteries) -
@alowhum
I would say this technique is efficient depending on the usecase.
For example, for basic ambiant sensors not needing any security why not. On other side, for main doors which could benefit security, I prefer to use hearbeat with less interval time, so it's possible to know if the sensor is offline. else controller wouldn't know this if the sensor only update when door state change. -
Totally true.
I was wondering what the state of Attiny85 support is. For absolute beginners (in workshops) it might be easier to connect the radio to a digispark.
// cancel that. It could never support encryption.