Help with scetch relaywithbutton



  • Hi, I am trying to have a double relay switching using FHEM controller.

    I took the code of scetch RelayWithButton and modified it for 2 relays.
    When I switch in FHEM, it will always switch both relays at the same time.

    I am a copy-pasting extreme code NOOB, so please be gentle!

    Thanks in advance
    xypzo

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_ON 1
    #define RELAY_OFF 0
    #define RELAY1_PIN  3  // Arduino Digital I/O pin number for relay 1
    #define RELAY2_PIN  4  // Arduino Digital I/O pin number for relay 2 
    #define BUTTON_PIN  7  // Arduino Digital I/O pin number for button 
    #define CHILD_ID_R 1   // Id of the sensor child R1
    #define CHILD_ID_R2 2   // Id of the sensor child R2
    
    
    Bounce debouncer = Bounce(); 
    int oldValue=0;
    bool state;
    MySensor gw;
    MyMessage msg1(CHILD_ID_R, V_LIGHT);
    MyMessage msg2(CHILD_ID_R2, V_LIGHT);
    
    
    void setup()  
    {  
      gw.begin(incomingMessage, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relays & Button", "0.1");
    
     // 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);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_R, S_LIGHT);
      gw.present(CHILD_ID_R2, S_LIGHT);
      
      // Make sure relays are off when starting up
      digitalWrite(RELAY1_PIN, RELAY_OFF);
      digitalWrite(RELAY2_PIN, RELAY_OFF);
      
      // Then set relay pins in output mode
      pinMode(RELAY1_PIN, OUTPUT);
      pinMode(RELAY2_PIN, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(CHILD_ID_R);
      digitalWrite(RELAY1_PIN, state?RELAY_ON:RELAY_OFF);
      state = gw.loadState(CHILD_ID_R2);
      digitalWrite(RELAY2_PIN, state?RELAY_ON:RELAY_OFF);
    }
    
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      gw.process();
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
      if (value != oldValue && value==0) {
          gw.send(msg1.set(state?false:true), true); // Send new state and request ack back
          gw.send(msg2.set(state?false:true), true);
      }
      oldValue = value;
    } 
     
    void incomingMessage(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(RELAY1_PIN, state?RELAY_ON:RELAY_OFF);
         digitalWrite(RELAY2_PIN, state?RELAY_ON:RELAY_OFF);
         
         // Store state in eeprom
         gw.saveState(CHILD_ID_R, state);
         gw.saveState(CHILD_ID_R2, state);
        
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }```

  • Hero Member

    @xypzo In your incoming message function, you need to check the message to see which relay it is for (message.sensor). You're not checking for that and you always switch both relays by issuing these two commands in a row:

    digitalWrite(RELAY1_PIN, state?RELAY_ON:RELAY_OFF);
    digitalWrite(RELAY2_PIN, state?RELAY_ON:RELAY_OFF);
    

    You need something like:

    if (message.sensor == CHILD_ID_R) {
         digitalWrite(RELAY1_PIN, state?RELAY_ON:RELAY_OFF);
         gw.saveState(CHILD_ID_R, state);
    }
    if (message.sensor == CHILD_ID_R2) {
         digitalWrite(RELAY2_PIN, state?RELAY_ON:RELAY_OFF);
         gw.saveState(CHILD_ID_R2, state);
    }
    

    Cheers
    Al



  • Wow, thank you so much, it works like a charm!


Log in to reply
 

Suggested Topics

12
Online

11.4k
Users

11.1k
Topics

112.7k
Posts