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.
  • 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
                • fhenrycoF Offline
                  fhenrycoF Offline
                  fhenryco
                  wrote on last edited by
                  #52

                  Hello,

                  since the relay has leds and is therefore power consuming, i was wondering if someone has written a sketch for a low power usage of the relay (as for the sensors) allowing the arduino (and the relay) to sleep all the time and wake up every 5 minutes for instance just to check if the controller has changed the desired state of the switch.

                  Of course then the relay could be as late as 5minute, but this is not an issue for my application ...

                  thanks for any suggestion

                  mfalkviddM 1 Reply Last reply
                  0
                  • fhenrycoF fhenryco

                    Hello,

                    since the relay has leds and is therefore power consuming, i was wondering if someone has written a sketch for a low power usage of the relay (as for the sensors) allowing the arduino (and the relay) to sleep all the time and wake up every 5 minutes for instance just to check if the controller has changed the desired state of the switch.

                    Of course then the relay could be as late as 5minute, but this is not an issue for my application ...

                    thanks for any suggestion

                    mfalkviddM Offline
                    mfalkviddM Offline
                    mfalkvidd
                    Mod
                    wrote on last edited by
                    #53

                    @fhenryco can't the relay node use power from whatever it is controlling?

                    For sleeping nodes, the smartsleep feature can be useful. Search for that keyword in the forum and look at https://www.mysensors.org/download/sensor_api_20#sleeping

                    fhenrycoF 1 Reply Last reply
                    0
                    • fhenrycoF Offline
                      fhenrycoF Offline
                      fhenryco
                      wrote on last edited by
                      #54

                      thanks , indeed it's because the relay is controlling a 12V battery power to solenoid valve , that low power consumption is needed. BTW the 12V is obtained from a 4S lipo (16.5V max voltage) and a 12 regulator and i dont know if its better to feed the arduino from a 5V regulator fed by the same Lipo , or use an additional set of AA batteries ... i remember that it's better to have an as low as possible drop in voltage to save power...

                      Boots33B 1 Reply Last reply
                      0
                      • fhenrycoF fhenryco

                        thanks , indeed it's because the relay is controlling a 12V battery power to solenoid valve , that low power consumption is needed. BTW the 12V is obtained from a 4S lipo (16.5V max voltage) and a 12 regulator and i dont know if its better to feed the arduino from a 5V regulator fed by the same Lipo , or use an additional set of AA batteries ... i remember that it's better to have an as low as possible drop in voltage to save power...

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

                        @fhenryco If you are trying to keep your power use down then you could also consider using either a Latching Relay or even a MOSFET.

                        gohanG 1 Reply Last reply
                        0
                        • mfalkviddM mfalkvidd

                          @fhenryco can't the relay node use power from whatever it is controlling?

                          For sleeping nodes, the smartsleep feature can be useful. Search for that keyword in the forum and look at https://www.mysensors.org/download/sensor_api_20#sleeping

                          fhenrycoF Offline
                          fhenrycoF Offline
                          fhenryco
                          wrote on last edited by
                          #56

                          @mfalkvidd smartsleep was not supported by domoticz at the begining of the year ... but there was a domoticz upgrade in july ... do you know if it's now supported ?

                          1 Reply Last reply
                          0
                          • Boots33B Boots33

                            @fhenryco If you are trying to keep your power use down then you could also consider using either a Latching Relay or even a MOSFET.

                            gohanG Offline
                            gohanG Offline
                            gohan
                            Mod
                            wrote on last edited by
                            #57

                            @Boots33 said in ๐Ÿ’ฌ Relay:

                            @fhenryco If you are trying to keep your power use down then you could also consider using either a Latching Relay or even a MOSFET.

                            Agreed, if you want to save power you need to use a latching relay. Also another regulator would be fine to power the arduino.

                            fhenrycoF 1 Reply Last reply
                            0
                            • gohanG gohan

                              @Boots33 said in ๐Ÿ’ฌ Relay:

                              @fhenryco If you are trying to keep your power use down then you could also consider using either a Latching Relay or even a MOSFET.

                              Agreed, if you want to save power you need to use a latching relay. Also another regulator would be fine to power the arduino.

                              fhenrycoF Offline
                              fhenrycoF Offline
                              fhenryco
                              wrote on last edited by
                              #58

                              @gohan can you explain the benefit (for a low power app) of a relay switching on electrical pulse rather something else ?
                              i mean, even the regular relay has both an open by default circuit output and close by default circuit ouput so it could remain on the desired state even when not powered up ...

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

                                I am referring that when relay is on it is drawing power to stay on, while a latching relay it only draws power during the switch on and only during the switch off.

                                fhenrycoF 1 Reply Last reply
                                0
                                • gohanG gohan

                                  I am referring that when relay is on it is drawing power to stay on, while a latching relay it only draws power during the switch on and only during the switch off.

                                  fhenrycoF Offline
                                  fhenrycoF Offline
                                  fhenryco
                                  wrote on last edited by
                                  #60

                                  @gohan not sure this is going to be an easy way: need find arduino code for latch relay, dont know if domoticz is supporting this device ?, ...

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

                                    Domoticz will just see the same relay node, it is the code that need to be a little different as the relay is switched with a pulse and you need a pin to verify the relay status (if you get the ones that have a monitoring contact)

                                    1 Reply Last reply
                                    0
                                    • amemo06A Offline
                                      amemo06A Offline
                                      amemo06
                                      wrote on last edited by
                                      #62

                                      Hi Everyone,
                                      maybe the below will help people facing random incorrect behaviors.
                                      In sample from december 2016 /mysensors/MySensors/examples/RelayActuator/RelayActuator.ino, there is something which could be seen as a bug.

                                      In my case, I have used that sample to control a lot of relais. I noticed some rare but really annoying errors. From time to time a relay got switched incorrectly.
                                      Annoying and strange as coding was pretty straightforward. All relais are coded to act in pulse mode.

                                                digitalWrite(message.sensor-1+RELAY_1, RELAY_OFF);
                                                wait(1000);
                                                digitalWrite(message.sensor-1+RELAY_1, RELAY_ON);
                                      

                                      And this is the reason of the bug.

                                      Explanation.
                                      During that second of wait, a lot of things may occurs. Due to the 'message' declaration, it is possible that the 'message' is getting changed.
                                      Typically a ping may come and as result the 'message' is not anymore the same.

                                      For that reason I would suggest the code to be changed a little bit.

                                      void receive(const MyMessage &message_orig)
                                      {
                                        MyMessage message;
                                        message = message_orig;
                                      

                                      This way, the function works with a local copy and you are sure nothing can change during the processing.

                                      Anyway, thanks for the sample it was nevertheless very helpful

                                      BulldogLowellB gohanG 2 Replies Last reply
                                      2
                                      • amemo06A amemo06

                                        Hi Everyone,
                                        maybe the below will help people facing random incorrect behaviors.
                                        In sample from december 2016 /mysensors/MySensors/examples/RelayActuator/RelayActuator.ino, there is something which could be seen as a bug.

                                        In my case, I have used that sample to control a lot of relais. I noticed some rare but really annoying errors. From time to time a relay got switched incorrectly.
                                        Annoying and strange as coding was pretty straightforward. All relais are coded to act in pulse mode.

                                                  digitalWrite(message.sensor-1+RELAY_1, RELAY_OFF);
                                                  wait(1000);
                                                  digitalWrite(message.sensor-1+RELAY_1, RELAY_ON);
                                        

                                        And this is the reason of the bug.

                                        Explanation.
                                        During that second of wait, a lot of things may occurs. Due to the 'message' declaration, it is possible that the 'message' is getting changed.
                                        Typically a ping may come and as result the 'message' is not anymore the same.

                                        For that reason I would suggest the code to be changed a little bit.

                                        void receive(const MyMessage &message_orig)
                                        {
                                          MyMessage message;
                                          message = message_orig;
                                        

                                        This way, the function works with a local copy and you are sure nothing can change during the processing.

                                        Anyway, thanks for the sample it was nevertheless very helpful

                                        BulldogLowellB Offline
                                        BulldogLowellB Offline
                                        BulldogLowell
                                        Contest Winner
                                        wrote on last edited by
                                        #63

                                        @amemo06

                                        void receive(const MyMessage &message)
                                        

                                        Because of the const keyword this is a const reference or said another way, a reference to a constant. The const keyword guarantees that the function may not change the object.

                                        So, I wonder what your real issue is....

                                        amemo06A 1 Reply Last reply
                                        0
                                        • BulldogLowellB BulldogLowell

                                          @amemo06

                                          void receive(const MyMessage &message)
                                          

                                          Because of the const keyword this is a const reference or said another way, a reference to a constant. The const keyword guarantees that the function may not change the object.

                                          So, I wonder what your real issue is....

                                          amemo06A Offline
                                          amemo06A Offline
                                          amemo06
                                          wrote on last edited by
                                          #64

                                          @BulldogLowell No This is not the coding of the function which changes it. It is most likely the event which triggers it which may changes it.
                                          I can show the coding with trace and trace result.

                                          void receive(const MyMessage &message)
                                          //void receive(const MyMessage &message_orig)
                                          {
                                          //  MyMessage message;
                                          //  message = message_orig;
                                            Serial.println("receive_in:");
                                          Serial.print("1st read:Sensor=");Serial.print(message.sensor);Serial.print(", getBool=");Serial.println(message.getBool());
                                            // We only expect one type of message from controller. But we better check anyway.
                                            if (message.type==V_STATUS) {
                                              // Change relay state
                                              if (message.getBool() == RELAY_ON){
                                                digitalWrite(message.sensor-1+RELAY_1, RELAY_OFF);
                                          Serial.print("2nd read:Sensor=");Serial.print(message.sensor);Serial.print(", getBool=");Serial.println(message.getBool());
                                                wait(1000);
                                                digitalWrite(message.sensor-1+RELAY_1, RELAY_ON);
                                          Serial.print("3rd read:Sensor=");Serial.print(message.sensor);Serial.print(", getBool=");Serial.println(message.getBool());      
                                              }
                                          Serial.println("receive_out:");
                                          }
                                          

                                          Below is a normal trace

                                          0;255;3;0;9;Eth: 0;0;3;0;18;PING
                                          0;255;3;0;9;Eth: 0;1;1;0;2;1
                                          receive_in:
                                          1st read:Sensor=1, getBool=1
                                          2nd read:Sensor=1, getBool=1
                                          3rd read:Sensor=1, getBool=1
                                          receive_out
                                          

                                          So you see the ping which occured before the message I'm interested in
                                          Now, because of the wait in the middle of the code, it may occurs the ping arrive in that period. Then the issue occurs.
                                          See the trace with error.

                                          0;255;3;0;9;Eth: 0;1;1;0;2;1
                                          receive_in:
                                          1st read:Sensor=1, getBool=1
                                          2nd read:Sensor=1, getBool=1
                                          0;255;3;0;9;Eth: 0;0;3;0;18;PING
                                          3rd read:Sensor=0, getBool=0
                                          receive_out
                                          

                                          In the 3rd read, sensor value has changed and getBool returns also a different value.

                                          Removing the wait or ensuring the function modules performs faster could reduce the risk. Still it will never be reliable.
                                          Better is to have either a local copy of the message like I wrote in the original post. Or make immediately copies of the information from the message I'm interested in.

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


                                          12

                                          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