2x Relay, 1x Reed Switch on 1 node for Home Assistant



  • Hey!
    N00b ahead warning!

    I had a lot of trouble trying to get 2 relays and a reed switch working on one node in Home Assistant, but after hours of trying i finally got some code that works

    It's very messy and is a big mash of sample code and other projects code but it works.

    Would be nice if it would remember the state after a power off but that is way beyond me

    If anyone ends up using this and cleans it up feel free to post it back here 😛

    Thanks!

    /*
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * http://www.mysensors.org/build/relay
     */
    
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_REPEATER_FEATURE
    #define MY_NODE_ID 43
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN1  3
    #define RELAY_PIN2  4
    #define BUTTON_PIN1  6 
    #define CHILD_ID1 1
    #define CHILD_ID2 2
    #define CHILD_ID3 3
    #define RELAY_ON 1
    #define RELAY_OFF 0
     
    
    Bounce debouncer = Bounce();
    bool state = false;
    bool state2 = false;
    bool initialValueSent = false;
    int oldValue=-1;
    
    MyMessage msg(CHILD_ID1, V_STATUS);
    MyMessage msg2(CHILD_ID2, V_STATUS);
    MyMessage msg3(CHILD_ID3,V_TRIPPED);
    
    void setup()
    {
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN1, RELAY_OFF);
      digitalWrite(RELAY_PIN2, RELAY_OFF);
      pinMode(RELAY_PIN1, OUTPUT);
      pinMode(RELAY_PIN2, OUTPUT);
      pinMode(BUTTON_PIN1,INPUT);
      
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN1,HIGH);
    
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN1);
      debouncer.interval(5);
    }
    
    void presentation()  {
      sendSketchInfo("Relay+Reed", "1.0");
      wait(100);
      present(CHILD_ID1, S_BINARY);
      wait(100);
      present(CHILD_ID2, S_BINARY);
      wait(100);
      present(CHILD_ID3, S_DOOR); 
    }
    
    void loop()
    {
      if (!initialValueSent) {
        Serial.println("Sending initial value");
        send(msg.set(state?RELAY_ON:RELAY_OFF));
        send(msg2.set(state2?RELAY_ON:RELAY_OFF));
        Serial.println("Requesting initial value from controller");
        request(CHILD_ID1, V_STATUS);
        wait(2000, C_SET, V_STATUS);
        request(CHILD_ID2, V_STATUS);
        wait(2000, C_SET, V_STATUS);
      }
      if (debouncer.update()) {
        if (debouncer.read()==LOW) {
          state = !state;
      //    // Send new state and request ack back
       //   send(msg.set(state?RELAY_ON:RELAY_OFF), true);
        }
        {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
    
      if (value != oldValue) {
         // Send in the new value
         send(msg3.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      }
    }
    
    }
    
    void receive(const MyMessage &message) {
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_STATUS && message.sensor ==1) {
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
        // Change relay state
        state = (bool)message.getInt();
        digitalWrite(RELAY_PIN1, state?RELAY_OFF:RELAY_ON);
        send(msg.set(state?RELAY_ON:RELAY_OFF));
      }
       
      if (message.type == V_STATUS && message.sensor ==2) {
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
        // Change relay state
        state2 = (bool)message.getInt();
        digitalWrite(RELAY_PIN2, state2?RELAY_OFF:RELAY_ON);
        send(msg2.set(state2?RELAY_ON:RELAY_OFF));
      }
    }
    

  • Hardware Contributor

    @Cameron-Payne
    Hello and welcome!
    Great to hear your success 🙂
    About saving the state you shoud look at this 😉 https://www.mysensors.org/download/sensor_api_20#saving-state And there are more infos about the API.
    Shortly save your state when it change, and load it in your setup.


Log in to reply
 

Suggested Topics

42
Online

11.4k
Users

11.1k
Topics

112.7k
Posts