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. Relay actuator sketch - auto off function

Relay actuator sketch - auto off function

Scheduled Pinned Locked Moved Development
relay
17 Posts 4 Posters 9.2k Views 4 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.
  • N Offline
    N Offline
    niccodemi
    wrote on last edited by
    #5

    1 arduino with 2 relays attached (each relay on different pin).

    1 Reply Last reply
    0
    • BulldogLowellB Offline
      BulldogLowellB Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by BulldogLowell
      #6

      try like this... I can compile but not test.

      Note: relay Pins in array will need to be edited, of course...

      #include <MySensor.h>
      #include <SPI.h>
      
      //#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
      #define PULSE_TIME 500UL  
      
      byte relayPin[] = {4,5};
      unsigned long relayStartTime[2];
      boolean pulseRelay[] = {false, false};
      
      MySensor gw;
      
      void setup()  
      {   
        gw.begin(incomingMessage, AUTO, true); // Initialize library and add callback for incoming messages
        gw.sendSketchInfo("MyRelay", "1.0"); // Send the sketch version information to the gateway and Controller
        for (byte sensor = 0; sensor < 2; sensor++) // Fetch relay status
        {
          gw.present(sensor, S_LIGHT); // Register all sensors to gw (they will be created as child devices)
          pinMode(relayPin[sensor], OUTPUT); // Then set relay pins in output mode
          //digitalWrite(relayPin[sensor], gw.loadState(sensor) ? RELAY_ON : RELAY_OFF); // You probably dont want to use EEPROM store here since you are only pulsing
        }
      }
      //
      void loop() 
      {
        gw.process();
        updateRelays();
      }
      
      void incomingMessage(const MyMessage &message) 
      {
        if (message.type==V_LIGHT) 
        {
           //digitalWrite(message.sensor, message.getBool() ? RELAY_ON : RELAY_OFF); // Change relay state
           pulseRelay[message.sensor] = true;
           relayStartTime[message.sensor] = millis();
           //gw.saveState(message.sensor, message.getBool()); // Store state in eeprom // Again, not needed here, I believe
           Serial.print("Incoming change for sensor:"); // Write some debug info
           Serial.print(message.sensor);
           Serial.print(", New status: ");
           Serial.println(message.getBool());
         } 
      }
      //
      void updateRelays()
      {
        for (byte sensor = 0; sensor < 2; sensor++)
        {
          if (pulseRelay[sensor] == true)
          {
            if (millis() - relayStartTime[sensor] <= PULSE_TIME)
            {
              digitalWrite(relayPin[sensor], HIGH);
            }
            else
            {
              digitalWrite(relayPin[sensor], LOW);
              pulseRelay[sensor] = false;
            }
          }
        }
      }
      
      N 1 Reply Last reply
      0
      • BulldogLowellB BulldogLowell

        try like this... I can compile but not test.

        Note: relay Pins in array will need to be edited, of course...

        #include <MySensor.h>
        #include <SPI.h>
        
        //#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
        #define PULSE_TIME 500UL  
        
        byte relayPin[] = {4,5};
        unsigned long relayStartTime[2];
        boolean pulseRelay[] = {false, false};
        
        MySensor gw;
        
        void setup()  
        {   
          gw.begin(incomingMessage, AUTO, true); // Initialize library and add callback for incoming messages
          gw.sendSketchInfo("MyRelay", "1.0"); // Send the sketch version information to the gateway and Controller
          for (byte sensor = 0; sensor < 2; sensor++) // Fetch relay status
          {
            gw.present(sensor, S_LIGHT); // Register all sensors to gw (they will be created as child devices)
            pinMode(relayPin[sensor], OUTPUT); // Then set relay pins in output mode
            //digitalWrite(relayPin[sensor], gw.loadState(sensor) ? RELAY_ON : RELAY_OFF); // You probably dont want to use EEPROM store here since you are only pulsing
          }
        }
        //
        void loop() 
        {
          gw.process();
          updateRelays();
        }
        
        void incomingMessage(const MyMessage &message) 
        {
          if (message.type==V_LIGHT) 
          {
             //digitalWrite(message.sensor, message.getBool() ? RELAY_ON : RELAY_OFF); // Change relay state
             pulseRelay[message.sensor] = true;
             relayStartTime[message.sensor] = millis();
             //gw.saveState(message.sensor, message.getBool()); // Store state in eeprom // Again, not needed here, I believe
             Serial.print("Incoming change for sensor:"); // Write some debug info
             Serial.print(message.sensor);
             Serial.print(", New status: ");
             Serial.println(message.getBool());
           } 
        }
        //
        void updateRelays()
        {
          for (byte sensor = 0; sensor < 2; sensor++)
          {
            if (pulseRelay[sensor] == true)
            {
              if (millis() - relayStartTime[sensor] <= PULSE_TIME)
              {
                digitalWrite(relayPin[sensor], HIGH);
              }
              else
              {
                digitalWrite(relayPin[sensor], LOW);
                pulseRelay[sensor] = false;
              }
            }
          }
        }
        
        N Offline
        N Offline
        niccodemi
        wrote on last edited by
        #7

        @BulldogLowell thanks again.

        BulldogLowellB 1 Reply Last reply
        0
        • N niccodemi

          @BulldogLowell thanks again.

          BulldogLowellB Offline
          BulldogLowellB Offline
          BulldogLowell
          Contest Winner
          wrote on last edited by
          #8

          @niccodemi

          great, let me know if it works for you

          N 1 Reply Last reply
          0
          • BulldogLowellB BulldogLowell

            @niccodemi

            great, let me know if it works for you

            N Offline
            N Offline
            niccodemi
            wrote on last edited by
            #9

            @BulldogLowell I tested it today and it works as expected.

            On the hardware side do you think it would make more sense to use transistor instead of relay (since I am dealing only with 12 volts)?

            BulldogLowellB 1 Reply Last reply
            0
            • N niccodemi

              @BulldogLowell I tested it today and it works as expected.

              On the hardware side do you think it would make more sense to use transistor instead of relay (since I am dealing only with 12 volts)?

              BulldogLowellB Offline
              BulldogLowellB Offline
              BulldogLowell
              Contest Winner
              wrote on last edited by
              #10

              @niccodemi

              I would consider what I'm switching (the load) as much as the voltage and the way they are being powered.

              " ...simple 12v 433 mhz remote control..."

              what kind of remote is this?

              N 1 Reply Last reply
              0
              • BulldogLowellB BulldogLowell

                @niccodemi

                I would consider what I'm switching (the load) as much as the voltage and the way they are being powered.

                " ...simple 12v 433 mhz remote control..."

                what kind of remote is this?

                N Offline
                N Offline
                niccodemi
                wrote on last edited by
                #11

                @BulldogLowell it is remote control fob similar to this one. It uses 12 volt battery (27A).

                DwaltD 1 Reply Last reply
                0
                • N niccodemi

                  @BulldogLowell it is remote control fob similar to this one. It uses 12 volt battery (27A).

                  DwaltD Offline
                  DwaltD Offline
                  Dwalt
                  wrote on last edited by
                  #12

                  @niccodemi
                  I am curious why you chose arduino -> relay -> 433 transmitter for your project and not simpler arduino -> 433 transmitter. I ask because I have one of these key fob RF cloners lying around and have not put it to use yet. I was considering using it to trigger scenes via a 433 receiver on a yet-to-be-built node.

                  Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

                  N 1 Reply Last reply
                  0
                  • DwaltD Dwalt

                    @niccodemi
                    I am curious why you chose arduino -> relay -> 433 transmitter for your project and not simpler arduino -> 433 transmitter. I ask because I have one of these key fob RF cloners lying around and have not put it to use yet. I was considering using it to trigger scenes via a 433 receiver on a yet-to-be-built node.

                    N Offline
                    N Offline
                    niccodemi
                    wrote on last edited by
                    #13

                    @Dwalt initially I tried with Vera and Rfxtrx433 but only later I learned there are many different coding systems on 433 Mhz and many (including Lightning4) are not supported in Rfxtrx plugin. I wanted to try with arduino + 433 transmitter but about same time I found out about mysensors and abandoned 433+arduino altogether. That said I've still got couple 433 mhz controlled switches and I want to integrate them to Vera-Mysensors environment. Until now I thought that the only option is to use relay. I would be interested to try other solutions.

                    DwaltD 1 Reply Last reply
                    0
                    • N niccodemi

                      @Dwalt initially I tried with Vera and Rfxtrx433 but only later I learned there are many different coding systems on 433 Mhz and many (including Lightning4) are not supported in Rfxtrx plugin. I wanted to try with arduino + 433 transmitter but about same time I found out about mysensors and abandoned 433+arduino altogether. That said I've still got couple 433 mhz controlled switches and I want to integrate them to Vera-Mysensors environment. Until now I thought that the only option is to use relay. I would be interested to try other solutions.

                      DwaltD Offline
                      DwaltD Offline
                      Dwalt
                      wrote on last edited by
                      #14

                      @niccodemi I have several of these 433 switches and use a nano (w/mains power) with the 433 transmitter from the store to control them thru MySensors/Vera. The drawback is that these cheap outlets do not give feedback (with MySensors or RFXtrx) but Vera tracks their state by last command.

                      The four I have on my setup work very reliably and have not failed a trigger command during the past two months of operation. The 433Mhz transmitter seems to have better distance within my house than the NRF24 and is very reliable, at least thru my limited experience. My 433 transmitter node is within line of sight of my gateway and only 3m distant. The 433 outlets are scattered throughout my house, on different floors and through multiple walls. I use them to control floor lamps and a fan.

                      I sniffed the RF code from the included remote using the 433 receiver and then put the codes within the sketch on the nano controlling the 433transmitter. I used this blog for details on the sniffing process. I got the set of four outlets for about $18 shipped during a sale so it worked out to $4.50 each.

                      Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

                      M 1 Reply Last reply
                      0
                      • DwaltD Dwalt

                        @niccodemi I have several of these 433 switches and use a nano (w/mains power) with the 433 transmitter from the store to control them thru MySensors/Vera. The drawback is that these cheap outlets do not give feedback (with MySensors or RFXtrx) but Vera tracks their state by last command.

                        The four I have on my setup work very reliably and have not failed a trigger command during the past two months of operation. The 433Mhz transmitter seems to have better distance within my house than the NRF24 and is very reliable, at least thru my limited experience. My 433 transmitter node is within line of sight of my gateway and only 3m distant. The 433 outlets are scattered throughout my house, on different floors and through multiple walls. I use them to control floor lamps and a fan.

                        I sniffed the RF code from the included remote using the 433 receiver and then put the codes within the sketch on the nano controlling the 433transmitter. I used this blog for details on the sniffing process. I got the set of four outlets for about $18 shipped during a sale so it worked out to $4.50 each.

                        M Offline
                        M Offline
                        mvader
                        wrote on last edited by
                        #15

                        @Dwalt can you post your sketch for these 433 mhz units?
                        Thanks!

                        DwaltD 1 Reply Last reply
                        0
                        • M mvader

                          @Dwalt can you post your sketch for these 433 mhz units?
                          Thanks!

                          DwaltD Offline
                          DwaltD Offline
                          Dwalt
                          wrote on last edited by
                          #16

                          @mvader

                          The original sketch was posted here. It is crude (my first arduino sketch) but it worked and continued to work for the past 9 months. I sniffed the codes and then hard coded them into the sketch rather than use one of the RF libraries which I couldn't figure out.

                          I recently updated it for it to work under MyS 1.5 but did not refine it. It is still ugly:

                          #include <MySensor.h>
                          #include <SPI.h>
                          #include <EEPROM.h>  
                          #include <NewRemoteReceiver.h>
                          #include <NewRemoteTransmitter.h>
                          #include <MyTransportNRF24.h>
                          #include <MyHwATMega328.h>
                          
                          #define TRANSMITTER_PIN 3
                          #define RECEIVER_INTERRUPT 1
                          #define RF433_CHILD_ID 0
                          #define NUMBER_OF_OUTLETS 4
                          #define DELAYSHORT 160
                          #define DELAYLONG  500
                              
                          #define SEND_DATA 3
                          
                          MySensor gw;
                          
                          
                          static void ookPulse(int on, int off) {
                            digitalWrite(SEND_DATA, HIGH);
                            delayMicroseconds(on);
                            digitalWrite(SEND_DATA, LOW);
                            delayMicroseconds(off);
                          }
                          
                          static void pt2262Send(uint16_t signature, uint8_t command) {
                            byte i, k;
                            // send 16 times
                            for(k=0;k<16;k++) {
                              // send signature first
                              for(i=0;i<16;i++) {
                                if((signature>>(15-i)) & 0x1) {
                                  ookPulse(DELAYLONG, DELAYSHORT);
                                } else {
                                  ookPulse(DELAYSHORT, DELAYLONG);
                                }
                              }
                              for(i=0;i<8;i++) {
                                if((command>>(7-i)) & 0x1) {
                                  ookPulse(DELAYLONG, DELAYSHORT);
                                } else {
                                  ookPulse(DELAYSHORT, DELAYLONG);
                                }
                              }
                             
                              // end with a '0'
                              ookPulse(DELAYSHORT, DELAYLONG);
                          
                              // short delay
                              gw.wait(5);
                            }
                          }
                          
                          
                          void setup() 
                          {
                            
                           Serial.begin(115200);   
                           gw.begin(incomingMessage, AUTO, true);
                           gw.sendSketchInfo("RF433", "1.1");
                           
                           for (int sensor=1; sensor<=NUMBER_OF_OUTLETS; sensor++){
                           gw.present(sensor, S_LIGHT);
                           }
                           
                          }
                          
                          void loop() {
                            gw.process();
                          }
                            void incomingMessage(const MyMessage &message) {
                            
                            if (message.type==V_LIGHT) {
                            int incomingLightState =  message.getBool(); 
                            int incomingOutlet = message.sensor;
                            
                           /* Serial.print("Outlet #: ");
                            Serial.println(message.sensor);
                            Serial.print("Command: ");
                            Serial.println(message.getBool());
                           */
                           if (incomingOutlet==1) {
                           if (incomingLightState==1) {
                              // Turn on  socket 1
                           //   Serial.println("\nTurn on Socket 1");
                           pt2262Send(0b0101000101010101, 0b00110011);
                           gw.wait(100); 
                           }
                           if (incomingLightState==0)  {
                              // Turn off socket 1
                           // Serial.println("\nTurn off Socket 1");
                          pt2262Send(0b0101000101010101, 0b00111100);
                          gw.wait(100); 
                           }
                           }
                           if (incomingOutlet==2) {
                           if (incomingLightState==1) {
                              // Turn on  socket 2
                           //   Serial.println("\nTurn on Socket 2");
                           pt2262Send(0b0101000101010101, 0b11000011);
                           gw.wait(100); 
                           }
                           if (incomingLightState==0)  {
                              // Turn off socket 2
                          //   Serial.println("\nTurn off Socket 2");
                          pt2262Send(0b0101000101010101, 0b11001100);
                          gw.wait(100); 
                           }
                           }
                           if (incomingOutlet==3) {
                           if (incomingLightState==1) {
                              // Turn on  socket 3
                           //   Serial.println("\nTurn on Socket 3");
                           pt2262Send(0b0101000101010111, 0b00000011);
                           gw.wait(100); 
                           }
                           if (incomingLightState==0)  {
                              // Turn off socket 3
                          //   Serial.println("\nTurn off Socket 3");
                          pt2262Send(0b0101000101010111, 0b00001100);
                          gw.wait(100);
                           }
                           }
                           if (incomingOutlet==4) {
                           if (incomingLightState==1) {
                              // Turn on  socket 4
                           //   Serial.println("\nTurn on Socket 4");
                           pt2262Send(0b0101000101011101, 0b00000011);
                           gw.wait(100); 
                           }
                           if (incomingLightState==0)  {
                              // Turn off socket 4
                          // Serial.println("\nTurn off Socket 4");
                          pt2262Send(0b0101000101011101, 0b00001100);
                          gw.wait(100); 
                           }
                           }
                            }
                           gw.wait(100);
                           }    
                              
                          

                          Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

                          M 1 Reply Last reply
                          0
                          • DwaltD Dwalt

                            @mvader

                            The original sketch was posted here. It is crude (my first arduino sketch) but it worked and continued to work for the past 9 months. I sniffed the codes and then hard coded them into the sketch rather than use one of the RF libraries which I couldn't figure out.

                            I recently updated it for it to work under MyS 1.5 but did not refine it. It is still ugly:

                            #include <MySensor.h>
                            #include <SPI.h>
                            #include <EEPROM.h>  
                            #include <NewRemoteReceiver.h>
                            #include <NewRemoteTransmitter.h>
                            #include <MyTransportNRF24.h>
                            #include <MyHwATMega328.h>
                            
                            #define TRANSMITTER_PIN 3
                            #define RECEIVER_INTERRUPT 1
                            #define RF433_CHILD_ID 0
                            #define NUMBER_OF_OUTLETS 4
                            #define DELAYSHORT 160
                            #define DELAYLONG  500
                                
                            #define SEND_DATA 3
                            
                            MySensor gw;
                            
                            
                            static void ookPulse(int on, int off) {
                              digitalWrite(SEND_DATA, HIGH);
                              delayMicroseconds(on);
                              digitalWrite(SEND_DATA, LOW);
                              delayMicroseconds(off);
                            }
                            
                            static void pt2262Send(uint16_t signature, uint8_t command) {
                              byte i, k;
                              // send 16 times
                              for(k=0;k<16;k++) {
                                // send signature first
                                for(i=0;i<16;i++) {
                                  if((signature>>(15-i)) & 0x1) {
                                    ookPulse(DELAYLONG, DELAYSHORT);
                                  } else {
                                    ookPulse(DELAYSHORT, DELAYLONG);
                                  }
                                }
                                for(i=0;i<8;i++) {
                                  if((command>>(7-i)) & 0x1) {
                                    ookPulse(DELAYLONG, DELAYSHORT);
                                  } else {
                                    ookPulse(DELAYSHORT, DELAYLONG);
                                  }
                                }
                               
                                // end with a '0'
                                ookPulse(DELAYSHORT, DELAYLONG);
                            
                                // short delay
                                gw.wait(5);
                              }
                            }
                            
                            
                            void setup() 
                            {
                              
                             Serial.begin(115200);   
                             gw.begin(incomingMessage, AUTO, true);
                             gw.sendSketchInfo("RF433", "1.1");
                             
                             for (int sensor=1; sensor<=NUMBER_OF_OUTLETS; sensor++){
                             gw.present(sensor, S_LIGHT);
                             }
                             
                            }
                            
                            void loop() {
                              gw.process();
                            }
                              void incomingMessage(const MyMessage &message) {
                              
                              if (message.type==V_LIGHT) {
                              int incomingLightState =  message.getBool(); 
                              int incomingOutlet = message.sensor;
                              
                             /* Serial.print("Outlet #: ");
                              Serial.println(message.sensor);
                              Serial.print("Command: ");
                              Serial.println(message.getBool());
                             */
                             if (incomingOutlet==1) {
                             if (incomingLightState==1) {
                                // Turn on  socket 1
                             //   Serial.println("\nTurn on Socket 1");
                             pt2262Send(0b0101000101010101, 0b00110011);
                             gw.wait(100); 
                             }
                             if (incomingLightState==0)  {
                                // Turn off socket 1
                             // Serial.println("\nTurn off Socket 1");
                            pt2262Send(0b0101000101010101, 0b00111100);
                            gw.wait(100); 
                             }
                             }
                             if (incomingOutlet==2) {
                             if (incomingLightState==1) {
                                // Turn on  socket 2
                             //   Serial.println("\nTurn on Socket 2");
                             pt2262Send(0b0101000101010101, 0b11000011);
                             gw.wait(100); 
                             }
                             if (incomingLightState==0)  {
                                // Turn off socket 2
                            //   Serial.println("\nTurn off Socket 2");
                            pt2262Send(0b0101000101010101, 0b11001100);
                            gw.wait(100); 
                             }
                             }
                             if (incomingOutlet==3) {
                             if (incomingLightState==1) {
                                // Turn on  socket 3
                             //   Serial.println("\nTurn on Socket 3");
                             pt2262Send(0b0101000101010111, 0b00000011);
                             gw.wait(100); 
                             }
                             if (incomingLightState==0)  {
                                // Turn off socket 3
                            //   Serial.println("\nTurn off Socket 3");
                            pt2262Send(0b0101000101010111, 0b00001100);
                            gw.wait(100);
                             }
                             }
                             if (incomingOutlet==4) {
                             if (incomingLightState==1) {
                                // Turn on  socket 4
                             //   Serial.println("\nTurn on Socket 4");
                             pt2262Send(0b0101000101011101, 0b00000011);
                             gw.wait(100); 
                             }
                             if (incomingLightState==0)  {
                                // Turn off socket 4
                            // Serial.println("\nTurn off Socket 4");
                            pt2262Send(0b0101000101011101, 0b00001100);
                            gw.wait(100); 
                             }
                             }
                              }
                             gw.wait(100);
                             }    
                                
                            
                            M Offline
                            M Offline
                            mvader
                            wrote on last edited by
                            #17

                            @Dwalt oh wow.. thanks so much.. i was waiting for you to get back to me but never saw this post.. and i just happened to stumble on it when i was looking to see if you had been back on the forum or not.
                            I'm good with the hardware side of things and can shell script and do windows scripting. but still have not learned this coding yet.
                            i have all the hardware for the 433 stuff.
                            i even got the RF stuff to work (on my RPI2) and sniffed all my codes
                            i could just never get it to work with my arduino stuff.
                            likely the code, so i'm excited to give your code a go.
                            Thanks!

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


                            15

                            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