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

                                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 Offline
                                S Offline
                                sineverba
                                Hardware Contributor
                                wrote on last edited by sineverba
                                #46

                                @sineverba
                                Edit:
                                I did edit the pre-setup configuration

                                int val = 0;  // the value of digital pin
                                #define CHILD_ID 1   // Id of the sensor child
                                MyMessage msg(CHILD_ID,V_LIGHT);
                                

                                And I did edit the loop:

                                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");
                                
                                    val = digitalRead(LOCK_1);
                                
                                    
                                    Serial.print("Il valore del relay รจ ");
                                    Serial.println(val);
                                
                                    send(msg.set(val),true); // Send new state and request ack back. 
                                  }
                                

                                I got the update in Domoticz in Hardware > Configuration > Children of nodes

                                alt text

                                And I did test also every 3 seconds, all ok.

                                But I don't have the refresh in "main page":

                                alt text

                                I did test removing current to Arduino when Relay was ON. With Arduino off, I did change the state in domoticz to OFF.

                                Because Arduino save previous state in EEPROM, after full start the relay did go ON, in Domoticz got "1" in children (all right, because digitalRead was 1) but main panel shows OFF.

                                I did expect a graphical change...

                                Thank you (and hope that all is clear) :)

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

                                  Hi to all!
                                  To achieve my goal (node that sends relay state after power failure to sync with domoticz), I'm editing the button script.

                                  I have added this line

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

                                  in setup, so I thought it send only one time at startup the current state after power failure.

                                  But..... the relay shutdown/shuton (or viceversa) twice, in fast sequence....

                                  Thank you very much!

                                  S Boots33B 2 Replies Last reply
                                  0
                                  • S sineverba

                                    Hi to all!
                                    To achieve my goal (node that sends relay state after power failure to sync with domoticz), I'm editing the button script.

                                    I have added this line

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

                                    in setup, so I thought it send only one time at startup the current state after power failure.

                                    But..... the relay shutdown/shuton (or viceversa) twice, in fast sequence....

                                    Thank you very much!

                                    S Offline
                                    S Offline
                                    sineverba
                                    Hardware Contributor
                                    wrote on last edited by
                                    #48

                                    @sineverba

                                    Bingo!

                                    If I did understand well, the actuator sketch DOESN'T directly acts the relay, but sends to the gateway and expect from the gw itself the "new state" (in effect, you short GND & PIN 4, arduino sends new state to the gateway and gateway resend the new state).

                                    So, 'cause I would to update the state on controller, I did this simple edit on setup:

                                    // Set relay to last known state (using eeprom storage) 
                                     state = loadState(CHILD_ID);
                                     digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                    
                                     Serial.print("Lo stato ultimo era ");
                                     Serial.println(state);
                                    
                                     if ( state == 1) {
                                       state = 0;
                                     } else {
                                       state = 1;
                                     }
                                     
                                     send(msg.set(state?false:true), true); // Send new state and request ack back
                                    

                                    Basically, If relay was OFF on a power failure, and someone on Domoticz try to start the relay, the state in Domoticz will be "on".

                                    When power returns, the arduino read the state (in my example OFF == 0), convert in 1 and send to the gateway the 1. The gateway // Domoticz will be answer with 0.

                                    So, relay doesn't nothing and Domoticz state is update.

                                    But.... why I need to invert the logic? :D

                                    1 Reply Last reply
                                    0
                                    • S sineverba

                                      Hi to all!
                                      To achieve my goal (node that sends relay state after power failure to sync with domoticz), I'm editing the button script.

                                      I have added this line

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

                                      in setup, so I thought it send only one time at startup the current state after power failure.

                                      But..... the relay shutdown/shuton (or viceversa) twice, in fast sequence....

                                      Thank you very much!

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

                                      @sineverba said in ๐Ÿ’ฌ Relay:

                                      To achieve my goal (node that sends relay state after power failure to sync with domoticz)

                                      Have a look at the sketch I used for my Synchronising Light switch you may be able to use some of that code perhaps.

                                      S 1 Reply Last reply
                                      1
                                      • Boots33B Boots33

                                        @sineverba said in ๐Ÿ’ฌ Relay:

                                        To achieve my goal (node that sends relay state after power failure to sync with domoticz)

                                        Have a look at the sketch I used for my Synchronising Light switch you may be able to use some of that code perhaps.

                                        S Offline
                                        S Offline
                                        sineverba
                                        Hardware Contributor
                                        wrote on last edited by
                                        #50

                                        @Boots33

                                        Never seen the request function.
                                        You are hero-member for a reason...
                                        Thank you very much! :) :)

                                        Boots33B 1 Reply Last reply
                                        1
                                        • S sineverba

                                          @Boots33

                                          Never seen the request function.
                                          You are hero-member for a reason...
                                          Thank you very much! :) :)

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

                                          @sineverba said in ๐Ÿ’ฌ Relay:

                                          Never seen the request function.

                                          You can find request and other useful info on the API page.
                                          Hope you are enjoying your MySensors journey :)

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


                                          15

                                          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