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. Troubleshooting
  3. Help with scetch relaywithbutton

Help with scetch relaywithbutton

Scheduled Pinned Locked Moved Troubleshooting
3 Posts 2 Posters 1.1k Views 2 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.
  • X Offline
    X Offline
    xypzo
    wrote on last edited by
    #1

    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());
       } 
    }```
    SparkmanS 1 Reply Last reply
    0
    • X xypzo

      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());
         } 
      }```
      SparkmanS Offline
      SparkmanS Offline
      Sparkman
      Hero Member
      wrote on last edited by
      #2

      @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

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xypzo
        wrote on last edited by
        #3

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

        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.1k

        Posts


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