Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Announcements
  3. 💬 Door, Window and Push-button Sensor

💬 Door, Window and Push-button Sensor

Scheduled Pinned Locked Moved Announcements
110 Posts 36 Posters 22.5k Views 32 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • hekH hek

    @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...

    R Offline
    R Offline
    rodaman
    wrote on last edited by
    #60

    @hek
    I have changed

    pinMode(buttonPin[i + FIRST_PIN], INPUT_PULLUP);
    

    to

    pinMode(buttonPin[i + FIRST_PIN], INPUT);
    

    thanks, but sadlly still does not work...

    1 Reply Last reply
    0
    • hekH Offline
      hekH Offline
      hek
      Admin
      wrote on last edited by
      #61

      @rodaman said in 💬 Door, Window and Push-button Sensor:

      http://www.mysensors.org/build/binary

      I don't get the buttonPin array thing at all?

      Why not just:
      pinMode(i + FIRST_PIN, INPUT_PULLUP);

      mfalkviddM R 2 Replies Last reply
      0
      • hekH hek

        @rodaman said in 💬 Door, Window and Push-button Sensor:

        http://www.mysensors.org/build/binary

        I don't get the buttonPin array thing at all?

        Why not just:
        pinMode(i + FIRST_PIN, INPUT_PULLUP);

        mfalkviddM Online
        mfalkviddM Online
        mfalkvidd
        Mod
        wrote on last edited by
        #62

        @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.

        1 Reply Last reply
        0
        • hekH hek

          @rodaman said in 💬 Door, Window and Push-button Sensor:

          http://www.mysensors.org/build/binary

          I don't get the buttonPin array thing at all?

          Why not just:
          pinMode(i + FIRST_PIN, INPUT_PULLUP);

          R Offline
          R Offline
          rodaman
          wrote on last edited by
          #63

          @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 :-)

          R 1 Reply Last reply
          1
          • R rodaman

            @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 :-)

            R Offline
            R Offline
            rodaman
            wrote on last edited by rodaman
            #64

            @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;
                }
             }
            }
            
            
            1 Reply Last reply
            1
            • gohanG Offline
              gohanG Offline
              gohan
              Mod
              wrote on last edited by
              #65

              Could these reed switches be used in the NC configuration for door/window sensor to save more battery?

              1 Reply Last reply
              0
              • scalzS Offline
                scalzS Offline
                scalz
                Hardware Contributor
                wrote on last edited by scalz
                #66

                @gohan
                yes exactly.
                i made a nrf5 board using this principle.

                1 Reply Last reply
                0
                • gohanG Offline
                  gohanG Offline
                  gohan
                  Mod
                  wrote on last edited by
                  #67

                  did you use those with 3 pins?

                  Nca78N 1 Reply Last reply
                  0
                  • scalzS Offline
                    scalzS Offline
                    scalz
                    Hardware Contributor
                    wrote on last edited by
                    #68

                    yes no-nc reed, connect them to IOs of your mcu and switch between input and output

                    1 Reply Last reply
                    1
                    • gohanG gohan

                      did you use those with 3 pins?

                      Nca78N Offline
                      Nca78N Offline
                      Nca78
                      Hardware Contributor
                      wrote on last edited by Nca78
                      #69

                      @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.

                      1 Reply Last reply
                      0
                      • gohanG Offline
                        gohanG Offline
                        gohan
                        Mod
                        wrote on last edited by
                        #70

                        given the low price, I think it is not worth the extra work to "mod" the normal reed switches :D

                        1 Reply Last reply
                        1
                        • alowhumA Offline
                          alowhumA Offline
                          alowhum
                          Plugin Developer
                          wrote on last edited by
                          #71

                          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)

                          1 Reply Last reply
                          0
                          • scalzS Offline
                            scalzS Offline
                            scalz
                            Hardware Contributor
                            wrote on last edited by
                            #72

                            @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.

                            1 Reply Last reply
                            1
                            • alowhumA Offline
                              alowhumA Offline
                              alowhum
                              Plugin Developer
                              wrote on last edited by alowhum
                              #73

                              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.

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                Pavel Polititsky
                                wrote on last edited by Pavel Polititsky
                                #74

                                I have some troubles with this device.
                                Sometimes it does not respond to the first click, sometimes it remains switched on after first click until second click

                                When I press the button once - I see the LED on my gateway blinks always 3 times
                                1 RX TX
                                2 RX ERR
                                3 RX ERR

                                Device powered with CR2032 and running on 1Mhz@1.8V (internal clock)
                                Button input pulled to GND with resistor 47K and waking up if VCC connected through the button
                                Also have a 10uF tantal capacitor on power supply pins +several 0,1uF ceramic capacitors

                                Actually I dont know how to debug/log my gateway messages on Linux

                                Node code:

                                
                                
                                // Enable debug prints to serial monitor
                                //#define MY_DEBUG
                                
                                // Enable and select radio type attached
                                #define MY_RADIO_NRF24
                                //#define MY_RADIO_NRF5_ESB
                                //#define MY_RADIO_RFM69
                                //#define MY_RADIO_RFM95
                                #define MY_RF24_PA_LEVEL RF24_PA_HIGH
                                
                                #include <MySensors.h>
                                
                                #define SKETCH_NAME "Door button"
                                #define SKETCH_MAJOR_VER "1"
                                #define SKETCH_MINOR_VER "0"
                                
                                #define PRIMARY_CHILD_ID 3
                                #define PRIMARY_BUTTON_PIN 2   // Arduino Digital I/O pin for button/reed switch
                                //#define BATTERY_SENSE_PIN A6  // select the input pin for the battery sense point
                                int oldBatteryPcnt = 0;
                                
                                // Change to V_LIGHT if you use S_LIGHT in presentation below
                                MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
                                
                                void setup()
                                {
                                  // Setup the buttons
                                  pinMode(PRIMARY_BUTTON_PIN, INPUT);
                                  //	pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
                                
                                }
                                
                                void presentation()
                                {
                                  // Send the sketch version information to the gateway and Controller
                                  sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
                                  present(PRIMARY_CHILD_ID, S_DOOR);
                                }
                                
                                // Loop will iterate on changes on the BUTTON_PINs
                                void loop()
                                {
                                  uint8_t value;
                                  static uint8_t sentValue = 2;
                                
                                  // Short delay to allow buttons to properly settle
                                  sleep(5);
                                
                                  value = digitalRead(PRIMARY_BUTTON_PIN);
                                
                                  if (value != sentValue) {
                                    // Value has changed from last transmission, send the updated value
                                    send(msg.set(value == HIGH));
                                    sentValue = value;
                                  }
                                
                                  // Sleep until something happens with the sensor
                                  sleep(PRIMARY_BUTTON_PIN - 2, CHANGE, 0);
                                }
                                

                                Serial data, one line = one button click

                                4;3;1;0;16;0
                                4;3;1;0;16;1
                                4;3;1;0;16;0
                                4;3;1;0;16;0
                                4;3;1;0;16;0
                                4;3;1;0;16;1
                                4;3;1;0;16;1
                                4;3;1;0;16;0
                                4;3;1;0;16;0
                                4;3;1;0;16;0
                                4;3;1;0;16;0
                                4;3;1;0;16;1
                                4;3;1;0;16;1
                                4;3;1;0;16;1
                                4;3;1;0;16;1
                                4;3;1;0;16;1
                                4;3;1;0;16;0
                                4;3;1;0;16;0
                                4;3;1;0;16;0
                                4;3;1;0;16;1
                                4;3;1;0;16;0
                                4;3;1;0;16;1
                                
                                1 Reply Last reply
                                0
                                • gohanG Offline
                                  gohanG Offline
                                  gohan
                                  Mod
                                  wrote on last edited by
                                  #75

                                  Uncomment the my_debug and you should see all the debug info.

                                  P 1 Reply Last reply
                                  0
                                  • gohanG gohan

                                    Uncomment the my_debug and you should see all the debug info.

                                    P Offline
                                    P Offline
                                    Pavel Polititsky
                                    wrote on last edited by
                                    #76

                                    @gohan I cant to set correct speed for the serial port because used 1mhz@int osc
                                    I tried all options...

                                    1 Reply Last reply
                                    0
                                    • gohanG Offline
                                      gohanG Offline
                                      gohan
                                      Mod
                                      wrote on last edited by
                                      #77

                                      You can do it on the gateway at least

                                      P 1 Reply Last reply
                                      0
                                      • gohanG gohan

                                        You can do it on the gateway at least

                                        P Offline
                                        P Offline
                                        Pavel Polititsky
                                        wrote on last edited by Pavel Polititsky
                                        #78

                                        @gohan already done but not possible to see anything
                                        Every line was shifted to the right side and finally no space left.
                                        I use minicom app for monitoring serial port

                                        ,1-1-0,s=3,c=1,t=16,pt=1,l=1,sg=,1-1-0,s=3,c=1,t=16,pt=1,l=1,sg=0:0
                                                                                                           ,1-1-0,s=3,c=1,t=16,pt=1,l=1,sg=0;255;3;0;9;3726496 TSF:MSG:READ0:0
                                                                                                                                                                              1;3;1;0;16;0
                                                                                                                                                                                          ,1-1-0,s=3,c=1,t=16,pt=1,l=1,sg=0;255;3;0;9;3727488 TSF:MSG:READ0:0
                                                                                                                                                                                                                                                             1;3;1;0;16;0
                                                                                                                                                                                                                                                                         ,1-1-0,s=3,c=1,t=16,pt=1,l=1,sg=0;255;3;0;9;3728475 TSF:MSG:READ0:0
                                                                                                                                                                                                                                                                                                                                            1;3;1;0;16;0
                                                                                                                                                                                                                                                                                                                                                        ,1-1-0,s=3,c1
                                                                                                                                                                                                                                                                                                                                                                    1
                                                                                                                                                                                                                                                                                                                                                                    0
                                                                                                                                                                                                                                                                                                                                                                    0
                                                                                                                                                                                                                                                                                                                                                                    1
                                                                                                                                                                                                                                                                                                                                                                    1
                                                                                                                                                                                                                                                                                                                                                                    1
                                                                                                                                                                                                                                                                                                                                                                    1
                                                                                                                                                                                                                                                                                                                                                                    =
                                        
                                        

                                        Actually i need to make 100-1000 clicks to catch a fail moment
                                        Here is working fine

                                        1 Reply Last reply
                                        0
                                        • gohanG Offline
                                          gohanG Offline
                                          gohan
                                          Mod
                                          wrote on last edited by
                                          #79

                                          Can't you just use the arduino ide serial monitor?

                                          P 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          11

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular