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. Development
  3. Multi Button Relay Sketch
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store

Multi Button Relay Sketch

Scheduled Pinned Locked Moved Development
69 Posts 12 Posters 25.2k Views 18 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.
  • rayanR rayan

    @Boots33
    ack is important , so we can not found any solution for this ? for exapmle add a code to this sketch , that when radio is disconnect so without ack work and when radio is ok this way stop and with ack work again.
    this idea is for when a radio have problem or fail or other reason... so this light or other divece dont stop working and work handy when radio go to correct or change radio....

    if you can change your code and add this option , so your sketch is best code for light control

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

    @rayan said:

    @Boots33
    ack is important , so we can not found any solution for this ? for exapmle add a code to this sketch , that when radio is disconnect so without ack work and when radio is ok this way stop and with ack work again.
    this idea is for when a radio have problem or fail or other reason... so this light or other divece dont stop working and work handy when radio go to correct or change radio....

    if you can change your code and add this option , so your sketch is best code for light control

    There have been a few threads about the issue of nodes not running the loop part of the sketch if a connection to the gateway cannot be established. Have a look Here and Here for a bit more info. It is a feature I am sure many would like to see in the next version of MySensors. For the moment MySensors does not support 100% what you want to do.

    If you want to combine the two versions of the sketches you will need to have a method to check whether a connection to the gateway is present. You could try something like this in the loop.

    
    if (isTransportOK()){
    
        // put code to execute when connection is available here
    
      } 
      else {
    
        // put code to execute when no connection is available here
    
      }
    
    

    You will be adding a fair bit of complexity to the sketch. Can you tell me why the ACK is so important for you.

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

      What is ACK ?

      And can you show me all code with your if transport is ok to send update status to gateway...but if transport is wrong example try send every 1 minut and stop when send is good.

      I am begginner sorry

      Did you know any info that this function will be implement in next version as you write in latest your post ?

      1 Reply Last reply
      0
      • Boots33B Boots33

        @rayan said:

        @Boots33
        ack is important , so we can not found any solution for this ? for exapmle add a code to this sketch , that when radio is disconnect so without ack work and when radio is ok this way stop and with ack work again.
        this idea is for when a radio have problem or fail or other reason... so this light or other divece dont stop working and work handy when radio go to correct or change radio....

        if you can change your code and add this option , so your sketch is best code for light control

        There have been a few threads about the issue of nodes not running the loop part of the sketch if a connection to the gateway cannot be established. Have a look Here and Here for a bit more info. It is a feature I am sure many would like to see in the next version of MySensors. For the moment MySensors does not support 100% what you want to do.

        If you want to combine the two versions of the sketches you will need to have a method to check whether a connection to the gateway is present. You could try something like this in the loop.

        
        if (isTransportOK()){
        
            // put code to execute when connection is available here
        
          } 
          else {
        
            // put code to execute when no connection is available here
        
          }
        
        

        You will be adding a fair bit of complexity to the sketch. Can you tell me why the ACK is so important for you.

        rayanR Offline
        rayanR Offline
        rayan
        wrote on last edited by rayan
        #43

        @Boots33
        ack is a feedback . is this true ? so when i am out of home and i want turn on light , so ack show me that light is on or no ! also when there is a problem in connection and ack dont receive, so node search and found a new way for connection.
        now i dont have a arduino and radio for test :) but do you think this code will be ok ?without else.

        void loop()
        {
         if (!isTransportOK())
        {
        if (...)            //if change switch to up or down
        ...                 //change state of relay
        if(isTransportOK())
        send(msg.set(state ? true : false), true);  // (last state)
        }
          
          debouncer.update();
          // Get the update value
          int value = debouncer.read();
          if (value != oldValue && value == 0) {
            send(msg.set(state ? false : true), true); // Send new state and request ack back
          }
          oldValue = value;
        }
        

        (i can not work with Bounce2 library :) ) please help to fill lines
        in loop we add a "if (!isTransportOK())" and write when change switch (on/off) state of relay change and this is repeat until connection will be ok ! so send a ack and for continue follow your code...
        is this right ? can you do this change ?

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

          Hi
          I can test it but i must have finall code because i dont know what code i must put in (...). I am begginer user and i dont know how write programming.

          rayanR 1 Reply Last reply
          0
          • pepsonP pepson

            Hi
            I can test it but i must have finall code because i dont know what code i must put in (...). I am begginer user and i dont know how write programming.

            rayanR Offline
            rayanR Offline
            rayan
            wrote on last edited by
            #45

            @pepson

            @Boots33 can solve this maybe because i dont work with Bounce2 lib.

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

              Ok but can you give me ready code which i must put in code to my relays and then i test it.

              rayanR 1 Reply Last reply
              0
              • pepsonP pepson

                Ok but can you give me ready code which i must put in code to my relays and then i test it.

                rayanR Offline
                rayanR Offline
                rayan
                wrote on last edited by
                #47

                @pepson
                i dont have a ready sketch! wait to boots answer

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

                  Ok i am waiting...

                  1 Reply Last reply
                  0
                  • Boots33B Offline
                    Boots33B Offline
                    Boots33
                    Hero Member
                    wrote on last edited by Boots33
                    #49

                    Guys I feel this thread has wandered a long way from the original topic and has taken up a lot of time. i really don't think there is a lot to be gained by turning the fairly simple relay sketch into what you now desire.

                    Even with the changes it still will not work if it boots up and can't find the gateway.

                    I am posting a final sketch which you may like to try, again it is untested so you may still need to work on it. I hope you understand I have a few of my own uncompleted projects that I want to get back to with the limited free time I have. Good luck with your MySensors journey.

                    #define MY_DEBUG
                    #define MY_RADIO_NRF24
                    #define MY_REPEATER_FEATURE
                    
                    #include <SPI.h>
                    #include <MySensors.h>
                    #include <Bounce2.h>
                    
                    #define RELAY_ON 1
                    #define RELAY_OFF 0
                    
                    #define SSR_A_ID 1   // Id of the sensor child
                    #define SSR_B_ID 2   // Id of the sensor child
                    
                    const int buttonPinA = 18;
                    const int buttonPinB = 19;
                    const int relayPinA = 5;
                    const int relayPinB = 6;
                    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
                    // Then set relay pins in output mode
                      pinMode(relayPinA, OUTPUT);
                      pinMode(relayPinB, OUTPUT);
                    
                      // 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);
                      
                    
                        /*--------------------- Added these lines for toggle switch-------------------------*/
                      oldValueA = digitalRead(buttonPinA);  // set oldValueA to the current status of the toggle switch
                      oldValueB = digitalRead(buttonPinB);  // set oldValueB to the current status of the toggle switch
                     // 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
                      
                    }
                    
                    void presentation()  {
                      // Send the sketch version information to the gateway and Controller
                      sendSketchInfo("Mains Controller", "1.0");
                    
                      // 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;
                    }
                    
                    
                    if (isTransportOK()){
                       // put code to execute when connection is available here
                        connectionPresent();
                    
                      } 
                      else {
                       // put code to execute when no connection is available here
                       connectionLost();
                      }
                    
                      
                    }
                    
                    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());
                      }
                    }
                    
                    
                    
                    void connectionPresent() {
                    // put code to execute when connection is available here
                         debouncerA.update();
                      // Get the update value
                      int valueA = debouncerA.read();
                      if (valueA != oldValueA) {
                        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) {
                        send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                      oldValueB = valueB;
                      } 
                    }
                    
                    
                    
                      void connectionLost() {
                    
                       // put code to execute when no connection is available here
                        debouncerA.update();
                      // Get the update value
                      int valueA = debouncerA.read();
                      if (valueA != oldValueA) {
                       // send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
                        send(msgA.set(stateA ? false : true), false); // Send new state with no request for ack
                        stateA = stateA ? false : true;               // invert the state
                         digitalWrite(relayPinA, stateA ? RELAY_ON : RELAY_OFF);  // toggle the relay
                      oldValueA = valueA;
                      }
                     
                    
                      debouncerB.update();
                      // Get the update value
                      int valueB = debouncerB.read();
                      if (valueB != oldValueB) {
                        //send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                        send(msgB.set(stateB ? false : true), false); // Send new state with no request for ack
                        stateB = stateB ? false : true;                // invert the state
                         digitalWrite(relayPinB, stateB ? RELAY_ON : RELAY_OFF);  // toggle the relay
                        
                      oldValueB = valueB;
                      }
                    }
                    
                    
                    rayanR 2 Replies Last reply
                    0
                    • pepsonP Offline
                      pepsonP Offline
                      pepson
                      wrote on last edited by
                      #50

                      Still not working correct. After lost connection with gateway and then change status , after connection restore , but status is still not update in Domoticz. Still show before lost connection.

                      1 Reply Last reply
                      0
                      • Boots33B Boots33

                        Guys I feel this thread has wandered a long way from the original topic and has taken up a lot of time. i really don't think there is a lot to be gained by turning the fairly simple relay sketch into what you now desire.

                        Even with the changes it still will not work if it boots up and can't find the gateway.

                        I am posting a final sketch which you may like to try, again it is untested so you may still need to work on it. I hope you understand I have a few of my own uncompleted projects that I want to get back to with the limited free time I have. Good luck with your MySensors journey.

                        #define MY_DEBUG
                        #define MY_RADIO_NRF24
                        #define MY_REPEATER_FEATURE
                        
                        #include <SPI.h>
                        #include <MySensors.h>
                        #include <Bounce2.h>
                        
                        #define RELAY_ON 1
                        #define RELAY_OFF 0
                        
                        #define SSR_A_ID 1   // Id of the sensor child
                        #define SSR_B_ID 2   // Id of the sensor child
                        
                        const int buttonPinA = 18;
                        const int buttonPinB = 19;
                        const int relayPinA = 5;
                        const int relayPinB = 6;
                        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
                        // Then set relay pins in output mode
                          pinMode(relayPinA, OUTPUT);
                          pinMode(relayPinB, OUTPUT);
                        
                          // 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);
                          
                        
                            /*--------------------- Added these lines for toggle switch-------------------------*/
                          oldValueA = digitalRead(buttonPinA);  // set oldValueA to the current status of the toggle switch
                          oldValueB = digitalRead(buttonPinB);  // set oldValueB to the current status of the toggle switch
                         // 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
                          
                        }
                        
                        void presentation()  {
                          // Send the sketch version information to the gateway and Controller
                          sendSketchInfo("Mains Controller", "1.0");
                        
                          // 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;
                        }
                        
                        
                        if (isTransportOK()){
                           // put code to execute when connection is available here
                            connectionPresent();
                        
                          } 
                          else {
                           // put code to execute when no connection is available here
                           connectionLost();
                          }
                        
                          
                        }
                        
                        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());
                          }
                        }
                        
                        
                        
                        void connectionPresent() {
                        // put code to execute when connection is available here
                             debouncerA.update();
                          // Get the update value
                          int valueA = debouncerA.read();
                          if (valueA != oldValueA) {
                            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) {
                            send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                          oldValueB = valueB;
                          } 
                        }
                        
                        
                        
                          void connectionLost() {
                        
                           // put code to execute when no connection is available here
                            debouncerA.update();
                          // Get the update value
                          int valueA = debouncerA.read();
                          if (valueA != oldValueA) {
                           // send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
                            send(msgA.set(stateA ? false : true), false); // Send new state with no request for ack
                            stateA = stateA ? false : true;               // invert the state
                             digitalWrite(relayPinA, stateA ? RELAY_ON : RELAY_OFF);  // toggle the relay
                          oldValueA = valueA;
                          }
                         
                        
                          debouncerB.update();
                          // Get the update value
                          int valueB = debouncerB.read();
                          if (valueB != oldValueB) {
                            //send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                            send(msgB.set(stateB ? false : true), false); // Send new state with no request for ack
                            stateB = stateB ? false : true;                // invert the state
                             digitalWrite(relayPinB, stateB ? RELAY_ON : RELAY_OFF);  // toggle the relay
                            
                          oldValueB = valueB;
                          }
                        }
                        
                        
                        rayanR Offline
                        rayanR Offline
                        rayan
                        wrote on last edited by rayan
                        #51
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • Boots33B Boots33

                          Guys I feel this thread has wandered a long way from the original topic and has taken up a lot of time. i really don't think there is a lot to be gained by turning the fairly simple relay sketch into what you now desire.

                          Even with the changes it still will not work if it boots up and can't find the gateway.

                          I am posting a final sketch which you may like to try, again it is untested so you may still need to work on it. I hope you understand I have a few of my own uncompleted projects that I want to get back to with the limited free time I have. Good luck with your MySensors journey.

                          #define MY_DEBUG
                          #define MY_RADIO_NRF24
                          #define MY_REPEATER_FEATURE
                          
                          #include <SPI.h>
                          #include <MySensors.h>
                          #include <Bounce2.h>
                          
                          #define RELAY_ON 1
                          #define RELAY_OFF 0
                          
                          #define SSR_A_ID 1   // Id of the sensor child
                          #define SSR_B_ID 2   // Id of the sensor child
                          
                          const int buttonPinA = 18;
                          const int buttonPinB = 19;
                          const int relayPinA = 5;
                          const int relayPinB = 6;
                          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
                          // Then set relay pins in output mode
                            pinMode(relayPinA, OUTPUT);
                            pinMode(relayPinB, OUTPUT);
                          
                            // 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);
                            
                          
                              /*--------------------- Added these lines for toggle switch-------------------------*/
                            oldValueA = digitalRead(buttonPinA);  // set oldValueA to the current status of the toggle switch
                            oldValueB = digitalRead(buttonPinB);  // set oldValueB to the current status of the toggle switch
                           // 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
                            
                          }
                          
                          void presentation()  {
                            // Send the sketch version information to the gateway and Controller
                            sendSketchInfo("Mains Controller", "1.0");
                          
                            // 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;
                          }
                          
                          
                          if (isTransportOK()){
                             // put code to execute when connection is available here
                              connectionPresent();
                          
                            } 
                            else {
                             // put code to execute when no connection is available here
                             connectionLost();
                            }
                          
                            
                          }
                          
                          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());
                            }
                          }
                          
                          
                          
                          void connectionPresent() {
                          // put code to execute when connection is available here
                               debouncerA.update();
                            // Get the update value
                            int valueA = debouncerA.read();
                            if (valueA != oldValueA) {
                              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) {
                              send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                            oldValueB = valueB;
                            } 
                          }
                          
                          
                          
                            void connectionLost() {
                          
                             // put code to execute when no connection is available here
                              debouncerA.update();
                            // Get the update value
                            int valueA = debouncerA.read();
                            if (valueA != oldValueA) {
                             // send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
                              send(msgA.set(stateA ? false : true), false); // Send new state with no request for ack
                              stateA = stateA ? false : true;               // invert the state
                               digitalWrite(relayPinA, stateA ? RELAY_ON : RELAY_OFF);  // toggle the relay
                            oldValueA = valueA;
                            }
                           
                          
                            debouncerB.update();
                            // Get the update value
                            int valueB = debouncerB.read();
                            if (valueB != oldValueB) {
                              //send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                              send(msgB.set(stateB ? false : true), false); // Send new state with no request for ack
                              stateB = stateB ? false : true;                // invert the state
                               digitalWrite(relayPinB, stateB ? RELAY_ON : RELAY_OFF);  // toggle the relay
                              
                            oldValueB = valueB;
                            }
                          }
                          
                          
                          rayanR Offline
                          rayanR Offline
                          rayan
                          wrote on last edited by
                          #52

                          @Boots33
                          hi my friend.
                          i use your sketch and thank you.but there is a problem.
                          sketchs in site , when relay is fail , when i send command , so show to me "Error sending switch command, check device/hardware !" but your sketch dont show this error. so i dont understand relay is fail or no , when we are out home. can you fix this ?

                          1 Reply Last reply
                          0
                          • korttomaK korttoma

                            Here is an example that I'm using with 4 relays and 4 monostable buttons:

                            // Enable debug prints to serial monitor
                            #define MY_DEBUG
                            
                            // Enable and select radio type attached
                            #define MY_RADIO_NRF24
                            
                            #define SN "RelayButtonArray"
                            #define SV "1.0"
                            
                            #include <MySensors.h>
                            #include <SPI.h>
                            #include <Bounce2.h>
                            #define RELAY_ON 0                      // switch around for ACTIVE LOW / ACTIVE HIGH relay
                            #define RELAY_OFF 1
                            //
                            
                            #define noRelays 4                     //2-4
                            const int relayPin[] = {14, 15, 16, 17};       //  switch around pins to your desire
                            const int buttonPin[] = {6, 7, 4, 5};   //  switch around pins to your desire
                            
                            class Relay             // relay class, store all relevant data (equivalent to struct)
                            {
                              public:
                                int buttonPin;                   // physical pin number of button
                                int relayPin;             // physical pin number of relay
                                boolean relayState;               // relay status (also stored in EEPROM)
                            };
                            
                            Relay Relays[noRelays];
                            Bounce debouncer[noRelays];
                            MyMessage msg[noRelays];
                            
                            /*
                              void before() {
                              for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                                // Then set relay pins in output mode
                                pinMode(pin, OUTPUT);
                                // Set relay to last known state (using eeprom storage)
                                digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
                              }
                              }*/
                            
                            void setup() {
                              wait(100);
                              // Initialize Relays with corresponding buttons
                              for (int i = 0; i < noRelays; i++) {
                                Relays[i].buttonPin = buttonPin[i];              // assign physical pins
                                Relays[i].relayPin = relayPin[i];
                                msg[i].sensor = i;                                   // initialize messages
                                msg[i].type = V_LIGHT;
                                pinMode(Relays[i].buttonPin, INPUT_PULLUP);
                                wait(100);
                                pinMode(Relays[i].relayPin, OUTPUT);
                                Relays[i].relayState = loadState(i);                               // retrieve last values from EEPROM
                                digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
                                send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
                                wait(50);
                                debouncer[i] = Bounce();                        // initialize debouncer
                                debouncer[i].attach(buttonPin[i]);
                                debouncer[i].interval(30);
                                wait(50);
                              }
                            }
                            
                            void presentation()
                            {
                              // Send the sketch version information to the gateway and Controller
                              sendSketchInfo(SN, SV);
                            
                              wait(100);
                            
                              for (int i = 0; i < noRelays; i++)
                                present(i, S_LIGHT);                               // present sensor to gateway
                            
                              wait(100);
                            }
                            
                            
                            void loop()
                            {
                              for (byte i = 0; i < noRelays; i++) {
                                if (debouncer[i].update()) {
                                  
                                  int value = debouncer[i].read();
                                  
                                  if ( value == LOW) {
                                    Relays[i].relayState = !Relays[i].relayState;
                                    digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
                                    send(msg[i].set(Relays[i].relayState ? true : false));
                                    // save sensor state in EEPROM (location == sensor number)
                                    saveState( i, Relays[i].relayState );
                            
                                  }
                            
                                }
                              }
                              //wait(20);
                            }
                            
                            void receive(const MyMessage &message) {
                              if (message.type == V_LIGHT) {
                                if (message.sensor < noRelays) {          // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
                                  Relays[message.sensor].relayState = message.getBool();
                                  digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
                                  saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
                                }
                              }
                              wait(20);
                            }
                            
                            breimannB Offline
                            breimannB Offline
                            breimann
                            wrote on last edited by
                            #53

                            @korttoma love this sketch. Used it for a dual LED strip light controller for our loft where our kids sleep - daughter on one side and boys on the other, both with LED strips (RGB) i installed. This way we can turn them on or off remotely without having to climb up there!!

                            Worked great. Thankyou for sharing.

                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              moskovskiy82
                              wrote on last edited by
                              #54

                              Anybody has a sketch where multiple buttons are put up on single wire and voltage drop is used to distinguish between them?

                              1 Reply Last reply
                              0
                              • nieatyN Offline
                                nieatyN Offline
                                nieaty
                                wrote on last edited by
                                #55
                                This post is deleted!
                                1 Reply Last reply
                                1
                                • pepsonP Offline
                                  pepsonP Offline
                                  pepson
                                  wrote on last edited by
                                  #56

                                  Hi
                                  Has anybody solution how resolved problem update status when connection with gateway is not available and then come back connection with gateway?

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

                                    Hi
                                    Has anybody solution how resolved problem update status when connection with gateway is not available and then come back connection with gateway?

                                    Boots33B 1 Reply Last reply
                                    0
                                    • pepsonP pepson

                                      Hi
                                      Has anybody solution how resolved problem update status when connection with gateway is not available and then come back connection with gateway?

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

                                      @pepson have a look at this Synchronising Light switch

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

                                        I try sketch from your link but i test and the same issu.
                                        I enable relay, then disable GATEWAY MySensors on DOmoticz and then change status relay to DISABLE by button. Then enable Gateway on Domoticz and still in domoticz show that relay is ENABLED. Not update status that i manual DISABLE relay by button.

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

                                          This is my sketch with my radio and this not working... no sync status after lost connection with gateway and after come back gateway...

                                          /*
                                             Relay with button sketch
                                             modified to work with no uplink
                                             to gateway and try to maintain sync to controller
                                          */
                                          
                                          
                                          #define MY_DEBUG                               // Enable debug prints to serial monitor
                                          
                                          #define MY_RADIO_RFM69
                                          #define MY_RFM69_FREQUENCY RF69_868MHZ
                                          #define MY_IS_RFM69HW                         // Enable and select radio type attached
                                          
                                          #define MY_TRANSPORT_WAIT_READY_MS 5000        //set how long to wait for transport ready in milliseconds
                                          
                                          // 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 10
                                          
                                          #include <MySensors.h>
                                          #include <Bounce2.h>
                                          
                                          #define RELAY_PIN  5      // Arduino Digital I/O pin number for relay 
                                          #define BUTTON_PIN  3     // Arduino Digital I/O pin number for button 
                                          #define CHILD_ID 1        // Id of the sensor child
                                          #define RELAY_ON 0
                                          #define RELAY_OFF 1
                                          
                                          Bounce debouncer = Bounce();
                                          int oldValue = 0;
                                          bool uplinkAvailable = true;
                                          bool state = false;
                                          bool requestState;
                                          bool firstStart = true;
                                          unsigned long uplinkCheckTime ;                // holder for uplink checks
                                          unsigned long uplinkCheckPeriod = 30*1000;     // time between checks for uplink in milliseconds
                                          unsigned long returnWait = 1500;               // how long to wait for return from controller in milliseconds
                                          MyMessage msg(CHILD_ID, V_STATUS);
                                          
                                          void setup(){
                                            pinMode(BUTTON_PIN, INPUT_PULLUP);           // Setup the button pin, Activate internal pull-up
                                            
                                            debouncer.attach(BUTTON_PIN);                // After setting up the button, setup debouncer
                                            debouncer.interval(5);
                                          
                                            pinMode(RELAY_PIN, OUTPUT);                 // set relay pin in output mode
                                            digitalWrite(RELAY_PIN, RELAY_OFF);         // Make sure relay is off when starting up
                                          }
                                          
                                          void presentation()  {
                                            // Send the sketch version information to the gateway and Controller
                                            sendSketchInfo("Relay & Button", "1.0");
                                          
                                            // Register all sensors to gw (they will be created as child devices)
                                            present(CHILD_ID, S_BINARY);
                                          }
                                          
                                          
                                          void loop(){
                                            if (firstStart) {                            // this code is only run once at startup
                                              Serial.println("First run started");
                                              if (request( CHILD_ID, V_STATUS)) {       // request the current state of the switch on the controller and check that an ack was received
                                                Serial.println("uplink available");
                                                wait (returnWait);                      //wait needed to allow request to return from controller
                                                Serial.print("controller state --- ");
                                                Serial.println(requestState);
                                                if (requestState != state) {           // check that controller is corectly showing the current relay state
                                                  send(msg.set(state), false);         // notify controller of current state
                                                }
                                              }
                                              else {
                                                Serial.println("uplink not available");
                                                uplinkAvailable = false;               // no uplink established
                                                uplinkCheckTime = millis();
                                              }
                                              firstStart = false;                     // set firstStart flag false to prevent code from running again
                                            }
                                          
                                            debouncer.update();
                                            int value = debouncer.read();                               // Get the update value
                                            if (value != oldValue && value == 0) {                      // check for new button push
                                              state =  !state;                                          // Toggle the state
                                              digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state
                                              if (!send(msg.set(state), true)) {                        //Attempt to notify controller of changed state
                                                Serial.println("uplink not available");
                                                uplinkAvailable = false;                                // no uplink available
                                                uplinkCheckTime = millis();                             
                                              }
                                            }
                                           oldValue = value;
                                          
                                            if (!uplinkAvailable && (millis() - uplinkCheckTime > uplinkCheckPeriod) ) {       // test to see if function should be called
                                              uplinkCheck();                                                                  // call uplink checking function
                                            }
                                          
                                          }
                                          
                                          /*-------------------start of functions--------------------------*/
                                          
                                          void receive(const MyMessage &message) {
                                            if (message.type == V_STATUS) {                                   // check to see if incoming message is for a switch
                                              switch (message.getCommand()) {                                 // message.getCommand will give us the command type of the incomming message
                                                case C_SET:                                                   //message is a set command  from controller to update relay state
                                                  state = message.getBool();                                  // get the new state
                                                  digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to new state
                                                  uplinkAvailable = true;                                     //  uplink established
                                                  /*---- Write some debug info----*/
                                                  Serial.print("Incoming change for sensor:");
                                                  Serial.print(message.sensor);
                                                  Serial.print(", New status: ");
                                                  Serial.println(message.getBool());
                                                  break;
                                                case C_REQ:                                               // message is a returning request from controller
                                                  requestState = message.getBool();                       // update requestState with returning state
                                                  break;
                                              }
                                            }
                                          }
                                          
                                          void uplinkCheck() {
                                            if (request( CHILD_ID, V_STATUS)) {         // request the current state of the switch on the controller and check that the request was succsessful
                                              Serial.println("uplink re-established");
                                              wait (returnWait);                        //wait needed to allow request to return from controller
                                              if (requestState != state) {              // check that controller is corectly showing the current relay state
                                                send(msg.set(state), false);            // notify controller of current state no ack
                                                uplinkAvailable = true;                 //  uplink established
                                              }
                                            }
                                            uplinkCheckTime = millis();                // reset the checktime
                                            Serial.println("uplinkchecktime reset");
                                          }```
                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          8

                                          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