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. My Project
  3. Clone 2 skelets Relay and Button's

Clone 2 skelets Relay and Button's

Scheduled Pinned Locked Moved My Project
19 Posts 5 Posters 7.8k 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.
  • hekH hek

    First you present realys with id 1,2,3 and then you present switches with id 0,1,2. They overlap..

    T Offline
    T Offline
    tomrask
    wrote on last edited by
    #5

    @hek

    Hey Hek I changed the code, but it will not help. Now I get 3 door sensor and nothing else. hope you will help a little so that I understand the code. thank you

    // 1 Door virker 2 relays virker
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define NUMBER_OF_SWITCHES 3
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    MySensor gw;
    Bounce debouncer[NUMBER_OF_SWITCHES];
    
    int oldValue[NUMBER_OF_SWITCHES];
    byte switchPin[NUMBER_OF_SWITCHES] = {5,6,7}; //<<<<<<<<<<< set your switch pins here
    
    MyMessage msg(0,V_TRIPPED);
    
    void setup()  
    {  
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay&Botton", "1.0");
    
    // Switches setup
      for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
      {
        pinMode(switchPin[i],INPUT_PULLUP);
        debouncer[i] = Bounce();
        debouncer[i].attach(switchPin[i]);
        debouncer[i].interval(5);
      }
      for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
      {
        gw.present(i, S_DOOR);
        delay(250);
      }
    
      // Fetch relay status
      for (int sensor=3, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
     
      
     }
    //
    void loop() 
    {
     
    
      for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
      {
        debouncer[i].update();
        int value = debouncer[i].read();
        if (value != oldValue[i]) 
        {
          gw.send(msg.setSensor(i).set(value == HIGH? true : false), false); 
        }
        oldValue[i] = value;
     
     }
    
     // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.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());
       } 
    
    }
    
    
    ferpandoF 1 Reply Last reply
    0
    • T tomrask

      @hek

      Hey Hek I changed the code, but it will not help. Now I get 3 door sensor and nothing else. hope you will help a little so that I understand the code. thank you

      // 1 Door virker 2 relays virker
      #include <MySensor.h>
      #include <SPI.h>
      #include <Bounce2.h>
      
      #define NUMBER_OF_SWITCHES 3
      #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 2 // Total number of attached relays
      #define RELAY_ON 1  // GPIO value to write to turn on attached relay
      #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
      
      MySensor gw;
      Bounce debouncer[NUMBER_OF_SWITCHES];
      
      int oldValue[NUMBER_OF_SWITCHES];
      byte switchPin[NUMBER_OF_SWITCHES] = {5,6,7}; //<<<<<<<<<<< set your switch pins here
      
      MyMessage msg(0,V_TRIPPED);
      
      void setup()  
      {  
        // Initialize library and add callback for incoming messages
        gw.begin(incomingMessage, AUTO, true);
        // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Relay&Botton", "1.0");
      
      // Switches setup
        for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
        {
          pinMode(switchPin[i],INPUT_PULLUP);
          debouncer[i] = Bounce();
          debouncer[i].attach(switchPin[i]);
          debouncer[i].interval(5);
        }
        for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
        {
          gw.present(i, S_DOOR);
          delay(250);
        }
      
        // Fetch relay status
        for (int sensor=3, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
          // Register all sensors to gw (they will be created as child devices)
          gw.present(sensor, S_LIGHT);
          // Then set relay pins in output mode
          pinMode(pin, OUTPUT);   
          // Set relay to last known state (using eeprom storage) 
          digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
       
        
       }
      //
      void loop() 
      {
       
      
        for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
        {
          debouncer[i].update();
          int value = debouncer[i].read();
          if (value != oldValue[i]) 
          {
            gw.send(msg.setSensor(i).set(value == HIGH? true : false), false); 
          }
          oldValue[i] = value;
       
       }
      
       // Alway process incoming messages whenever possible
        gw.process();
      }
      
      void incomingMessage(const MyMessage &message) {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_LIGHT) {
           // Change relay state
           digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
           // Store state in eeprom
           gw.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());
         } 
      
      }
      
      
      ferpandoF Offline
      ferpandoF Offline
      ferpando
      Hero Member
      wrote on last edited by ferpando
      #6

      @tomrask
      What Hek is trying to tell you is that you can't have 2 devices with the same number.

      Change your first loop for:

        for (int sensor=0, pin=RELAY_1; sensor<NUMBER_OF_RELAYS;sensor++, pin++) {
          // Register all sensors to gw (they will be created as child devices)
          gw.present(sensor, S_LIGHT);
          // Then set relay pins in output mode
          pinMode(pin, OUTPUT);   
          // Set relay to last known state (using eeprom storage) 
          digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
      

      Change your second presentation loop for this

        for (int i = NUMBER_OF_RELAYS; i < (NUMBER_OF_RELAYS+NUMBER_OF_SWITCHES); i++)
        {
          gw.present(i, S_DOOR);
          delay(250);
        }
      

      This way you are creating relays 0 and 1, and then switches 2, 3 and 4

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tomrask
        wrote on last edited by
        #7

        Hey Ferpando

        thanks, now the sensor is displayed on vera. but only one contact and a door sensor works .
        I have not quite mastered the code yet , help wanted.

        // 3 Door 1 virker 2 relays 1 virker
        #include <MySensor.h>
        #include <SPI.h>
        #include <Bounce2.h>
        
        #define NUMBER_OF_SWITCHES 3
        #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
        #define NUMBER_OF_RELAYS 2 // Total number of attached relays
        #define RELAY_ON 1  // GPIO value to write to turn on attached relay
        #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
        
        MySensor gw;
        Bounce debouncer[NUMBER_OF_SWITCHES];
        
        int oldValue[NUMBER_OF_SWITCHES];
        byte switchPin[NUMBER_OF_SWITCHES] = {5,6,7}; //<<<<<<<<<<< set your switch pins here
        
        MyMessage msg(0,V_TRIPPED);
        
        void setup()  
        {  
          // Initialize library and add callback for incoming messages
          gw.begin(incomingMessage, AUTO, true);
          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Relay&Botton", "1.0");
        
          // Fetch relay status
          for (int sensor=0, pin=RELAY_1; sensor<NUMBER_OF_RELAYS;sensor++, pin++) {
            // Register all sensors to gw (they will be created as child devices)
            gw.present(sensor, S_LIGHT);
            // Then set relay pins in output mode
            pinMode(pin, OUTPUT);   
            // Set relay to last known state (using eeprom storage) 
            digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
          }
         
          // Switches setup
          for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
          {
            pinMode(switchPin[i],INPUT_PULLUP);
            debouncer[i] = Bounce();
            debouncer[i].attach(switchPin[i]);
            debouncer[i].interval(5);
          }
          for (int i = NUMBER_OF_RELAYS; i < (NUMBER_OF_RELAYS+NUMBER_OF_SWITCHES); i++)
          {
            gw.present(i, S_DOOR);
            delay(250);
          }
         }
        //
        void loop() 
        {
         
        
           for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
          {
            debouncer[i].update();
            int value = debouncer[i].read();
            if (value != oldValue[i]) 
            {
              gw.send(msg.setSensor(i).set(value == HIGH? true : false), false); 
            }
            oldValue[i] = value;
         
         }
        
         // Alway process incoming messages whenever possible
          gw.process();
        }
        
        void incomingMessage(const MyMessage &message) {
          // We only expect one type of message from controller. But we better check anyway.
          if (message.type==V_LIGHT) {
             // Change relay state
             digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
             // Store state in eeprom
             gw.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());
           } 
        
        }
        
        Here
        
        1 Reply Last reply
        0
        • T Offline
          T Offline
          tomrask
          wrote on last edited by
          #8

          Is there anyone out there who just want to give me a hand by getting this skeleton to work. I will be very happy just a little help. It could be great.

          1 Reply Last reply
          0
          • Vladut GrecuV Offline
            Vladut GrecuV Offline
            Vladut Grecu
            wrote on last edited by
            #9

            Can you post the serial output of the Arduino?

            T 1 Reply Last reply
            0
            • Vladut GrecuV Vladut Grecu

              Can you post the serial output of the Arduino?

              T Offline
              T Offline
              tomrask
              wrote on last edited by
              #10

              @Vladut-Grecu

              This is what I get from serial output

              repeater started, id 1
              send: 1-1-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
              send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
              read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:M
              send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Relay&Botton
              send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
              send: 1-1-0-0 s=0,c=0,t=0,pt=0,l=5,st=ok:1.4.1
              send: 1-1-0-0 s=1,c=0,t=0,pt=0,l=5,st=ok:1.4.1
              send: 1-1-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1

              1 Reply Last reply
              0
              • Vladut GrecuV Offline
                Vladut GrecuV Offline
                Vladut Grecu
                wrote on last edited by Vladut Grecu
                #11

                There is something going on with you code. It tells your gateway that you only have 3 devices (0,1,2)

                send: 1-1-0-0 s=0,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                send: 1-1-0-0 s=1,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                send: 1-1-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                

                I'll look into it when I get home, later this day.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tomrask
                  wrote on last edited by
                  #12

                  @Vladut-Grecu

                  thanks, I 'll be glad if you will

                  1 Reply Last reply
                  0
                  • Vladut GrecuV Offline
                    Vladut GrecuV Offline
                    Vladut Grecu
                    wrote on last edited by
                    #13

                    @tomrask said:
                    Please modify your sketch like this

                    for (int sensor=0, pin=RELAY_1; sensor<NUMBER_OF_RELAYS;sensor++, pin++) {
                        // Register all sensors to gw (they will be created as child devices)
                        gw.present(sensor, S_LIGHT);
                        Serial.print("\nThe nr ");
                        Serial.print(sensor);
                        Serial.print(" relay has been presented\n");
                        // Then set relay pins in output mode
                        pinMode(pin, OUTPUT);   
                        // Set relay to last known state (using eeprom storage) 
                        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
                      }
                    

                    and

                     for (int i = NUMBER_OF_RELAYS; i < (NUMBER_OF_RELAYS+NUMBER_OF_SWITCHES); i++)
                      {
                        gw.present(i, S_DOOR);
                        Serial.print("\nThe nr ");
                        Serial.print(i);
                        Serial.print(" switch has been presented\n");
                        delay(250);
                      }
                    

                    And post the serial output again.

                    T 1 Reply Last reply
                    0
                    • Vladut GrecuV Vladut Grecu

                      @tomrask said:
                      Please modify your sketch like this

                      for (int sensor=0, pin=RELAY_1; sensor<NUMBER_OF_RELAYS;sensor++, pin++) {
                          // Register all sensors to gw (they will be created as child devices)
                          gw.present(sensor, S_LIGHT);
                          Serial.print("\nThe nr ");
                          Serial.print(sensor);
                          Serial.print(" relay has been presented\n");
                          // Then set relay pins in output mode
                          pinMode(pin, OUTPUT);   
                          // Set relay to last known state (using eeprom storage) 
                          digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
                        }
                      

                      and

                       for (int i = NUMBER_OF_RELAYS; i < (NUMBER_OF_RELAYS+NUMBER_OF_SWITCHES); i++)
                        {
                          gw.present(i, S_DOOR);
                          Serial.print("\nThe nr ");
                          Serial.print(i);
                          Serial.print(" switch has been presented\n");
                          delay(250);
                        }
                      

                      And post the serial output again.

                      T Offline
                      T Offline
                      tomrask
                      wrote on last edited by
                      #14

                      @Vladut-Grecu

                      Hey Vladut

                      This is the seriel output on my Nano

                      repeater started, id 1
                      send: 1-1-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
                      send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
                      read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:M
                      send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Relay&Botton
                      send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                      send: 1-1-0-0 s=0,c=0,t=3,pt=0,l=5,st=ok:1.4.1

                      The nr 0 relay has been presented
                      send: 1-1-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1

                      The nr 1 relay has been presented
                      send: 1-1-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1

                      The nr 2 switch has been presented
                      send: 1-1-0-0 s=3,c=0,t=0,pt=0,l=5,st=ok:1.4.1

                      The nr 3 switch has been presented
                      send: 1-1-0-0 s=4,c=0,t=0,pt=0,l=5,st=ok:1.4.1

                      The nr 4 switch has been presented
                      send: 1-1-0-0 s=4,c=1,t=0,pt=2,l=2,st=ok:0
                      send: 1-1-0-0 s=3,c=1,t=0,pt=2,l=2,st=ok:0
                      send: 1-1-0-0 s=4,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=2,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
                      read: 1-0-1 s=4,c=1,t=0,pt=2,l=2:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
                      read: 1-0-1 s=4,c=1,t=0,pt=2,l=2:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=fail:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
                      read: 1-0-1 s=3,c=1,t=0,pt=2,l=2:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=fail:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=ok:1
                      read: 1-0-1 s=4,c=1,t=0,pt=2,l=2:1
                      send: 1-1-0-1 s=3,c=1,t=0,pt=2,l=2,st=ok:1
                      send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1

                      1 Reply Last reply
                      0
                      • Vladut GrecuV Offline
                        Vladut GrecuV Offline
                        Vladut Grecu
                        wrote on last edited by
                        #15

                        @tomrask said:

                        From your serial output, I think that the problem is with vera.(config, etc.. I don`t use vera)
                        The node presents your devices and the gateway recognizes them correctly.

                        send: 1-1-0-0 s=0,c=0,t=3,pt=0,l=5,st=ok:1.4.1
                        The nr 0 relay has been presented
                        
                        send: 1-1-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1
                        The nr 1 relay has been presented
                        
                        send: 1-1-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                        The nr 2 switch has been presented
                        
                        send: 1-1-0-0 s=3,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                        The nr 3 switch has been presented
                        
                        send: 1-1-0-0 s=4,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                        The nr 4 switch has been presented
                        

                        I guess you pressed the buttons to test them but when you press button nr 3 (child4) you got an error

                        send: 1-1-0-1 s=4,c=1,t=0,pt=2,l=2,st=fail:1
                        read: 1-0-1 s=4,c=1,t=0,pt=2,l=2:1
                        

                        You either wrongly configured vera to use child 4 as something else rather than a button (and it doesn't expect for an update, even if you present it as a button and it says that is ok) or it's beyond my understanding.

                        I will kindly ask @hek to to take a look over what I said and verify the integrity of the code presented but before that, I'll try to do your setup and report back.

                        1 Reply Last reply
                        0
                        • hekH Offline
                          hekH Offline
                          hek
                          Admin
                          wrote on last edited by
                          #16

                          I suggest you start simple. It is important you understand each step of the code.

                          • Remove loops (present each sensor separately).
                          • Remove debounce code (just read the digital inputs directly). They complicate things and you will better grasp what is happening without it.
                          • Same goes for incoming messages. Look at the incoming message and try to understand what digital output (relay) you want to set,

                          I could probably write the code for you. But what is the point? If you manage it yourself you'll feel like superstar. Take it in small steps and ask more questions if you don't understand what the purpose a particular line has.

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            tomrask
                            wrote on last edited by
                            #17

                            @hek

                            Hey Hek

                            Thank you for your good advice. I have a little vacation here in Easter. so I got it to work with 2 door sensors maybe it's not the best looking program . but it works .
                            Where can I learn more about these lines, I do not understand 100 % yet :

                            Bounce debouncer1 = Bounce();
                            
                            debouncer1.attach(BUTTON_PIN1);
                            
                            digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                            

                            Thanks for the help .

                            Tom Rask

                            // Sketch control 4 physical relays and 2 Doorsensors. 
                            // This example will remember relay state even after power failure.
                            
                            #include <MySensor.h>
                            #include <SPI.h>
                            #include <Bounce2.h> // button/reed switch program
                            
                            #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                            #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_ID1 5 // Arduino Digital I/O pin for button/reed switch
                            #define CHILD_ID2 6 // Arduino Digital I/O pin for button/reed switch
                            #define BUTTON_PIN1 7 // Arduino Digital I/O pin for button/reed switch
                            #define BUTTON_PIN2 8 // Arduino Digital I/O pin for button/reed switch
                            
                            MySensor gw;
                            
                            Bounce debouncer1 = Bounce(); 
                            Bounce debouncer2 = Bounce(); 
                            int oldValue1=-1;
                            int oldValue2=-1;
                            
                            // Change to V_LIGHT if you use S_LIGHT in presentation below
                            MyMessage msg1(CHILD_ID1,V_TRIPPED); // button/reed switch program
                            MyMessage msg2(CHILD_ID2,V_TRIPPED);
                            
                            
                            void setup()  
                            {   
                              // Initialize library and add callback for incoming messages
                              gw.begin(incomingMessage, AUTO, true);
                              // Send the sketch version information to the gateway and Controller
                              gw.sendSketchInfo("Relay and Door", "1.0");
                            
                              // Fetch relay status
                              
                                // Register all sensors to gw (they will be created as child devices)
                                gw.present(1, S_LIGHT);
                                gw.present(2, S_LIGHT);
                                gw.present(3, S_LIGHT);
                                gw.present(4, S_LIGHT);
                                // Then set relay pins in output mode
                                pinMode(3, OUTPUT);   
                                pinMode(4, OUTPUT); 
                                pinMode(5, OUTPUT); 
                                pinMode(6, OUTPUT); 
                                
                               // Setup the button
                                pinMode(BUTTON_PIN1,INPUT);// button/reed switch program
                                pinMode(BUTTON_PIN2,INPUT);// button/reed switch program
                                
                                // Set relay to last known state (using eeprom storage) 
                                digitalWrite(3, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                digitalWrite(4, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                digitalWrite(5, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                digitalWrite(6, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                // Activate internal pull-up
                                
                                digitalWrite(BUTTON_PIN1,HIGH);// button/reed switch program
                                digitalWrite(BUTTON_PIN2,HIGH);// button/reed switch program
                                  
                                // After setting up the button, setup debouncer
                                debouncer1.attach(BUTTON_PIN1);// button/reed switch program
                                debouncer2.attach(BUTTON_PIN2);// button/reed switch program
                                debouncer1.interval(5);// button/reed switch program
                                debouncer2.interval(5);// button/reed switch program   
                              
                                // 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.
                                gw.present(CHILD_ID1, S_DOOR); // button/reed switch program  
                                gw.present(CHILD_ID2, S_DOOR); // button/reed switch program 
                            }
                            
                            
                            void loop() 
                            {
                              // Alway process incoming messages whenever possible
                              gw.process();
                             debouncer1.update();
                              // Get the update value
                              int value1 = debouncer1.read();
                             
                              if (value1 != oldValue1) {
                                 // Send in the new value
                                 gw.send(msg1.set(value1==HIGH ? 1 : 0));
                                 oldValue1 = value1;
                              }
                            
                            debouncer2.update();
                              // Get the update value
                              int value2 = debouncer2.read();
                             
                              if (value2 != oldValue2) {
                                 // Send in the new value
                                 gw.send(msg2.set(value2==HIGH ? 1 : 0));
                                 oldValue2 = value2;
                              }
                            }
                            
                            void incomingMessage(const MyMessage &message) {
                              // We only expect one type of message from controller. But we better check anyway.
                              if (message.type==V_LIGHT) {
                                 // Change relay state
                                 digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                                 // Store state in eeprom
                                 gw.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());
                              }  
                            }
                            
                            
                            AWIA 1 Reply Last reply
                            0
                            • hekH Offline
                              hekH Offline
                              hek
                              Admin
                              wrote on last edited by
                              #18

                              @tomrask

                              Read more about bouncing here:
                              http://en.wikipedia.org/wiki/Switch#Contact_bounce

                              https://github.com/thomasfredericks/Bounce2/wiki

                              1 Reply Last reply
                              0
                              • T tomrask

                                @hek

                                Hey Hek

                                Thank you for your good advice. I have a little vacation here in Easter. so I got it to work with 2 door sensors maybe it's not the best looking program . but it works .
                                Where can I learn more about these lines, I do not understand 100 % yet :

                                Bounce debouncer1 = Bounce();
                                
                                debouncer1.attach(BUTTON_PIN1);
                                
                                digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                                

                                Thanks for the help .

                                Tom Rask

                                // Sketch control 4 physical relays and 2 Doorsensors. 
                                // This example will remember relay state even after power failure.
                                
                                #include <MySensor.h>
                                #include <SPI.h>
                                #include <Bounce2.h> // button/reed switch program
                                
                                #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                                #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_ID1 5 // Arduino Digital I/O pin for button/reed switch
                                #define CHILD_ID2 6 // Arduino Digital I/O pin for button/reed switch
                                #define BUTTON_PIN1 7 // Arduino Digital I/O pin for button/reed switch
                                #define BUTTON_PIN2 8 // Arduino Digital I/O pin for button/reed switch
                                
                                MySensor gw;
                                
                                Bounce debouncer1 = Bounce(); 
                                Bounce debouncer2 = Bounce(); 
                                int oldValue1=-1;
                                int oldValue2=-1;
                                
                                // Change to V_LIGHT if you use S_LIGHT in presentation below
                                MyMessage msg1(CHILD_ID1,V_TRIPPED); // button/reed switch program
                                MyMessage msg2(CHILD_ID2,V_TRIPPED);
                                
                                
                                void setup()  
                                {   
                                  // Initialize library and add callback for incoming messages
                                  gw.begin(incomingMessage, AUTO, true);
                                  // Send the sketch version information to the gateway and Controller
                                  gw.sendSketchInfo("Relay and Door", "1.0");
                                
                                  // Fetch relay status
                                  
                                    // Register all sensors to gw (they will be created as child devices)
                                    gw.present(1, S_LIGHT);
                                    gw.present(2, S_LIGHT);
                                    gw.present(3, S_LIGHT);
                                    gw.present(4, S_LIGHT);
                                    // Then set relay pins in output mode
                                    pinMode(3, OUTPUT);   
                                    pinMode(4, OUTPUT); 
                                    pinMode(5, OUTPUT); 
                                    pinMode(6, OUTPUT); 
                                    
                                   // Setup the button
                                    pinMode(BUTTON_PIN1,INPUT);// button/reed switch program
                                    pinMode(BUTTON_PIN2,INPUT);// button/reed switch program
                                    
                                    // Set relay to last known state (using eeprom storage) 
                                    digitalWrite(3, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                    digitalWrite(4, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                    digitalWrite(5, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                    digitalWrite(6, gw.loadState(1)?RELAY_ON:RELAY_OFF);
                                    // Activate internal pull-up
                                    
                                    digitalWrite(BUTTON_PIN1,HIGH);// button/reed switch program
                                    digitalWrite(BUTTON_PIN2,HIGH);// button/reed switch program
                                      
                                    // After setting up the button, setup debouncer
                                    debouncer1.attach(BUTTON_PIN1);// button/reed switch program
                                    debouncer2.attach(BUTTON_PIN2);// button/reed switch program
                                    debouncer1.interval(5);// button/reed switch program
                                    debouncer2.interval(5);// button/reed switch program   
                                  
                                    // 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.
                                    gw.present(CHILD_ID1, S_DOOR); // button/reed switch program  
                                    gw.present(CHILD_ID2, S_DOOR); // button/reed switch program 
                                }
                                
                                
                                void loop() 
                                {
                                  // Alway process incoming messages whenever possible
                                  gw.process();
                                 debouncer1.update();
                                  // Get the update value
                                  int value1 = debouncer1.read();
                                 
                                  if (value1 != oldValue1) {
                                     // Send in the new value
                                     gw.send(msg1.set(value1==HIGH ? 1 : 0));
                                     oldValue1 = value1;
                                  }
                                
                                debouncer2.update();
                                  // Get the update value
                                  int value2 = debouncer2.read();
                                 
                                  if (value2 != oldValue2) {
                                     // Send in the new value
                                     gw.send(msg2.set(value2==HIGH ? 1 : 0));
                                     oldValue2 = value2;
                                  }
                                }
                                
                                void incomingMessage(const MyMessage &message) {
                                  // We only expect one type of message from controller. But we better check anyway.
                                  if (message.type==V_LIGHT) {
                                     // Change relay state
                                     digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                                     // Store state in eeprom
                                     gw.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());
                                  }  
                                }
                                
                                
                                AWIA Offline
                                AWIA Offline
                                AWI
                                Hero Member
                                wrote on last edited by
                                #19

                                @tomrask when you set the relays to the " last known state" the same value (1) is used for all the relays.

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


                                11

                                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