LAN DS18B20 WEB Temperature Monitor 1 meter Cable
www.WEB-Relays.com
www.Facebook.com/KMtronic

Arduino: Microchip Keeloq Retransmit Garage Door Code

Spread the love

Arduino: Microchip Keeloq Retransmit Garage Door Code

 

// Taken from http://arduino.ru/forum/apparatnye-voprosy/biblioteka-chteniya-id-brelkov-signalizatsiii-hcs301-keeloq?page=1

#include "math.h"

#define txPin 8
#define rxPin 7
#define signalPin 6

int lastRxValue = 0;
int tempRxValue = 0;
unsigned long lastRxTime = 0;
unsigned long tempTime = 0;
unsigned long difTime = 0;
boolean bValidPacket = false;
int decodeMethod = 1; //0 real - as in manual, 1 inver as Oleg do


//Global vars end
void send_meander(int time){
 digitalWrite(txPin, HIGH);
 delayMicroseconds(time);
 digitalWrite(txPin, LOW);
 delayMicroseconds(time);
}
//keelog start
int keelog_state = 0;
int keelogCounter = 0;
byte keelog_code[9];

void keelog_vardump(){
 if(decodeMethod == 0){
 Serial.println(" - keelog origin - ");
 }
 else{
 Serial.println(" - keelog invert - ");
 }
 Serial.print(keelog_code[0], HEX);
 Serial.print(keelog_code[1], HEX);
 Serial.print(keelog_code[2], HEX);
 Serial.print(keelog_code[3], HEX);
 Serial.println("-hop");
 Serial.print(keelog_code[4], HEX);
 Serial.print(keelog_code[5], HEX);
 Serial.print(keelog_code[6], HEX);
 Serial.println("-fix");
 Serial.print(keelog_code[7], HEX);
 Serial.println("-btn");
 Serial.print(keelog_code[8], HEX);
 Serial.println("-dop");
 
 /*for(int i = 0; i<9; i++){
 Serial.print(keelog_code[i], HEX);
 Serial.print(" - ");
 }*/
 keelog_send(keelog_code);
 digitalWrite(signalPin, HIGH);
 delay(100);
 digitalWrite(signalPin, LOW);
 keelog_state = 0;
 for(int i = 0; i<9; i++){
 keelog_code[i]=0;
 }
}
void keelog_send(byte* keelog_code){
 Serial.println("- sending keelog -");
 for(int i = 0; i<9; i++){
 Serial.print(keelog_code[i], HEX);
 Serial.print(" - ");
 }
 for(int i = 0; i<11; i++){//посылаем преамблу
 send_meander(400);
 }
 digitalWrite(txPin, HIGH);
 delayMicroseconds(400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(4000);//посылаем хедер
 
 for( int i = 0; i<9; i++){
 if(decodeMethod==1){
 for(int i2 = 7;i2>=0;i2--){
 if(bitRead(keelog_code[i], i2)){
 digitalWrite(txPin, HIGH);
 delayMicroseconds(400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(2*400);
 }
 else{
 digitalWrite(txPin, HIGH);
 delayMicroseconds(2*400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(400);
 }
 }
 }
 else{
 for(int i2 = 0;i2<8;i2++){
 if(!bitRead(keelog_code[i], i2)){
 digitalWrite(txPin, HIGH);
 delayMicroseconds(400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(2*400);
 }
 else{
 digitalWrite(txPin, HIGH);
 delayMicroseconds(2*400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(400);
 }
 } 
 }
 
 }
}
void keelog_get(){
 bValidPacket = false;
 if(keelog_state==0){//ждем преамбулу и хедер
 if(difTime > 280 && difTime < 620 && lastRxValue != tempRxValue){
 keelogCounter ++;
 }
 else{
 if(keelogCounter==23){
 if(difTime>2800 && difTime<6200 && lastRxValue == 0){
 keelog_state=1;
 }
 }
 keelogCounter = 0;
 }
 }
 else if(keelog_state==1){// получаем биты
 if(difTime > 560 && difTime < 1240 && lastRxValue == 1){// получили 1
 if(decodeMethod==0){
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]>>1)|B10000000;
 }
 else{
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]<<1)|B00000000;
 }
 bValidPacket = true;
 }
 else if(difTime > 280 && difTime < 620 && lastRxValue == 1){
 if(decodeMethod==0){
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]>>1)|B00000000;
 }
 else{
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]<<1)|B00000001;
 }
 bValidPacket = true;
 }
 else if(lastRxValue == 0){
 }
 else{
 keelog_state=1;
 keelogCounter = 0;
 }
 
 if(bValidPacket){
 keelogCounter++;
 if(keelogCounter==66){
 keelog_vardump();
 keelogCounter = 0;
 keelog_state = 0;
 }
 }
 }
}
//keelog end


void setup() { 
 pinMode(txPin, OUTPUT);
 pinMode(rxPin, INPUT);
 pinMode(signalPin, OUTPUT);
 //
 Serial.begin(9600);
 Serial.println("start");
 lastRxValue = digitalRead(rxPin);
 lastRxTime = micros();
 
 //attachInterrupt(0, grab, CHANGE);
}

void loop() {
 tempRxValue = digitalRead(rxPin);
 
 if(tempRxValue != lastRxValue){
 tempTime = micros();
 difTime = tempTime - lastRxTime;
 keelog_get(); 
 lastRxTime = tempTime;
 lastRxValue = tempRxValue;
 }
}

 

LOG:

 - keelog invert - 
F9A4C3E-hop
89FA0-fix
84-btn
2-dop
- sending keelog -
F - 9A - 4C - 3E - 89 - FA - 0 - 84 - 2 -

 

Arduino: Microchip Keeloq Retransmit Garage Door Code
Arduino: Microchip Keeloq Retransmit Garage Door Code

 

 

KMtronic LAN WEB 8 Channel Relay board