ESP8266: Yet Another Arduino Wiegand Library

 

Yet Another Arduino Wiegand Library

 

 

#define PIN_D0 D1
#define PIN_D1 D2

// The object that handles the wiegand protocol
Wiegand wiegand;

// Initialize Wiegand reader
void setup() {
Serial.begin(9600);
delay(1);
Serial.println("Start...");

//Install listeners and initialize Wiegand reader
wiegand.onReceive(receivedData, "Card readed: ");
wiegand.onStateChange(stateChanged, "State changed: ");
wiegand.begin(WIEGAND_LENGTH_AUTO);

//initialize pins as INPUT
pinMode(PIN_D0, INPUT);
pinMode(PIN_D1, INPUT);
}

// Continuously checks for pending messages and polls updates from the wiegand inputs
void loop() {
// Checks for pending messages 
//Serial.println("Start2 ..");
wiegand.flush();

// Check for changes on the the wiegand input pins
wiegand.setPin0State(digitalRead(PIN_D0));
wiegand.setPin1State(digitalRead(PIN_D1));
}

// Notifies when a reader has been connected or disconnected.
// Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onStateChange()`
void stateChanged(bool plugged, const char* message) {
Serial.print(message);
Serial.println(plugged ? "CONNECTED" : "DISCONNECTED");
}

// Notifies when a card was read.
// Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onReceive()`
void receivedData(uint8_t* data, uint8_t bits, const char* message) {
Serial.print(message);

//Print value in HEX
uint8_t bytes = (bits+7)/8;
for (int i=0; i<bytes; i++) {
Serial.print(data[i] >> 4, 16);
Serial.print(data[i] & 0xF, 16);
}
Serial.println();
}

 

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