Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Relay with button example sketch, works...but not as expected

Relay with button example sketch, works...but not as expected

Scheduled Pinned Locked Moved Troubleshooting
15 Posts 4 Posters 4.8k Views 2 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.
  • R Offline
    R Offline
    roffy
    wrote on last edited by
    #1

    Hi all, am fairly new to automation but have tinkered with arduino in the past.
    my setup is currently
    VeraLite UI7
    arduino seral gateway (working with no obvious errors)
    1 arduino nanno running door/window/button sketch
    (this is working as expected)

    and the one i have just setup running relayWithButton.

    so, i have no relay to controll i just want my button to be able to activate some scenes in vera.
    the sketch and button are attached and running, added to the vera seemingly ok.
    i can turn on/off the device in the vera UI and can see commands being recieved in serial monitor on the relay node arduino.

    one thing i am stuck on, it seems that my button only changes the device state after the gw has altered it first.
    i.e. if i turn it on in vera, the button when pressed will then turn it off again.
    after that the button becomes useless and i have to turn it back on through the vera UI.

    What am i missing here? is the button behaving as it should or should the button be active (change the device state) every press?

    here is my serial monitor readout of the button press After turning the device on in the vera UI

    Incoming change for sensor:1, New status: 1  //i just turned it on in vera
    send: 2-2-0-0 s=1,c=1,t=2,pt=2,l=2,st=ok:0        // just pressed the button
    read: 0-0-2 s=1,c=1,t=2,pt=2,l=2:0
    This is an ack from gateway
    

    and here is the serial monitor readout when pressing the button again without touching the vera UI

    send: 2-2-0-0 s=1,c=1,t=2,pt=2,l=2,st=ok:0
    read: 0-0-2 s=1,c=1,t=2,pt=2,l=2:0
    This is an ack from gateway
    

    the divice does not change state after the second press.

    Thanks for any help, sorry if im missing something bleeding obvious!

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

      how are you powering the radio and arduino, on sensor side? Post your sensor's sketch too...

      Home Assistant / Vera Plus UI7
      ESP8266 GW + mySensors 2.3.2
      Alexa / Google Home

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roffy
        wrote on last edited by
        #3

        powered from the usb of my PC as i have only uploaded the sketch then went to try it out.
        i was thinking it wasent a power supply problem as the sensor seems to be working ok. no errors or fails in the serial monitor.

        sketch is a direct coppy and past from the example of relay with button. here it is.

        // Example sketch för a "light switch" where you can control light or something 
        // else from both vera and a local physical button (connected between digital
        // pin 3 and GND).
        // This node also works as a repeader for other nodes
        
        #include <MySensor.h>
        #include <SPI.h>
        #include <Bounce2.h>
        
        #define RELAY_PIN  4  // 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 1
        #define RELAY_OFF 0
        
        Bounce debouncer = Bounce(); 
        int oldValue=0;
        bool state;
        MySensor gw;
        MyMessage msg(CHILD_ID,V_LIGHT);
        
        void setup()  
        {  
          gw.begin(incomingMessage, AUTO, true);
        
          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Relay & Button", "1.0");
        
         // Setup the button
          pinMode(BUTTON_PIN,INPUT);
          // Activate internal pull-up
          digitalWrite(BUTTON_PIN,HIGH);
          
          // After setting up the button, setup debouncer
          debouncer.attach(BUTTON_PIN);
          debouncer.interval(5);
        
          // Register all sensors to gw (they will be created as child devices)
          gw.present(CHILD_ID, S_LIGHT);
        
          // Make sure relays are off when starting up
          digitalWrite(RELAY_PIN, RELAY_OFF);
          // Then set relay pins in output mode
          pinMode(RELAY_PIN, OUTPUT);   
              
          // Set relay to last known state (using eeprom storage) 
          state = gw.loadState(CHILD_ID);
          digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
        }
        
        
        /*
        *  Example on how to asynchronously check for new messages from gw
        */
        void loop() 
        {
          gw.process();
          debouncer.update();
          // Get the update value
          int value = debouncer.read();
          if (value != oldValue && value==0) {
              gw.send(msg.set(state?false:true), true); // Send new state and request ack back
          }
          oldValue = value;
        } 
         
        void incomingMessage(const MyMessage &message) {
          // We only expect one type of message from controller. But we better check anyway.
          if (message.isAck()) {
             Serial.println("This is an ack from gateway");
          }
        
          if (message.type == V_LIGHT && strlen(msg.getString()) != 0) {
             // Change relay state
             state = message.getBool();
             digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
             // Store state in eeprom
             gw.saveState(CHILD_ID, state);
            
             // Write some debug info
             Serial.print("Incoming change for sensor:");
             Serial.print(message.sensor);
             Serial.print(", New status: ");
             Serial.println(message.getBool());
           } 
        }
        
        

        does this help at all?

        1 Reply Last reply
        0
        • rvendrameR Offline
          rvendrameR Offline
          rvendrame
          Hero Member
          wrote on last edited by
          #4

          Try changing this line

          if (value != oldValue && value==0) {
          

          to this

          if (value != oldValue) {
          

          Home Assistant / Vera Plus UI7
          ESP8266 GW + mySensors 2.3.2
          Alexa / Google Home

          BulldogLowellB 1 Reply Last reply
          0
          • BulldogLowellB Offline
            BulldogLowellB Offline
            BulldogLowell
            Contest Winner
            wrote on last edited by BulldogLowell
            #5

            @roffy said:

            gw.send(msg.set(state?false:true), true);

            also, are you aware that you are not changing the variable "state" with a button press?

            nor are you affecting the relay, fyi...

            void loop() 
            {
              gw.process();
              debouncer.update();
              // Get the update value
              int value = debouncer.read();
              if (value != oldValue && value==0)
              {
                  state = !state;
                  digitalWrite(RELAY_PIN, state);
                  gw.send(msg.set(state, true)); // Send new state and request ack back
              }
              oldValue = value;
            } 
            
            1 Reply Last reply
            0
            • BulldogLowellB Offline
              BulldogLowellB Offline
              BulldogLowell
              Contest Winner
              wrote on last edited by
              #6

              @roffy said:

              gw.saveState(CHILD_ID, state);

              void loop() 
              {
                gw.process();
                debouncer.update();
                // Get the update value
                int value = debouncer.read();
                if (value != oldValue && value==0)
                {
                    state = !state;
                    digitalWrite(RELAY_PIN, state);
                    gw.saveState(CHILD_ID, state);// forgot to save the new state to EEPROM
                    gw.send(msg.set(state, true)); // Send new state and request ack back
                }
                oldValue = value;
              } 
              

              gw.saveState(CHILD_ID, state);

              1 Reply Last reply
              0
              • rvendrameR rvendrame

                Try changing this line

                if (value != oldValue && value==0) {
                

                to this

                if (value != oldValue) {
                
                BulldogLowellB Offline
                BulldogLowellB Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by
                #7

                @rvendrame said:

                Try changing this line

                if (value != oldValue && value==0) {
                

                to this

                if (value != oldValue) {
                

                I don't think you want to affect a state change on both sides of the button press....

                1 Reply Last reply
                0
                • rvendrameR Offline
                  rvendrameR Offline
                  rvendrame
                  Hero Member
                  wrote on last edited by
                  #8

                  @BulldogLowell said:

                  I don't think you want to affect a state change on both sides of the button press....

                  ... if you are thinking in a pushbutton , yes I agree with you --- But what about a regular on-off switch?

                  Home Assistant / Vera Plus UI7
                  ESP8266 GW + mySensors 2.3.2
                  Alexa / Google Home

                  1 Reply Last reply
                  0
                  • BulldogLowellB Offline
                    BulldogLowellB Offline
                    BulldogLowell
                    Contest Winner
                    wrote on last edited by
                    #9

                    @rvendrame said:

                    @BulldogLowell said:

                    I don't think you want to affect a state change on both sides of the button press....

                    ... if you are thinking in a pushbutton , yes I agree with you --- But what about a regular on-off switch?

                    For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

                    1 Reply Last reply
                    0
                    • rvendrameR Offline
                      rvendrameR Offline
                      rvendrame
                      Hero Member
                      wrote on last edited by
                      #10

                      @BulldogLowell said:

                      For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

                      Why not? I do have the original on/off switch from my lights for example. I want to control them via automation but also via wall switch...

                      Home Assistant / Vera Plus UI7
                      ESP8266 GW + mySensors 2.3.2
                      Alexa / Google Home

                      BulldogLowellB 1 Reply Last reply
                      0
                      • rvendrameR rvendrame

                        @BulldogLowell said:

                        For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

                        Why not? I do have the original on/off switch from my lights for example. I want to control them via automation but also via wall switch...

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

                        @rvendrame said:

                        @BulldogLowell said:

                        For an on/off switch? well you would probably not have that on a device that is also controlled remotely/wirelessly.

                        Why not? I do have the original on/off switch from my lights for example. I want to control them via automation but also via wall switch...

                        yeah, but every wall switch I have, which are integrated into my home automation, are not two position switches. Anyways, the OP has to chime in here... in the rare event he's using a two-position switch, well your code may still need to change. Time to hear from the OP.

                        1 Reply Last reply
                        0
                        • rvendrameR Offline
                          rvendrameR Offline
                          rvendrame
                          Hero Member
                          wrote on last edited by
                          #12

                          @BulldogLowell said:

                          also, are you aware that you are not changing the variable "state" with a button press?

                          Just to mention, state is being changed:

                           if (value != oldValue && value==0)
                            {
                                state = !state;   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                digitalWrite(RELAY_PIN, state);
                                gw.saveState(CHILD_ID, state);// forgot to save the new state to EEPROM
                                gw.send(msg.set(state, true)); // Send new state and request ack back
                            }
                          

                          Home Assistant / Vera Plus UI7
                          ESP8266 GW + mySensors 2.3.2
                          Alexa / Google Home

                          BulldogLowellB 1 Reply Last reply
                          0
                          • rvendrameR rvendrame

                            @BulldogLowell said:

                            also, are you aware that you are not changing the variable "state" with a button press?

                            Just to mention, state is being changed:

                             if (value != oldValue && value==0)
                              {
                                  state = !state;   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                  digitalWrite(RELAY_PIN, state);
                                  gw.saveState(CHILD_ID, state);// forgot to save the new state to EEPROM
                                  gw.send(msg.set(state, true)); // Send new state and request ack back
                              }
                            
                            BulldogLowellB Offline
                            BulldogLowellB Offline
                            BulldogLowell
                            Contest Winner
                            wrote on last edited by
                            #13

                            @rvendrame

                            @rvendrame said:

                            Just to mention, state is being changed:

                            yes, I added that... it was missing from OP's code

                            1 Reply Last reply
                            0
                            • hekH Offline
                              hekH Offline
                              hek
                              Admin
                              wrote on last edited by
                              #14

                              Yes, as you are aware not the WithButton example was created to be used with a monostable button.

                              Haven't really thought about bi-stable but should be doable.

                              The state is actually not stored in EEPROM until ack message comes back from gateway. It is also only then the actual realy is flipped.

                              As you already mention above removing value==0 would send of a change when flipping button. Not sure if one should pick up oldValue from eeprom at startup as well.

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                roffy
                                wrote on last edited by
                                #15

                                wow thanks for all the suggestions guys! sorry for the slow reply, this is the first time iv actually been able to sit at my computer since i posted, if only we didnt have to work to pay bills id be here all day...ahhh if only..... anyway!

                                Yes i am using a pushbutton not a rocker switch so i would need it to only do something on the down press and ignore the upward contact on the release of the button.

                                Ill try changing the script as explained above and see what that does, (as you have probably guessed by now my arduino tinkering only covered the first 10 or so lessons in the basic tutorial, then i went off and only looked up specific coding for the small projects i was working on....and most of which i have forgotten as i haven't used them for a long time :( need to brush up on what i should already know i guess)

                                anyway i shall post results as soon as i try out the suggestions. i think you all get what im trying to achieve here! thanks again,

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


                                16

                                Online

                                11.7k

                                Users

                                11.2k

                                Topics

                                113.1k

                                Posts


                                Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • MySensors
                                • OpenHardware.io
                                • Categories
                                • Recent
                                • Tags
                                • Popular