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



  • 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!


  • Hero Member

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



  • 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?


  • Hero Member

    Try changing this line

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

    to this

    if (value != oldValue) {
    

  • Contest Winner

    @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;
    } 
    

  • Contest Winner

    @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);


  • Contest Winner

    @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....


  • Hero Member

    @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?


  • Contest Winner

    @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.


  • Hero Member

    @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...


  • Contest Winner

    @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.


  • Hero Member

    @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
      }
    

  • Contest Winner

    @rvendrame

    @rvendrame said:

    Just to mention, state is being changed:

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


  • Admin

    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.



  • 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,


Log in to reply
 

Suggested Topics

26
Online

11.2k
Users

11.1k
Topics

112.5k
Posts