Skip to content

Commit

Permalink
Merge pull request #1 from alxsmora1/release/1.0.0
Browse files Browse the repository at this point in the history
Release/1.0.0
  • Loading branch information
alxsmora1 authored Aug 15, 2020
2 parents e17d97e + ebc99f9 commit 5472243
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Raspberry Pi Wiegand Reader

### Descripción
Codigo para la lectura de tags/tarjetas RFID mediante una lectora de RFID conectada a una Raspberry Pi con Pigpio.

### Requisitos
* Python 2.7
* [Pigpio](http://abyz.me.uk/rpi/pigpio/download.html)

### Instalación de Pigpio
$ sudo apt-get update
$ sudo apt-get install pigpio python-pigpio python3-pigpio


### Instrucciones de uso

$ sudo pigpiod
$ git clone https://github.com/alxsmora1/rasberrypi_wiegand.git
$ cd raspberrypi_wiegand
$ python wiegand.py


### Metodo de conexión
* GPIO (BCM)
* Se necesita conectar el cable verde (D0) en el GPIO 14 y el cable blanco (D1) en el GPIO 15.
* Tambien es requerido conectar la tierra (GND) de la lectora a la Raspberry en un pin GND, de lo contrario esto genera que haya ruido durante la lectura y los datos no puedan ser interpretados correcatmente.
114 changes: 114 additions & 0 deletions wiegand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env python

import pigpio

class decoder:

"""
A class to read Wiegand codes of an arbitrary length.
The code length and value are returned.
"""

def __init__(self, pi, gpio_0, gpio_1, callback, bit_timeout=6):

"""
Instantiate with the pi, gpio for 0 (green wire), the gpio for 1
(white wire), the callback function, and the bit timeout in
milliseconds which indicates the end of a code.
The callback is passed the code length in bits and the value.
"""

self.pi = pi
self.gpio_0 = gpio_0
self.gpio_1 = gpio_1

self.callback = callback

self.bit_timeout = bit_timeout

self.in_code = False

self.pi.set_mode(gpio_0, pigpio.INPUT)
self.pi.set_mode(gpio_1, pigpio.INPUT)

self.pi.set_pull_up_down(gpio_0, pigpio.PUD_UP)
self.pi.set_pull_up_down(gpio_1, pigpio.PUD_UP)

self.cb_0 = self.pi.callback(gpio_0, pigpio.FALLING_EDGE, self._cb)
self.cb_1 = self.pi.callback(gpio_1, pigpio.FALLING_EDGE, self._cb)

def _cb(self, gpio, level, tick):

"""
Accumulate bits until both gpios 0 and 1 timeout.
"""

if level < pigpio.TIMEOUT:

if self.in_code == False:
self.bits = 1
self.num = 0

self.in_code = True
self.code_timeout = 0
self.pi.set_watchdog(self.gpio_0, self.bit_timeout)
self.pi.set_watchdog(self.gpio_1, self.bit_timeout)
else:
self.bits += 1
self.num = self.num << 1

if gpio == self.gpio_0:
self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout
else:
self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout
self.num = self.num | 1

else:

if self.in_code:

if gpio == self.gpio_0:
self.code_timeout = self.code_timeout | 1 # timeout gpio 0
else:
self.code_timeout = self.code_timeout | 2 # timeout gpio 1

if self.code_timeout == 3: # both gpios timed out
self.pi.set_watchdog(self.gpio_0, 0)
self.pi.set_watchdog(self.gpio_1, 0)
self.in_code = False
self.callback(self.bits, self.num)

def cancel(self):

"""
Cancel the Wiegand decoder.
"""

self.cb_0.cancel()
self.cb_1.cancel()

if __name__ == "__main__":

import time

import pigpio

import wiegand

def callback(bits, value):
print("bits={} value={:026b}".format(bits, value))
card_id = int("{:026b}".format(value)[1:25],2)
print("Card ID: {:010d}".format(card_id))

pi = pigpio.pi()

w = wiegand.decoder(pi, 14, 15, callback)

time.sleep(1000)

w.cancel()

pi.stop()

0 comments on commit 5472243

Please sign in to comment.