Relay actuator sketch - auto off function



  • I would like to control simple 12v 433 mhz remote control using relay. The thing is that relay needs to close only for half a second and then open again (just enough to send pulse out). It would be possible to create a scene in vera but due to very short response time I think it would be better to do this on node itself. There is similar functionality on Fibaro wall module (auto-off function).

    Not sure how to modify code - add delay and reverse previous state?

    void loop() 
    {
      // 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());
       } 
    }
    

  • Contest Winner

    @niccodemi

    you want to do it like this... but I don't know how many relays you have or which is your first:

    initialize/define these variables:

    #define PULSE_TIME 500UL  // half a second
    boolean goPulse;
    unsigned long pulseStartTime;
    

    and your loop() and relayPulse() functions like this:

    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);
        goPulse = true;
        pulseStartTime = millis();
        // 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());
      }
      relayPulse(); 
    }
    void relayPulse()
    {
      if (goPulse)
      {
        if (millis() - pulseStartTime < PULSE_TIME)
        {
          digitalWrite(yourRelay, HIGH);// didn't know your pin
        }
        else
        {
          digitalWrite(yourRelay, LOW); // didn't know your pin
          goPulse = false;
        }
      }  
    }


  • @BulldogLowell thanks, initially I want to use 2 relays.


  • Contest Winner

    @niccodemi

    switched separately, yes?



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


  • Contest Winner

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


  • @BulldogLowell thanks again.


  • Contest Winner

    @niccodemi

    great, let me know if it works for you



  • @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)?


  • Contest Winner

    @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?



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



  • @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.



  • @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.



  • @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.



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



  • @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);
     }    
        
    


  • @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!


Log in to reply
 

Suggested Topics

54
Online

11.4k
Users

11.1k
Topics

112.7k
Posts