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 wihtout ackn from controller

Relay with button wihtout ackn from controller

Scheduled Pinned Locked Moved Troubleshooting
4 Posts 2 Posters 985 Views 1 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.
  • M Offline
    M Offline
    Markus.
    wrote on last edited by
    #1

    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

    BulldogLowellB 1 Reply Last reply
    0
    • M Markus.

      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

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

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Markus.
        wrote on last edited by
        #3

        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?

        BulldogLowellB 1 Reply Last reply
        0
        • M Markus.

          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?

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

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

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


          19

          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