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. Development
  3. Multi relay with two Binary switches

Multi relay with two Binary switches

Scheduled Pinned Locked Moved Development
2 Posts 1 Posters 791 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.
  • DickD Offline
    DickD Offline
    Dick
    wrote on last edited by
    #1

    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());
    }
    }

    
    
    DickD 1 Reply Last reply
    0
    • DickD Dick

      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());
      }
      }

      
      
      DickD Offline
      DickD Offline
      Dick
      wrote on last edited by
      #2

      @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

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


      18

      Online

      11.7k

      Users

      11.2k

      Topics

      113.0k

      Posts


      Copyright 2019 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