Skip to content

Instantly share code, notes, and snippets.

@svdgraaf
Last active November 4, 2018 16:46
Show Gist options
  • Save svdgraaf/7517993 to your computer and use it in GitHub Desktop.
Save svdgraaf/7517993 to your computer and use it in GitHub Desktop.
Raspi RFID scanner
{
"170038E91C": "1003729691",
"17003F5A1D": "1004151834"
}
ipython==1.1.0
python-ldap==2.4.13
requests==2.0.1
import logging
import pifacedigitalio
from evdev import InputDevice, categorize, ecodes
from utils import switch_relay, check_id
# setup logging
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',level=logging.DEBUG)
logging.info('rfid scanner starting')
# connect to the piface
p = pifacedigitalio.PiFaceDigital()
p.relays[0].turn_off()
logging.info('Turned relay off')
# connect to rfid device for events you can find the correct device with `lsusb`
dev = InputDevice('/dev/input/event0')
badge_no = ''
logging.info('Listening for rfid events')
# every time a card is scanned, a 'keystroke event' will be fired for every character of the card, we need to
# listen for this, and group all the events together, untill we receive a newline (enter) key. Then we can
# check all the previous characters in 'badge_no' if it was correct
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
event_cat = categorize(event)
if event_cat.keystate:
# if we received an ENTER, we check the previous string since
# the last time an ENTER was received
if event_cat.keycode == 'KEY_KPENTER':
logging.info('Received %s' % badge_no)
# is access granted?
if check_id(badge_no):
switch_relay(p.relays[0])
# reset for new input
badge_no = ''
else:
key = event_cat.keycode
key = key.replace('KEY_', '')
badge_no = badge_no + key
[program:scanner]
command=/home/svdgraaf/rfid/scanner.py
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/rfid-scanner.log
stderr_logfile=/var/log/rfid-scanner.error.log
directory=/home/svdgraaf/rifd/
import logging
import time
import json
import requests
# setup logging
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',level=logging.DEBUG)
# if the access_string is in the cards file, access is granted
def check_id(access_string):
json_data=open('cards.json')
data = json.load(json_data)
if access_string in data:
logging.info('ACCESS GRANTED')
return True
else:
logging.info('ACCESS DENIED')
return False
# open the correct relay
def switch_relay(relay):
logging.info('Opening Relay')
relay.turn_on()
# keep relay open for 3 secs
time.sleep(3)
logging.info('Closing Relay')
# close the relay
relay.turn_off()
return True
@LuisFDuarte
Copy link

Hi, really interesting project I want to do something similar with RFID and Raspberry Pi thanks for this, by the way where you find a fritzing part for PifaceDigital?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment