Skip to content
  • 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. 💬 Relay
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store

💬 Relay

Scheduled Pinned Locked Moved Announcements
139 Posts 47 Posters 33.7k Views 45 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.
  • riochickenR Offline
    riochickenR Offline
    riochicken
    wrote on last edited by
    #26

    @mfalkvidd : I have directly soldered the LED's to the board, would be a waste of relays.. :) Thanks I will look into the examples you just gave...

    1 Reply Last reply
    1
    • riochickenR Offline
      riochickenR Offline
      riochicken
      wrote on last edited by
      #27

      @mfalkvidd I have no clue how to decode the examples you gave, normally in vb or powershell I would check if a pin would be active if another color was requested so I could turn off the other color but I am a bit lost with the code....

      1 Reply Last reply
      0
      • pepsonP Offline
        pepsonP Offline
        pepson
        wrote on last edited by pepson
        #28

        Hi all
        I use this sketch for controlling 2 x relays. Can anybody change me this sketch to controliling only 1 x relay ?

        Next problem....
        Anybody know how edit sketch for relays to add itrms to send repeate update status to gateway? I want eliminate problem when node start and change status , but gateway is disabled. And after gateway will be enable show in my Domoticz old status/last memory before disable gateway. But after this node has changed status and this status is not show in Domoticz after run. I want to work it as Z-Wave. Z-wave has always actual status. Is any chance to also MySensors Gateway and not has always actual status ?

        /**
           DESCRIPTION
           Sketch for 2x relay with buttons monostable. After back power all relays set OFF and send correct status OFF to controller.  
        */
        
        // 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_RFM69_FREQUENCY RF69_868MHZ
        #define MY_IS_RFM69HW
        
        // Enabled repeater feature for this node
        #define MY_REPEATER_FEATURE
        
        // Node id defaults to AUTO (tries to fetch id from controller)
        #define MY_NODE_ID AUTO
        
        #include <SPI.h>
        #include <MySensors.h>
        #include <Bounce2.h>
        
        // Define Relays
        #define RELAY_ON 1  // GPIO value to write to turn on attached relay
        #define RELAY_OFF 0  // GPIO value to write to turn off attached relay
        
        // Define Sensor ID's
        #define SSR_A_ID 1   // Id of the sensor child
        #define SSR_B_ID 2   // Id of the sensor child
        
        // Define buttons and relays
        const int buttonPinA = 3;
        const int buttonPinB = 4;
        const int relayPinA = 5;
        const int relayPinB = 6;
        
        // Define Variables
        int oldValueA = 0;
        int oldValueB = 0;
        bool stateA = false;
        bool stateB = false;
        int trigger = 0;
        
        Bounce debouncerA = Bounce();
        Bounce debouncerB = Bounce();
        
        MyMessage msgA(SSR_A_ID, V_STATUS);
        MyMessage msgB(SSR_B_ID, V_STATUS);
        
        void setup()
        {
        
          pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
          pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
        
        
          // After setting up the buttons, setup debouncer
          debouncerA.attach(buttonPinA);
          debouncerA.interval(5);
          debouncerB.attach(buttonPinB);
          debouncerB.interval(5);
        
          // Make sure relays are off when starting up
          digitalWrite(relayPinA, RELAY_OFF);
          digitalWrite(relayPinB, RELAY_OFF);
          // Then set relay pins in output mode
          pinMode(relayPinA, OUTPUT);
          pinMode(relayPinB, OUTPUT);
        
        }
        
        void presentation()  {
          // Send the sketch version information to the gateway and Controller
          sendSketchInfo("2xRelay with monostable", "2.1");
        
          // Register all sensors to gw (they will be created as child devices)
          present(SSR_A_ID, S_LIGHT);
          present(SSR_B_ID, S_LIGHT);
        
        }
        
        /*
           Example on how to asynchronously check for new messages from gw
        */
        void loop()
        {
        if (trigger == 0){
          send(msgA.set(false)); // Send off state for relayA to ensure controller knows the switch is off
          send(msgB.set(false)); // Send off state for relayB to ensure controller knows the switch is off
          trigger = 1;
        }
          
          debouncerA.update();
          // Get the update value
          int valueA = debouncerA.read();
          if (valueA != oldValueA && valueA == 0) {
            send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
        
          }
          oldValueA = valueA;
         
        
          debouncerB.update();
          // Get the update value
          int valueB = debouncerB.read();
          if (valueB != oldValueB && valueB == 0) {
            send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
        
          }
          oldValueB = valueB;
        }
        
        void receive(const MyMessage &message) {
          // We only expect one type of message from controller. But we better check anyway.
          if (message.type == V_STATUS) {
              
            switch (message.sensor) {
              case 1:
                stateA = message.getBool();
                digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
                
                break;
              case 2:
                stateB = message.getBool();
                digitalWrite(message.sensor + 4, stateB ? RELAY_ON : RELAY_OFF);
                
                break;
              
            }
           
              // Write some debug info
            Serial.print("Incoming change for sensor:");
            Serial.println(message.sensor);
            Serial.print("from node:");
            Serial.println(message.sender);
            Serial.print(", New status: ");
            Serial.println(message.getBool());
          }
        }```
        1 Reply Last reply
        0
        • oufmanO Offline
          oufmanO Offline
          oufman
          wrote on last edited by
          #29

          Hello,
          I would like to do an auto off after 2 sec directly in the sketch, however I do not understand how to write the code? I do not want to make a loop, but on pressing the ON button from my box the relay is activated is deactivated after 2 sec.

          Thank you for your help.

          1 Reply Last reply
          0
          • EfflonE Offline
            EfflonE Offline
            Efflon
            wrote on last edited by
            #30

            I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
            Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?
            Also, after presentation, shouldn't there be a send() to the controller?

            korttomaK Boots33B 2 Replies Last reply
            0
            • EfflonE Efflon

              I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
              Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?
              Also, after presentation, shouldn't there be a send() to the controller?

              korttomaK Offline
              korttomaK Offline
              korttoma
              Hero Member
              wrote on last edited by
              #31

              @Efflon you are right about the loop() function it seems like the part where it should change the relay state is missing. Did you try to use this example code?

              The button should be wired between PIN 3 and GND so that when you push the button PIN 3 is Grounded.

              PIN 3 is connected to a "Debouncer" in the "setup()" ( so that when you push the button the code detects only one push and not several ).

              loop() monitors the debouncer and when it detects that you have pushed the button it will send the new state to the controller. I think it should also change the state so if it does not do that you could add the following line

              digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); //write new state to relay output

              Before this line in the loop() function:
              send(msg.set(state?false:true), true); // Send new state and request ack back

              You could also add the line:
              saveState(CHILD_ID, state); // Store state in eeprom

              To the same part of the code so that the state change is written to eeprom so that the previous state can be returned after a power failure.

              • Tomas
              EfflonE 1 Reply Last reply
              0
              • korttomaK korttoma

                @Efflon you are right about the loop() function it seems like the part where it should change the relay state is missing. Did you try to use this example code?

                The button should be wired between PIN 3 and GND so that when you push the button PIN 3 is Grounded.

                PIN 3 is connected to a "Debouncer" in the "setup()" ( so that when you push the button the code detects only one push and not several ).

                loop() monitors the debouncer and when it detects that you have pushed the button it will send the new state to the controller. I think it should also change the state so if it does not do that you could add the following line

                digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); //write new state to relay output

                Before this line in the loop() function:
                send(msg.set(state?false:true), true); // Send new state and request ack back

                You could also add the line:
                saveState(CHILD_ID, state); // Store state in eeprom

                To the same part of the code so that the state change is written to eeprom so that the previous state can be returned after a power failure.

                EfflonE Offline
                EfflonE Offline
                Efflon
                wrote on last edited by
                #32

                @korttoma
                Thanks for the explanation! Just as I suspected with missing relay code.

                1 Reply Last reply
                0
                • EfflonE Efflon

                  I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
                  Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?
                  Also, after presentation, shouldn't there be a send() to the controller?

                  Boots33B Offline
                  Boots33B Offline
                  Boots33
                  Hero Member
                  wrote on last edited by
                  #33

                  @Efflon said:

                  I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
                  Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?

                  The loop in the relay with button actuator sketch does three main things

                  checks for a new button push
                  changes the current state
                  Sends the new state to the controller

                  If you look at the message code you can see it also asks for an ACK back from the controller. That is the true at the end of the line.

                  send(msg.set(state?false:true), true);                         // Send new state and request ack back
                  

                  It is this returned ACK that is used to switch the relay to the new state in the void receive function

                  The one drawback in this code if it is used for say a light switch is that if this node looses contact with the controller then the relay cannot be changed by pressing the button.

                  EfflonE 1 Reply Last reply
                  1
                  • Boots33B Boots33

                    @Efflon said:

                    I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
                    Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?

                    The loop in the relay with button actuator sketch does three main things

                    checks for a new button push
                    changes the current state
                    Sends the new state to the controller

                    If you look at the message code you can see it also asks for an ACK back from the controller. That is the true at the end of the line.

                    send(msg.set(state?false:true), true);                         // Send new state and request ack back
                    

                    It is this returned ACK that is used to switch the relay to the new state in the void receive function

                    The one drawback in this code if it is used for say a light switch is that if this node looses contact with the controller then the relay cannot be changed by pressing the button.

                    EfflonE Offline
                    EfflonE Offline
                    Efflon
                    wrote on last edited by
                    #34

                    @Boots33 Ok, you explanation is what was my only explanation to the "missing" code. When experimenting with this using the mqtt gateway sketch and home-assistant as controller, the ACK is just an ACK and not a "real" message with payload thus just toggling the state bool and getting things out of sync..

                    1 Reply Last reply
                    0
                    • WoekaW Offline
                      WoekaW Offline
                      Woeka
                      wrote on last edited by Woeka
                      #35

                      I did not get the example working properly with my 8 channel SSR. So here is my updated version of the example.

                      I have changed the way the pins are assigned by using a array. The current example tries to connect relays 7 and 8 to pins 9 and 10 when you select an 8 channel relays. This won't work because those pins are used by the radio module.

                      I think you can connect a relays to any pin which is not used. If so this will make it possible to connect up to 14 relays to the mini pro when also the pins A5-A7 are available.

                      // Enable debug prints to serial monitor
                      #define MY_DEBUG
                      
                      // Enable and select radio type attached
                      #define MY_RADIO_NRF24
                      //#define MY_RADIO_RFM69
                      
                      // Enable repeater functionality for this node
                      #define MY_REPEATER_FEATURE
                      
                      #include <MySensors.h>
                      
                      const int PINS[] = {3, 4, 5, 6, 7, 8, 14, 15}; // I/O pins 3, 4, 5, 6, 7, 8, A0, A1 for the relays 
                      #define NUMBER_OF_RELAYS 8 // 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
                      
                      
                      void before()
                      {
                          for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
                              // Then set relay pins in output mode
                              pinMode(PINS[sensor-1], OUTPUT);
                              // Set relay to last known state (using eeprom storage)
                              digitalWrite(PINS[sensor-1], loadState(sensor)?RELAY_ON:RELAY_OFF);
                              
                          }
                      }
                      
                      void setup()
                      {
                      
                      }
                      
                      void presentation()
                      {
                          // Send the sketch version information to the gateway and Controller
                          sendSketchInfo("Relay", "1.0");
                      
                          for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
                              // Register all sensors to gw (they will be created as child devices)
                              present(sensor, S_BINARY);
                          }
                      }
                      
                      
                      void loop()
                      {
                      
                      }
                      
                      void receive(const MyMessage &message)
                      {
                          // We only expect one type of message from controller. But we better check anyway.
                          if (message.type==V_STATUS) {
                              // Change relay state
                              digitalWrite(PINS[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF);
                              // Store state in eeprom
                              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());
                          }
                      }
                      
                      I 1 Reply Last reply
                      3
                      • S Offline
                        S Offline
                        Samuel235
                        Hardware Contributor
                        wrote on last edited by
                        #36

                        I'm attempting to get my double relay board containing two switch inputs. I see that the two sketches suggested on the relay page are there but one seems to be updated to the new version which allows you to use multiple relays on the board but the other isn't, as far as i can see. How do I go about making the sketch with buttons accept more relays?

                        MySensors 2.1.1
                        Controller - OpenHAB (Virtual Machine)
                        Gateway - Arduino Mega MQTT Gateway W5100

                        Boots33B 1 Reply Last reply
                        0
                        • WoekaW Woeka

                          I did not get the example working properly with my 8 channel SSR. So here is my updated version of the example.

                          I have changed the way the pins are assigned by using a array. The current example tries to connect relays 7 and 8 to pins 9 and 10 when you select an 8 channel relays. This won't work because those pins are used by the radio module.

                          I think you can connect a relays to any pin which is not used. If so this will make it possible to connect up to 14 relays to the mini pro when also the pins A5-A7 are available.

                          // Enable debug prints to serial monitor
                          #define MY_DEBUG
                          
                          // Enable and select radio type attached
                          #define MY_RADIO_NRF24
                          //#define MY_RADIO_RFM69
                          
                          // Enable repeater functionality for this node
                          #define MY_REPEATER_FEATURE
                          
                          #include <MySensors.h>
                          
                          const int PINS[] = {3, 4, 5, 6, 7, 8, 14, 15}; // I/O pins 3, 4, 5, 6, 7, 8, A0, A1 for the relays 
                          #define NUMBER_OF_RELAYS 8 // 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
                          
                          
                          void before()
                          {
                              for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
                                  // Then set relay pins in output mode
                                  pinMode(PINS[sensor-1], OUTPUT);
                                  // Set relay to last known state (using eeprom storage)
                                  digitalWrite(PINS[sensor-1], loadState(sensor)?RELAY_ON:RELAY_OFF);
                                  
                              }
                          }
                          
                          void setup()
                          {
                          
                          }
                          
                          void presentation()
                          {
                              // Send the sketch version information to the gateway and Controller
                              sendSketchInfo("Relay", "1.0");
                          
                              for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
                                  // Register all sensors to gw (they will be created as child devices)
                                  present(sensor, S_BINARY);
                              }
                          }
                          
                          
                          void loop()
                          {
                          
                          }
                          
                          void receive(const MyMessage &message)
                          {
                              // We only expect one type of message from controller. But we better check anyway.
                              if (message.type==V_STATUS) {
                                  // Change relay state
                                  digitalWrite(PINS[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF);
                                  // Store state in eeprom
                                  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());
                              }
                          }
                          
                          I Offline
                          I Offline
                          ijobain
                          wrote on last edited by ijobain
                          #37

                          Hi @Woeka
                          I've been testing your code, but works inverse mode
                          if switch in domoticz is on the relay is off and vice versa

                          RaspberryPi - Domoticz - last stable | MySensors Gateway USB - 2.1.1 | RFLink Gateway USB - 45.9

                          Boots33B 1 Reply Last reply
                          0
                          • I ijobain

                            Hi @Woeka
                            I've been testing your code, but works inverse mode
                            if switch in domoticz is on the relay is off and vice versa

                            Boots33B Offline
                            Boots33B Offline
                            Boots33
                            Hero Member
                            wrote on last edited by
                            #38

                            @ijobain Try changing

                            #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
                            

                            To

                            #define RELAY_ON 1  // GPIO value to write to turn on attached relay
                            #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
                            

                            and see if that works as you expected

                            I 1 Reply Last reply
                            1
                            • S Samuel235

                              I'm attempting to get my double relay board containing two switch inputs. I see that the two sketches suggested on the relay page are there but one seems to be updated to the new version which allows you to use multiple relays on the board but the other isn't, as far as i can see. How do I go about making the sketch with buttons accept more relays?

                              Boots33B Offline
                              Boots33B Offline
                              Boots33
                              Hero Member
                              wrote on last edited by
                              #39

                              @Samuel235 Have a look at this thread for some ideas.

                              S 1 Reply Last reply
                              0
                              • Boots33B Boots33

                                @ijobain Try changing

                                #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
                                

                                To

                                #define RELAY_ON 1  // GPIO value to write to turn on attached relay
                                #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
                                

                                and see if that works as you expected

                                I Offline
                                I Offline
                                ijobain
                                wrote on last edited by
                                #40

                                @Boots33
                                Thank you so much
                                I am newbie

                                RaspberryPi - Domoticz - last stable | MySensors Gateway USB - 2.1.1 | RFLink Gateway USB - 45.9

                                1 Reply Last reply
                                0
                                • Boots33B Boots33

                                  @Samuel235 Have a look at this thread for some ideas.

                                  S Offline
                                  S Offline
                                  Samuel235
                                  Hardware Contributor
                                  wrote on last edited by
                                  #41

                                  @Boots33 - Thanks dude, I have had a quick skim read just but will bookmark it when i get my gateway working. These issues right now are driving me insane, and spending the day to try and find the problem. Thank you for linking me to that post!

                                  MySensors 2.1.1
                                  Controller - OpenHAB (Virtual Machine)
                                  Gateway - Arduino Mega MQTT Gateway W5100

                                  1 Reply Last reply
                                  0
                                  • R Offline
                                    R Offline
                                    Rene046
                                    wrote on last edited by
                                    #42

                                    I just tried this sketch, works on latest version, but i cant seen any relais in Domoticz, only see the node relais, did i miss something

                                    HenryWhiteH 1 Reply Last reply
                                    0
                                    • R Rene046

                                      I just tried this sketch, works on latest version, but i cant seen any relais in Domoticz, only see the node relais, did i miss something

                                      HenryWhiteH Offline
                                      HenryWhiteH Offline
                                      HenryWhite
                                      wrote on last edited by
                                      #43

                                      @Rene046 you could try my sketch: https://forum.mysensors.org/topic/6638/multiple-relays-motion-sketch-fully-customizable-optional-timer-manual-override

                                      R 1 Reply Last reply
                                      0
                                      • HenryWhiteH HenryWhite

                                        @Rene046 you could try my sketch: https://forum.mysensors.org/topic/6638/multiple-relays-motion-sketch-fully-customizable-optional-timer-manual-override

                                        R Offline
                                        R Offline
                                        Rene046
                                        wrote on last edited by
                                        #44

                                        @HenryWhite

                                        thx

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          sineverba
                                          Hardware Contributor
                                          wrote on last edited by sineverba
                                          #45

                                          Hi to all!
                                          I would edit the secure relay sketch so in loop the node sends to the gateway, let's say every hours, his state (on or off).

                                          1. I avoid delay otherwise Arduino cannot receive the comands from gateway, so I will use the millis sketch, modified from the official Arduino website.

                                          My doubt is... How can I get the current state of the relay?

                                          Relevant part of sketch is in loop and before setup()

                                          /* TIME variables */
                                          unsigned long previousMillis = 0;        // will store last time state is sent
                                          // constants won't change:
                                          const long interval = 10000;           // interval at which to send state (milliseconds === seconds x 1000). For test we try every 10 seconds
                                          

                                          And the main loop

                                          void loop()
                                          {
                                          
                                            // check to see if it's time to send state; that is, if the difference
                                            // between the current time and last time you sent state is bigger than
                                            // the interval at which you want to send the state.
                                            unsigned long currentMillis = millis();
                                          
                                            if (currentMillis - previousMillis >= interval) {
                                              // save the last time you blinked the LED
                                              previousMillis = currentMillis;
                                          
                                              // send the state here!
                                              // test
                                              Serial.println("This is the moment where I need to send state");
                                          
                                              //send(?????????); // Send current state. Probabily will update also the log?
                                            }
                                          

                                          The base sketch is >>> https://github.com/mysensors/MySensors/blob/master/examples/SecureActuator/SecureActuator.ino

                                          And the millis example is >>> https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

                                          Thank you to all!

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


                                          18

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 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
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular