Multi relay with two Binary switches



  • I created 2 project where I need 5 relays and 2 buttons. If I put the number of relays on 5, I only see 3 in my Domoticz. The switches are visible in Domoticz without problems. How can I fix the relay issue?

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69

    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE

    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>

    #define RELAY_1 A0 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 5 // 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_IRBIN 3
    #define CHILD_ID_IRBUI 4
    #define IRBIN_PIN 3 // Arduino Digital I/O pin for button/reed switch
    #define IRBUI_PIN 4

    Bounce debouncerIRBIN = Bounce();
    Bounce debouncerIRBUI = Bounce();

    int oldValue_IRBIN = -1;
    int oldValue_IRBUI = -1;

    MyMessage msgIRBIN(CHILD_ID_IRBIN, V_TRIPPED);
    MyMessage msgIRBUI(CHILD_ID_IRBUI, V_TRIPPED);

    void before()
    {
    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    }
    }

    void setup()
    {
    // Setup the button1
    pinMode(IRBIN_PIN, INPUT);
    debouncerIRBIN.attach(IRBIN_PIN);
    debouncerIRBIN.interval(5);
    digitalWrite(IRBIN_PIN, HIGH);
    // Setup the button2
    pinMode(IRBUI_PIN, INPUT);
    debouncerIRBUI.attach(IRBUI_PIN);
    debouncerIRBUI.interval(5);
    digitalWrite(IRBUI_PIN, HIGH);

    }

    void presentation()
    {
    // 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.
    present(CHILD_ID_IRBIN, S_DOOR);
    present(CHILD_ID_IRBUI, S_DOOR);

    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)
    	present(sensor, S_BINARY);
    }
    

    }

    void loop()
    {
    debouncerIRBIN.update();
    // Get the update value
    int valueIRBIN = debouncerIRBIN.read();

    if (valueIRBIN != oldValue_IRBIN) {
    // Send in the new value
    send(msgIRBIN.set(valueIRBIN == HIGH ? 1 : 0));
    oldValue_IRBIN = valueIRBIN;
    }

    debouncerIRBUI.update();
    // Get the update value
    int valueIRBUI = debouncerIRBUI.read();

    if (valueIRBUI != oldValue_IRBUI) {
    // Send in the new value
    send(msgIRBUI.set(valueIRBUI == HIGH ? 1 : 0));
    oldValue_IRBUI = valueIRBUI;
    }
    }

    void receive(const MyMessage &message)
    {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type==V_STATUS) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    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());
    }
    }

    
    


  • @dick said in Multi relay with two Binary switches:

    I created 2 project where I need 5 relays and 2 buttons. If I put the number of relays on 5, I only see 3 in my Domoticz. The switches are visible in Domoticz without problems. How can I fix the relay issue?

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69

    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE

    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>

    #define RELAY_1 A0 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 5 // 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_IRBIN 3
    #define CHILD_ID_IRBUI 4
    #define IRBIN_PIN 3 // Arduino Digital I/O pin for button/reed switch
    #define IRBUI_PIN 4

    Bounce debouncerIRBIN = Bounce();
    Bounce debouncerIRBUI = Bounce();

    int oldValue_IRBIN = -1;
    int oldValue_IRBUI = -1;

    MyMessage msgIRBIN(CHILD_ID_IRBIN, V_TRIPPED);
    MyMessage msgIRBUI(CHILD_ID_IRBUI, V_TRIPPED);

    void before()
    {
    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    }
    }

    void setup()
    {
    // Setup the button1
    pinMode(IRBIN_PIN, INPUT);
    debouncerIRBIN.attach(IRBIN_PIN);
    debouncerIRBIN.interval(5);
    digitalWrite(IRBIN_PIN, HIGH);
    // Setup the button2
    pinMode(IRBUI_PIN, INPUT);
    debouncerIRBUI.attach(IRBUI_PIN);
    debouncerIRBUI.interval(5);
    digitalWrite(IRBUI_PIN, HIGH);

    }

    void presentation()
    {
    // 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.
    present(CHILD_ID_IRBIN, S_DOOR);
    present(CHILD_ID_IRBUI, S_DOOR);

    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)
    present(sensor, S_BINARY);
    }
    }

    void loop()
    {
    debouncerIRBIN.update();
    // Get the update value
    int valueIRBIN = debouncerIRBIN.read();

    if (valueIRBIN != oldValue_IRBIN) {
    // Send in the new value
    send(msgIRBIN.set(valueIRBIN == HIGH ? 1 : 0));
    oldValue_IRBIN = valueIRBIN;
    }

    debouncerIRBUI.update();
    // Get the update value
    int valueIRBUI = debouncerIRBUI.read();

    if (valueIRBUI != oldValue_IRBUI) {
    // Send in the new value
    send(msgIRBUI.set(valueIRBUI == HIGH ? 1 : 0));
    oldValue_IRBUI = valueIRBUI;
    }
    }

    void receive(const MyMessage &message)
    {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type==V_STATUS) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    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());
    }
    }

    
    

    Sorry, add my questions two times


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 1
  • 6
  • 1
  • 3

55
Online

11.4k
Users

11.1k
Topics

112.7k
Posts