Arduino: Microchip Keeloq Retransmit Garage Door Code

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

 

 

Randomnerdtutorials: ESP8266 DS18B20 Temperature Sensor Web Server with Arduino IDE

 

ESP8266 DS18B20 Temperature Sensor Web Server with Arduino IDE – output XML data

 

/*
This example code is in the public domain.
*/

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin D1 on the ESP8266 12-E - GPIO 5
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature DS18B20(&oneWire);
char temperatureCString[7];
char temperatureFString[7];

ESP8266WebServer server(80);
const char *ssid = "KMTRONIC";
const char *password = "12345678";
String XML;

void buildXML() {
 // <response><sensor><id>10000000000001</id><temp>26.37</temp></sensor></response>
 getTemperature();
 XML = "<?xml version='1.0'?>";
 XML += "<response>";
 XML += "<sensor><id>10000000000001</id><temp>";
 XML += temperatureCString;
 XML += "</temp></sensor>"; //incoming data from Arduino. It means it is the value of mapping function.
 XML += "</response>";
}

void handleXML() {
 buildXML();
 server.send(200,"text/xml",XML);
}

void getTemperature() {
 float tempC;
 float tempF;
 do {
 DS18B20.requestTemperatures(); 
 tempC = DS18B20.getTempCByIndex(0);
 dtostrf(tempC, 2, 2, temperatureCString);
 tempF = DS18B20.getTempFByIndex(0);
 dtostrf(tempF, 3, 2, temperatureFString);
 delay(100);
 } while (tempC == 85.0 || tempC == (-127.0));
}

void setup() {

DS18B20.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement

Serial.begin(9600); 
 WiFi.begin(ssid,password); //trying to connect the modem
 while(WiFi.status() != WL_CONNECTED) {
 delay(500);
 }
 WiFi.mode(WIFI_STA);
 Serial.println("\n\n. . . BOOTING ESP8266 . . .\n");
 Serial.print("\n");
 Serial.print("CONNECTED TO ACCESS POINT : ");
 Serial.println(ssid);
 Serial.flush();
 Serial.println(WiFi.localIP());
 //it is sending the IP to Arduino's port. So I can see the IP on Serial Monitor
 Serial.flush();
 server.on("/status.xml",handleXML);
 server.begin(); 
}

void loop() {
 server.handleClient();
}

 

For test hardware used:

ESP8266 DS18B20 Temperature Sensor Web Server with Arduino IDE

 

 

XML Output

 

 if (c == '\n' && blank_line) {
 getTemperature();
 client.println(F("<?xml version = \"1.0\" encoding=\"UTF-8\"?>"));
 client.println(F("<response>"));
 client.println(F("<sensor>"));
 client.println(F("<id>10000000000001</id>"));
 client.println(F("<temp>"));
 client.println(temperatureCString);
 client.println(F("</temp>"));
 client.println(F("</sensor>"));
 client.println(F("</response>")); 
 break;
 }

 

PHP file for parse temperature

<?php
// Loading the XML file
 $xml = simplexml_load_file("http://192.168.1.166/status.xml");

   foreach($xml->children() as $sensor)
   {
   echo "\r\nTemperature : ".$sensor->temp."\r\n";
   echo "\r\n";
   }
?>

 

 

 

 

Modbus slave example 2: Arduino float number into HEX ModBus register

 

 

 

 

/**
 * Modbus slave example 2:
 * The purpose of this example is to link the Arduino digital and analog
 * pins to an external device.
 *
 * Recommended Modbus Master: QModbus
 * http://qmodbus.sourceforge.net/
 */

#include <ModbusRtu.h>
#define ID 1

Modbus slave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDI
boolean led;
int8_t state = 0;
unsigned long tempus;

// data array for modbus network sharing
uint16_t au16data[9];

void setup() {
 
 // start communication
 slave.begin( 19200 );
 tempus = millis() + 100;
 digitalWrite(13, HIGH );
}

/**
 * Loop procedure
 */
void loop() {
 // poll messages
 // blink led pin on each valid message
 state = slave.poll( au16data, 9 );

if (state > 4) {
 tempus = millis() + 50;
 digitalWrite(13, HIGH);
 }
 if (millis() > tempus) digitalWrite(13, LOW );

// Modbus array
 io_poll();
}

void io_poll() {

// Energy Meter voltage
 // 43 67 87 61
 au16data[5] = 0x4367;
 au16data[6] = 0x8761;

}

 

 

 

/**
 * Modbus slave example 2:
 * The purpose of this example is to link the Arduino digital and analog
 * pins to an external device.
 *
 * Recommended Modbus Master: QModbus
 * http://qmodbus.sourceforge.net/
 */

#include <ModbusRtu.h>
#define ID 1

union fsend_t{
 float f;
 uint16_t u[2];
};


#include <OneWire.h>
int DS18S20_Pin = 10; //DS18S20 Signal pin on digital 10
OneWire ds(DS18S20_Pin); // on digital pin 10

Modbus slave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDI
boolean led;
int8_t state = 0;
unsigned long tempus;

// data array for modbus network sharing
uint16_t au16data[9];

/**
 * Setup procedure
 */
void setup() {
 io_setup(); // I/O settings

// start communication
 slave.begin( 9600 );
 tempus = millis() + 100;
 digitalWrite(13, HIGH );
}

/**
 * Loop procedure
 */
void loop() {
 // poll messages
 // blink led pin on each valid message
 state = slave.poll( au16data, 9 );

if (state > 4) {
 tempus = millis() + 50;
 digitalWrite(13, HIGH);
 }
 if (millis() > tempus) digitalWrite(13, LOW );

// link the Arduino pins to the Modbus array
 io_poll();
}


void io_setup() {
 // define i/o

digitalWrite(13, HIGH ); // this is for the UNO led pin

}

/**
 * Link between the Arduino pins and the Modbus array
 */
void io_poll() {

union fsend_t floatConvert;
 
 floatConvert.f = getTemp();

au16data[5] = floatConvert.u[1];
 au16data[6] = floatConvert.u[0];

// diagnose communication
 au16data[6] = slave.getInCnt();
 au16data[7] = slave.getOutCnt();
 au16data[8] = slave.getErrCnt();
}


float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

//delay(100);

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}

 

Arduino float number into HEX ModBus register
Arduino float number into HEX ModBus register