433mhz keyfob remote



  • hi there,
    i'm here to share my project, it's just a simple keyfob programming/receiver to be connected to our mysensors net. I use this to control my surveillance sistem (activation/deactivation).
    i wrote this sketch for mysensors 1.5, so maybe it need to be translated for latest version.
    my coding it's so rude, so i hope someone can also improve it.
    later i will post schematic and the used component list.
    cheers

    #include <MySensor.h>
    #include <SPI.h>
    #include <RCSwitch.h> 
    #include <EEPROMEx.h>
    
    #define STATIC_ID 4
    #define CHILD_ID 1
    
    RCSwitch RFKey = RCSwitch();
    
    const byte progSwitch = 4;                  // Prgram button
    const byte ledGreen = 6;                  // green led
    const byte ledRed = 7;                  // red led
    
    unsigned long lastAvailable = 0;
    unsigned long previousMillis = 0; 
    
    int timeout = 3000;               // set unlock timeout here
    
    MySensor gw;
    MyMessage msg(CHILD_ID,V_TRIPPED);
    int oldValue=-1;
    unsigned long code1, code2, code3;
    
    void setup() { 
      gw.begin(NULL, STATIC_ID, false);
    
      pinMode(progSwitch, INPUT_PULLUP);      // Prog button
      pinMode(ledGreen, OUTPUT);              // unlock/learning
      pinMode(ledRed, OUTPUT);                // invalid code/stop learnig
    
      RFKey.enableTransmit(3);     // Transmitter is connected to Arduino Pin #3  
      RFKey.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
    
      gw.sendSketchInfo("Ricevitore 433 MHZ", "1.0");
      gw.present(CHILD_ID, S_DOOR); 
     
      code1 = EEPROM.readLong( 50 ); 
      code2 = EEPROM.readLong( 100 ); 
      code3 = EEPROM.readLong( 150 ); 
      
    }
    
    void loop() {
    
      if ( !digitalRead(progSwitch) ) {                    // check button
        unsigned long lastMillis = 0;
        previousMillis = millis();
        while ( !digitalRead(progSwitch) ) {
          lastMillis = millis();
        }
        if ( (lastMillis - previousMillis) > 10000 ){                 // press button 10sec
          updateCode();                                               // change remote codes 
        }
        else if ( (lastMillis - previousMillis) > 200 ){              // debounce
          progRemote();                                               // send radio code to remotes learn
        }
      }
      ////////////////////////////////////////////////////////
      if ( RFKey.available() ) {       // multicode control
        blinkLed(ledRed, 1, 50); 
        unsigned long receivedCode = RFKey.getReceivedValue();
        RFKey.resetAvailable();
        
    
       Serial.println (receivedCode);
    
        if ( receivedCode == code1 ){       // check first code  
    
          unsigned long checkTime = millis();
          receivedCode = 0;
          boolean flag = false;
    
          while ( millis() < (checkTime + timeout) ) {
            if ( RFKey.available() ) {
              blinkLed(ledRed, 1, 50);
              receivedCode = RFKey.getReceivedValue();
              RFKey.resetAvailable();
              if ( receivedCode == code2 ){                //check second code 
                Serial.println ("*** UNLOCKED ***");
                digitalWrite( ledGreen, HIGH);
                digitalWrite( ledRed, LOW);            
                gw.send(msg.set(0==HIGH ? 1 : 0));             // send unlock code
                flag = true;
                break;
              } 
            }
          } 
          if (flag == false){
            gw.send(msg.set(1==HIGH ? 1 : 0)); 
            blinkLed(ledGreen, 1, 100);
            Serial.println ("*** LOCKED ***");
            digitalWrite( ledGreen, LOW);
            digitalWrite( ledRed, HIGH);         
            RFKey.resetAvailable();
            unsigned long checkTime = millis();
            receivedCode = 0;
          } 
        }
        else if ( receivedCode == code3 ) {                // CODICE AUX
            gw.send(msg.set(2)); 
            blinkLed(ledGreen, 1, 100);
            Serial.println ("*** AUX ***");
            delay(3000);
            RFKey.resetAvailable();
            receivedCode = 0;    
        }
        
        else{
          RFKey.resetAvailable();
          delay (1000);
          Serial.println("*** invalid code ***");
          Serial.print("received code: ");
          Serial.println(receivedCode);
          receivedCode = 0;
        }
      }
    }
    
    
    
    void progRemote(){                                    // keyfob programming
    
      unsigned long code1 = EEPROM.readLong( 50 ); 
      unsigned long code2 = EEPROM.readLong( 100 ); 
      unsigned long code3 = EEPROM.readLong( 150 );   
    
      Serial.println("--- CODE 1 ---");
      sendCode(code1);
      Serial.println("terminated");
      blinkLed(ledRed, 1, 1000);
    
      Serial.println("--- CODE 2 ---");
      sendCode(code2);
      Serial.println("terminated");
      blinkLed(ledRed, 1, 1000);
      
      Serial.println("--- CODE 3 ---");
      sendCode(code3);
      Serial.println("terminated");
      blinkLed(ledRed, 1, 1000);  
    }
    
    
    void sendCode ( unsigned long sendCode) {
      Serial.print("Sending code (20sec): ");
      Serial.println( sendCode );
      previousMillis = millis();
      unsigned long lastLedBlink = millis();
      boolean ledStatus = false;
    
      while ( millis() < ( previousMillis + 20000 ) ) {
        RFKey.send( sendCode, 24 );
        blinkLed(ledGreen, 1, 50);
      }
    
    }
    
    ////////////////////////////////////////////////////////////////////////////
    
    void updateCode() {                                          // chandge radio code
      Serial.println("change radio code");
    
      //unsigned long randomCode1 = random (1000000,9999999);
      unsigned long randomCode1 = random (1000,4094);          // 12 bit
     // randomCode1 = 3618;
      Serial.print ("Codice random1 : ");
      Serial.println(randomCode1);
    
      //unsigned long randomCode2 = random (1000000,9999999);
      unsigned long randomCode2 = random (1000,4094);          // 12 bit  
     // randomCode2 = 2769;
      Serial.print ("Codice random2 : ");
      Serial.println(randomCode2);
    
      //unsigned long randomCode3 = random (1000000,9999999);
      unsigned long randomCode3 = random (1000,4094);          // 12 bit
     // randomCode3 = 1720;
      Serial.print ("Codice random3 : ");
      Serial.println(randomCode3);
    
    
      EEPROM.writeLong( 50 , randomCode1 ); 
      EEPROM.writeLong( 100 , randomCode2 );
      EEPROM.writeLong( 150 , randomCode3 );   
      blinkLed(ledGreen, 1, 3000); 
    }
    
    ////////////////////////////////////////////////////////////////////////////
    
    void checkCode() {                                            // check received code
      unsigned long receivedCode = RFKey.getReceivedValue();
      unsigned long code1 = EEPROM.readLong( 50 ); 
      unsigned long code2 = EEPROM.readLong( 100 ); 
    
      if ( receivedCode == code1 ){       // check first code
        blinkLed(ledGreen, 1, 100);
        Serial.println ("first code unlocked");
        RFKey.resetAvailable();
        unsigned long checkTime = millis();
        receivedCode = 0;
        while ( millis() < (checkTime + 3000) ) {
          //    Serial.println ("while");
          if ( RFKey.available() ) {
            receivedCode = RFKey.getReceivedValue();
            if ( receivedCode == code2 ){                //check second code
              // Serial.println ("second code unlocked");
              RFKey.resetAvailable();
              Serial.println("*** valid code received ***");
              //          ALstatus = !ALstatus;
              //          return true;
            }
          }
        }
      }
     
      else{
        RFKey.resetAvailable();
        delay (1000);
        Serial.println("*** invalid code ***");
        Serial.print("received code: ");
        Serial.println(receivedCode);
        //  return false;
      }
    }
    
    ////////////////////////////////////////////////////////////////////////////
    
    void blinkLed(byte ledPin, byte times, int Delay){
      for ( int i = 0; i < times; i++){
        digitalWrite (ledPin, HIGH);
        delay (Delay);
        digitalWrite (ledPin, LOW);
        delay (Delay);
      }
    }
    
    ////////////////////////////////////////////////////////////////////////////
    
    
    
    /*
    Instruction
     press PROG button to send code for keyfob programming
     hold PROG about 10 sec. to reset and generate new code for keyfob programming
     */
    
    
    
    
    

Log in to reply
 

Suggested Topics

302
Online

9.1k
Users

9.8k
Topics

102.4k
Posts