2 Reed Switches 1 Arduino Nano



  • Hoping for some help from the MySensors experts. Im running a home automation system using Domoticz as my controller and MySensors 2.0 Library for my nodes. I noticed the close proximity of my front door and my garage door and wanted to save a microcontroller and radio by having it handle the reed switches for both doors. First off I do not consider myself a coder Im much more of a hardware kind of guy. I ended up cobbling this sketch together by looking at an example I found in the forums and looking at the included binary switch sketch.

    My issue is when I open the front door, both the garage door and front door indicators on my Domoticz dashboard change state. When I open and close the garage door only the garage door indicator changes state. I suspect it has something to do with how the pin ID's are assigned but I just am not very strong in that department. Any help would be appreciated.

    /**

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

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69

    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>

    #define CHILD_ID_1 3
    #define BUTTON_PIN_1 3 // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID_2 4
    #define BUTTON_PIN_2 4 // Arduino Digital I/O pin for button/reed switch

    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce();

    int oldValue1=-1;
    int oldValue2=-1;

    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg1(CHILD_ID_1,V_TRIPPED);
    MyMessage msg2(CHILD_ID_2,V_TRIPPED);

    void setup()
    {
    // Setup the button
    pinMode(BUTTON_PIN_1,INPUT);
    pinMode(BUTTON_PIN_2,INPUT);
    // Activate internal pull-up
    digitalWrite(BUTTON_PIN_1,HIGH);
    digitalWrite(BUTTON_PIN_2,HIGH);

    // After setting up the button, setup debouncer
    debouncer1.attach(BUTTON_PIN_1);
    debouncer1.interval(5);
    debouncer2.attach(BUTTON_PIN_2);
    debouncer2.interval(5);
    }

    void presentation() {
    // Register binary input sensor to gw (they will be created as child devices)
    // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
    // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
    present(CHILD_ID_1, S_DOOR);
    present(CHILD_ID_2, S_DOOR);
    }

    // Check if digital input has changed and send in new value
    void loop()
    {
    {
    debouncer1.update();
    // Get the update value
    int value1 = debouncer1.read();

    if (value1 != oldValue1) {
      // Send in the new value
      send(msg1.set(value1==HIGH ? 1 : 0));
      oldValue1 = value1;
    

    }
    }

    {
    debouncer2.update();
    // Get the update value
    int value2 = debouncer2.read();

    if (value2 != oldValue2) {
      // Send in the new value
      send(msg2.set(value2==HIGH ? 1 : 0));
      oldValue2 = value2;
    

    }
    }
    }



  • The compiler is unable to find out what "value" is, since it has not been used before.
    Try this

    gw.send(msg.set(HIGH));
    

  • Mod

    @Ddawme the code looks good, except that it is hard to read. Use the auto format tool in the Arduino IDE to format it, that will make it much readable. Use the code button in the forum to mark the code as code.

    How is your wiring? Are both switches connected to ground? Does the same problem appear if you physically disconnect the garage door switch and open the front door?

    Next step could be to enable debug on the node to get information on what it does. You can also add Serial.println statements to see which parts of the code is executed and what values the variables have.

    Here is the code in a tidier presentation:

    /**
    
        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
        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
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_1 3
    #define BUTTON_PIN_1 3 // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID_2 4
    #define BUTTON_PIN_2 4 // Arduino Digital I/O pin for button/reed switch
    
    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce();
    
    int oldValue1 = -1;
    int oldValue2 = -1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg1(CHILD_ID_1, V_TRIPPED);
    MyMessage msg2(CHILD_ID_2, V_TRIPPED);
    
    void setup()
    {
      // Setup the button
      pinMode(BUTTON_PIN_1, INPUT);
      pinMode(BUTTON_PIN_2, INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_1, HIGH);
      digitalWrite(BUTTON_PIN_2, HIGH);
    
      // After setting up the button, setup debouncer
      debouncer1.attach(BUTTON_PIN_1);
      debouncer1.interval(5);
      debouncer2.attach(BUTTON_PIN_2);
      debouncer2.interval(5);
    }
    
    void presentation() {
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(CHILD_ID_1, S_DOOR);
      present(CHILD_ID_2, S_DOOR);
    }
    
    // Check if digital input has changed and send in new value
    void loop()
    {
      debouncer1.update();
      // Get the update value
      int value1 = debouncer1.read();
    
      if (value1 != oldValue1) {
        // Send in the new value
        send(msg1.set(value1 == HIGH ? 1 : 0));
        oldValue1 = value1;
      }
    
      debouncer2.update();
      // Get the update value
      int value2 = debouncer2.read();
    
      if (value2 != oldValue2) {
        // Send in the new value
        send(msg2.set(value2 == HIGH ? 1 : 0));
        oldValue2 = value2;
      }
    }
    


  • @mfalkvidd thank you. I was disappointed when I sat how it formatted. I will check your suggestions today.



  • @mfalkvidd

    Sorry to leave you hanging for so long. Was finally able to start my troubleshooting of the node today. Turns out it did end up being a wiring issue, filed as "Technician Error". Rather than having both the reed switches share a common ground, I had one sensor grounding to the data pin of the other.


  • Mod

    @Ddawme great that you found the problem. Thanks for reporting back!

    Need to keep an extra eye on those pesky technicians 😉


Log in to reply
 

Suggested Topics

2
Online

11.4k
Users

11.1k
Topics

112.7k
Posts