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. My Project
  3. Combine Relay Actuator (with button) and BinarySwitch examples?

Combine Relay Actuator (with button) and BinarySwitch examples?

Scheduled Pinned Locked Moved My Project
7 Posts 4 Posters 4.9k Views 1 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.
  • T Offline
    T Offline
    TommySharp
    wrote on last edited by
    #1

    Combine Relay Actuator (with button) and BinarySwitch examples?

    Have to say firstly that I'm still a novice so hopefully someone can help me out....

    I've managed to use the examples to create a Relay Actuator with button and a separate Binary Switch sensor using two separate arduino nanos.

    I'm now trying to combine the two sketches into one and don't quite know what I'm doing....

    Any help would be appreciated....

    1 Reply Last reply
    0
    • greglG Offline
      greglG Offline
      gregl
      Hero Member
      wrote on last edited by
      #2

      Hi
      I'm a bit confused what sketches you are trying to combine..
      I take it you are starting with
      http://www.mysensors.org/build/relay#relaywithbuttonactuator-example

      as a base?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TommySharp
        wrote on last edited by
        #3

        Yeah I'm just using the examples provided by this website...

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TommySharp
          wrote on last edited by
          #4

          Ideally I'd start with the relay with button script and then I want to add 2 window/door sensors for my garage doors.

          1 Reply Last reply
          0
          • Sander StolkS Offline
            Sander StolkS Offline
            Sander Stolk
            wrote on last edited by
            #5

            I have this as an example but it's not showing the buttons in my controller:
            The relays on the other hand I can control

            // Example sketch showing how to control physical relays. 
            // This example will remember relay state even after power failure.
            
            #include <MySensor.h>
            #include <SPI.h>
            #include <Bounce2.h>
            
            #define CHILD_ID 2
            #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
            #define NUMBER_OF_RELAYS 2 // Total number of attached relays
            #define RELAY_ON 0  // GPIO value to write to turn on attached relay
            #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
            
            #define CHILD_ID 3
            #define BUTTON_PIN_1 3  // Arduino Digital I/O pin for button/reed switch
            #define BUTTON_PIN_2 8  // Arduino Digital I/O pin for button/reed switch
            
            MySensor gw;
            Bounce debouncer = Bounce(); 
            int oldValue=-1;
            
            // Change to V_LIGHT if you use S_LIGHT in presentation below
            MyMessage msg(CHILD_ID,V_TRIPPED);
            
            void setup()  
            {  
              // Initialize library and add callback for incoming messages
              gw.begin(incomingMessage, AUTO, true);
              // Send the sketch version information to the gateway and Controller
              gw.sendSketchInfo("Relay", "1.0");
              gw.present(CHILD_ID, S_DOOR);
              // Fetch relay status
              for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                // Register all sensors to gw (they will be created as child devices)
                gw.present(sensor, S_LIGHT);
                // Then set relay pins in output mode
                pinMode(pin, OUTPUT);   
                // Set relay to last known state (using eeprom storage) 
                digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
              }
               // Setup the button
              pinMode(BUTTON_PIN_1,INPUT);
              // Activate internal pull-up
              digitalWrite(BUTTON_PIN_1,HIGH);
              pinMode(BUTTON_PIN_2,INPUT);
              // Activate internal pull-up
              digitalWrite(BUTTON_PIN_2,HIGH);
              
              // After setting up the button, setup debouncer
              debouncer.attach(BUTTON_PIN_1);
              debouncer.interval(5);
              debouncer.attach(BUTTON_PIN_2);
              debouncer.interval(5);
              
              // 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.
                
            }
            
            
            void loop() 
            {
              // Alway process incoming messages whenever possible
              gw.process();
              
                debouncer.update();
              // Get the update value
              int value = debouncer.read();
             
              if (value != oldValue) {
                 // Send in the new value
                 gw.send(msg.set(value==HIGH ? 1 : 0));
                 oldValue = value;
              }
            }
            
            void incomingMessage(const MyMessage &message) {
              // We only expect one type of message from controller. But we better check anyway.
              if (message.type==V_LIGHT) {
                 // Change relay state
                 digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                 // Store state in eeprom
                 gw.saveState(message.sensor, message.getBool());
                 // Write some debug info
                 Serial.print("Incoming change for sensor:");
                 Serial.print(message.sensor);
                 Serial.print(", New status: ");
                 Serial.println(message.getBool());
               } 
            }
            
            
            
            E 1 Reply Last reply
            0
            • Sander StolkS Sander Stolk

              I have this as an example but it's not showing the buttons in my controller:
              The relays on the other hand I can control

              // Example sketch showing how to control physical relays. 
              // This example will remember relay state even after power failure.
              
              #include <MySensor.h>
              #include <SPI.h>
              #include <Bounce2.h>
              
              #define CHILD_ID 2
              #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
              #define NUMBER_OF_RELAYS 2 // Total number of attached relays
              #define RELAY_ON 0  // GPIO value to write to turn on attached relay
              #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
              
              #define CHILD_ID 3
              #define BUTTON_PIN_1 3  // Arduino Digital I/O pin for button/reed switch
              #define BUTTON_PIN_2 8  // Arduino Digital I/O pin for button/reed switch
              
              MySensor gw;
              Bounce debouncer = Bounce(); 
              int oldValue=-1;
              
              // Change to V_LIGHT if you use S_LIGHT in presentation below
              MyMessage msg(CHILD_ID,V_TRIPPED);
              
              void setup()  
              {  
                // Initialize library and add callback for incoming messages
                gw.begin(incomingMessage, AUTO, true);
                // Send the sketch version information to the gateway and Controller
                gw.sendSketchInfo("Relay", "1.0");
                gw.present(CHILD_ID, S_DOOR);
                // Fetch relay status
                for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                  // Register all sensors to gw (they will be created as child devices)
                  gw.present(sensor, S_LIGHT);
                  // Then set relay pins in output mode
                  pinMode(pin, OUTPUT);   
                  // Set relay to last known state (using eeprom storage) 
                  digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
                }
                 // Setup the button
                pinMode(BUTTON_PIN_1,INPUT);
                // Activate internal pull-up
                digitalWrite(BUTTON_PIN_1,HIGH);
                pinMode(BUTTON_PIN_2,INPUT);
                // Activate internal pull-up
                digitalWrite(BUTTON_PIN_2,HIGH);
                
                // After setting up the button, setup debouncer
                debouncer.attach(BUTTON_PIN_1);
                debouncer.interval(5);
                debouncer.attach(BUTTON_PIN_2);
                debouncer.interval(5);
                
                // 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.
                  
              }
              
              
              void loop() 
              {
                // Alway process incoming messages whenever possible
                gw.process();
                
                  debouncer.update();
                // Get the update value
                int value = debouncer.read();
               
                if (value != oldValue) {
                   // Send in the new value
                   gw.send(msg.set(value==HIGH ? 1 : 0));
                   oldValue = value;
                }
              }
              
              void incomingMessage(const MyMessage &message) {
                // We only expect one type of message from controller. But we better check anyway.
                if (message.type==V_LIGHT) {
                   // Change relay state
                   digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                   // Store state in eeprom
                   gw.saveState(message.sensor, message.getBool());
                   // Write some debug info
                   Serial.print("Incoming change for sensor:");
                   Serial.print(message.sensor);
                   Serial.print(", New status: ");
                   Serial.println(message.getBool());
                 } 
              }
              
              
              
              E Offline
              E Offline
              ewgor
              wrote on last edited by
              #6

              @Sander-Stolk are you using Openhab? can you share your config please?

              1 Reply Last reply
              0
              • Sander StolkS Offline
                Sander StolkS Offline
                Sander Stolk
                wrote on last edited by
                #7

                Well well well...
                I've got it working with 2 relays and 2 door sensors.

                See my sketch below!

                The problem is the relays which are configured by counting and adding up numbers and the child_id's for 1 button and the debouncer missing config in the skecth.

                With this sketch you can monitor 2 doors and 2 relays

                // Example sketch showing how to control physical relays. 
                // This example will remember relay state even after power failure.
                
                #include <MySensor.h>
                #include <SPI.h>
                #include <Bounce2.h>
                
                #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                #define NUMBER_OF_RELAYS 2 // Total number of attached relays
                #define RELAY_ON 0  // GPIO value to write to turn on attached relay
                #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
                
                #define BUTTON_PIN_1  3  // Arduino Digital I/O pin for button/reed switch
                #define BUTTON_PIN_2  8  // Arduino Digital I/O pin for button/reed switch
                
                MySensor gw;
                Bounce debouncer_1 = Bounce();
                Bounce debouncer_2 = Bounce();
                int oldValue_1=-1;
                int oldValue_2=-1;
                
                MyMessage msgGK(BUTTON_PIN_1,V_TRIPPED);
                MyMessage msgGD(BUTTON_PIN_2,V_TRIPPED);
                
                void setup()  
                {   
                  // Initialize library and add callback for incoming messages
                  gw.begin(incomingMessage, AUTO, true);
                  // Send the sketch version information to the gateway and Controller
                  gw.sendSketchInfo("Garagebox", "1.0");
                
                  // Fetch relay status
                  for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                    // Register all sensors to gw (they will be created as child devices)
                    gw.present(sensor, S_LIGHT);
                    // Then set relay pins in output mode
                    pinMode(pin, OUTPUT);   
                    // Set relay to last known state (using eeprom storage) 
                    digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
                  }
                
                // Setup the doorsensor
                  //pinMode(BUTTON_PIN,INPUT);
                  // Activate internal pull-up
                  //digitalWrite(BUTTON_PIN,HIGH);
                  
                  pinMode(BUTTON_PIN_1,INPUT);
                  // Activate internal pull-up
                  digitalWrite(BUTTON_PIN_1,HIGH);
                  
                  pinMode(BUTTON_PIN_2,INPUT);
                  // Activate internal pull-up
                  digitalWrite(BUTTON_PIN_2,HIGH);
                  
                  // After setting up the button, setup debouncer
                  //debouncer.attach(BUTTON_PIN);
                  //debouncer.interval(5);
                  debouncer_1.attach(BUTTON_PIN_1);
                  debouncer_1.interval(5);
                  debouncer_2.attach(BUTTON_PIN_2);
                  debouncer_2.interval(5);
                  
                  // 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.
                  gw.present(BUTTON_PIN_1, S_DOOR);
                  gw.present(BUTTON_PIN_2, S_DOOR);  
                }
                
                
                //  Check if digital input has changed and send in new value
                
                
                
                void loop() 
                {
                  // Alway process incoming messages whenever possible
                  gw.process();
                  
                  debouncer_1.update();
                  // Get the update value
                  int value_1 = debouncer_1.read();
                 
                  if (value_1 != oldValue_1) {
                     // Send in the new value
                     gw.send(msgGK.set(value_1==HIGH ? 1 : 0));
                     oldValue_1 = value_1;
                  }
                  
                    debouncer_2.update();
                  // Get the update value
                  int value_2 = debouncer_2.read();
                 
                  if (value_2 != oldValue_2) {
                     // Send in the new value
                     gw.send(msgGD.set(value_2==HIGH ? 1 : 0));
                     oldValue_2 = value_2;
                  }
                }
                
                void incomingMessage(const MyMessage &message) {
                  // We only expect one type of message from controller. But we better check anyway.
                  if (message.type==V_LIGHT) {
                     // Change relay state
                     digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                     // Store state in eeprom
                     gw.saveState(message.sensor, message.getBool());
                     // Write some debug info
                     Serial.print("Incoming change for sensor:");
                     Serial.print(message.sensor);
                     Serial.print(", New status: ");
                     Serial.println(message.getBool());
                   } 
                }
                
                
                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                20

                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