Combine Relay Actuator (with button) and BinarySwitch examples?



  • Combine Relay Actuator (with button) and BinarySwitch examples?

    Have to say firstly that I'm still a novice so hopefully someone can help me out....

    I've managed to use the examples to create a Relay Actuator with button and a separate Binary Switch sensor using two separate arduino nanos.

    I'm now trying to combine the two sketches into one and don't quite know what I'm doing....

    Any help would be appreciated....


  • Hero Member

    Hi
    I'm a bit confused what sketches you are trying to combine..
    I take it you are starting with
    http://www.mysensors.org/build/relay#relaywithbuttonactuator-example

    as a base?



  • Yeah I'm just using the examples provided by this website...



  • Ideally I'd start with the relay with button script and then I want to add 2 window/door sensors for my garage doors.



  • I have this as an example but it's not showing the buttons in my controller:
    The relays on the other hand I can control

    // Example sketch showing how to control physical relays. 
    // This example will remember relay state even after power failure.
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define CHILD_ID 2
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    
    #define CHILD_ID 3
    #define BUTTON_PIN_1 3  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON_PIN_2 8  // Arduino Digital I/O pin for button/reed switch
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    
    void setup()  
    {  
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.0");
      gw.present(CHILD_ID, S_DOOR);
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
       // Setup the button
      pinMode(BUTTON_PIN_1,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_1,HIGH);
      pinMode(BUTTON_PIN_2,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_2,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN_1);
      debouncer.interval(5);
      debouncer.attach(BUTTON_PIN_2);
      debouncer.interval(5);
      
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
        
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
      
        debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    
    


  • @Sander-Stolk are you using Openhab? can you share your config please?



  • Well well well...
    I've got it working with 2 relays and 2 door sensors.

    See my sketch below!

    The problem is the relays which are configured by counting and adding up numbers and the child_id's for 1 button and the debouncer missing config in the skecth.

    With this sketch you can monitor 2 doors and 2 relays

    // Example sketch showing how to control physical relays. 
    // This example will remember relay state even after power failure.
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    
    #define BUTTON_PIN_1  3  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON_PIN_2  8  // Arduino Digital I/O pin for button/reed switch
    
    MySensor gw;
    Bounce debouncer_1 = Bounce();
    Bounce debouncer_2 = Bounce();
    int oldValue_1=-1;
    int oldValue_2=-1;
    
    MyMessage msgGK(BUTTON_PIN_1,V_TRIPPED);
    MyMessage msgGD(BUTTON_PIN_2,V_TRIPPED);
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Garagebox", "1.0");
    
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    
    // Setup the doorsensor
      //pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      //digitalWrite(BUTTON_PIN,HIGH);
      
      pinMode(BUTTON_PIN_1,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_1,HIGH);
      
      pinMode(BUTTON_PIN_2,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_2,HIGH);
      
      // After setting up the button, setup debouncer
      //debouncer.attach(BUTTON_PIN);
      //debouncer.interval(5);
      debouncer_1.attach(BUTTON_PIN_1);
      debouncer_1.interval(5);
      debouncer_2.attach(BUTTON_PIN_2);
      debouncer_2.interval(5);
      
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      gw.present(BUTTON_PIN_1, S_DOOR);
      gw.present(BUTTON_PIN_2, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
      
      debouncer_1.update();
      // Get the update value
      int value_1 = debouncer_1.read();
     
      if (value_1 != oldValue_1) {
         // Send in the new value
         gw.send(msgGK.set(value_1==HIGH ? 1 : 0));
         oldValue_1 = value_1;
      }
      
        debouncer_2.update();
      // Get the update value
      int value_2 = debouncer_2.read();
     
      if (value_2 != oldValue_2) {
         // Send in the new value
         gw.send(msgGD.set(value_2==HIGH ? 1 : 0));
         oldValue_2 = value_2;
      }
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    

Log in to reply
 

Suggested Topics

  • 8
  • 90
  • 3
  • 7
  • 1
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts