Relay with button wihtout ackn from controller



  • Hi All,
    I am a bit lost at the moment with the Sketch "relay with button". If i understand this code correctly, is the normal process "push the button" ->node sends Status Change to Controller and wait for ack from Controller.
    How can i get this working without to wait for the Controllers ack ?

    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    #include <MySensors.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN 5  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN 6  // 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
    //#define MY_NODE_ID 98
    //#define MY_PARENT_NODE_ID 0
    //#define MY_PARENT_NODE_IS_STATIC
    //#define MY_RF24_PA_LEVEL RF24_PA_MIN
    
    Bounce debouncer = Bounce(); 
    int oldValue=0;
    bool state;
    
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    void setup()  
    {  
      // 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);
    
      // 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 = loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    }
    
    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_LIGHT);
    }
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      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;
    } 
    
    void receive(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) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         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());
       } 
    }
    

    Many Thanks

    Markus


  • Contest Winner

    @Markus. said in Relay with button wihtout ackn from controller:

    How can i get this working without to wait for the Controllers ack ?

    right now, the device

    1. waits for a button press
    2. sends a message to your controller with the new state
    3. waits for the message type (V_LIGHT) to acknowledge the state change and updates the relay

    if you want to change the state of the relay on the button press (isn't clear to me that you want to do that) you need to move:

    digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    

    inside the button press state change event:

    if (value != oldValue && value==0) 
    {
      send(msg.set(state?false:true), true); // Send new state and request ack back
      digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);
    }
    

    this would allow you to update the relay regardless of the connection to the gateway.



  • ahh okay thanks 🙂
    And If I want to use both funtionallitys ... For example a Button Input Change the state and also the Controller itself can Change the state like the normal relay sketch?


  • Contest Winner

    @Markus.

    I think that is how your original sketch works!

    Unless the gateway isn't functioning.

    You can always leave the function in both places.


Log in to reply
 

Suggested Topics

18
Online

11.4k
Users

11.1k
Topics

112.7k
Posts