3

(coco) #1

Build an NFC-powered door lock


TUTORIAL


#!/usr/bin/env python3

from evdev import InputDevice
from select import select
The first line specifies the environment in which
you’ll be working and that you want to use Python 3.
Then, we need to import InputDevice and select,
both of which will allow us to read the data from the
RFID fobs.
Next, add the following three lines of code:
rfid_presented = “”
keys = “X^1234567890XXXXqwertzuiopXXXXasdfghjkl
XXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX”
dev = InputDevice(‘/dev/input/event0’)

The first line creates and initialises an empty variable
in which we’ll store the characters that make up the
data string from the key fob, one by one. The second
line defines a string of characters which later code
will refer to in order to establish which character has
been typed. The third line defines your input device
so the code knows which to listen to. You’ll need
to check what ID your input device has been given
by the Raspbian operating system and modify your
code accordingly. To do this, type the following into a
Terminal window:
ls -lah /dev/input/by-id

You should see a list of all your Raspberry Pi’s
input devices, with a symlink for each one giving it a
friendlier name. Look for the long name which is clearly
your RFID reader – in this case it’s ‘event0’ (because
I’m shelled into the Pi via SSH and there are no other
USB devices attached, such as a keyboard and mouse).

ALWAYS LISTENING...
The system needs to continuously listen for input from
the reader, so we achieve this by defining an infinite
loop like this:
while True:

Everything else in the program takes place within
that loop, so it’s now constantly listening for an event.

To handle the event, add these lines to the while loop:
while True:
r,w,x = select([dev], [], [])
for event in dev.read():
if event.type==1 and event.value==1:
if event.code==28:
if rfid_presented==”0001155844”:
# Unlock Door
print(“Unlocking Door.”)
GPIO.output(13,GPIO.HIGH)

time.sleep(5)

# Lock Door again
print(“Locking Door Again.”)
GPIO.output(13,GPIO.LOW)
else:
print(“Access Denied.”)

rfid_presented = “”
else:
rfid_presented += keys[ event.code ]
Inside this loop, we’re listening for events of type
1 which means a (virtual) key has been pressed
and a character entered. Each time that happens,
we look at the event code to see if it’s 28, which
corresponds to the newline character, which we
know from before means the end of the data being
read from the fob. If the data that has been entered
isn’t a newline, we take that data and add it to
the rfid_presented variable using the ‘+=’ syntax,
gradually building it up with a character at a time to
create the string of text. Once we get a character
which is 28, i.e. the newline, we stop building the
text string up and see if it’s the data that we’re after.
For the sake of clarity in this example, I’ve hard-
coded the data we’re looking for into the if/else
statements (see the ‘Building in flexibility’ box for a
better real-world alternative). You’ll obviously need
to substitute that with the data from your own key
fob. If the string read from the key fob is the one
we’re looking for, then we jump into action and do
five things:


  1. Print a line of text to the Terminal window
    saying what we’re doing (which is very useful
    for debugging)


Instead of hard-coding the data from the RFID fob into your code, consider storing a list
of authorised codes in a MySQL database instead, and modify the Python code with an
SQL query to look up the presented code in the database to check validity. This opens
up a world of possibilities; you could create a web interface to manage access remotely
and assign owners’ names to fobs, or you could add extra columns to your database
table to restrict access to certain days or times... ideal for cleaners or dog walkers, etc.

BUILDING IN FLEXIBILITY


Right
The RFID reader
inputs data it reads
from the NFC tags
(key fobs) into the
operating system
like a keyboard
inputting text
Free download pdf