3

(coco) #1
FORGE


  1. Unlock the door by switching the GPIO pin to
    HIGH, which triggers the relay

  2. Wait for five seconds to allow time for the
    door to be opened

  3. Print another line of text saying the door is
    now being locked again

  4. Lock the door again by switching the GPIO pin
    back to LOW


If the data read from the fob isn’t the data we’re
looking for, then we do nothing apart from print a
line of text saying ‘Access Denied’ to the Terminal.
After either outcome, it’s extremely important to
remember to clear out the rfid_presented variable
so that we’re starting afresh next time ready for the
next unlock attempt.
Before those lines of code which control the GPIO
pin will work, you first need to import the RPi.GPIO
module for use in your script, as well as the time
module which will enable the sleep function to work.
To do this, go back to the top of your script and
underneath the two imports you already have, add
these lines:


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)

As well as importing the two extra Python
modules you need, this will also configure and
initialise the GPIO pins ready for use.
With your Python script now complete, save and
exit by pressing CTRL+X to exit and then pressing Y
to save changes. Then to load your program, type:


python lock.py

THE MOMENT OF TRUTH – DOES IT WORK?
If you hold the authorised key fob to your reader, you
should immediately hear the relay ‘click’ and if you try
the striker plate, it will allow itself to be opened. After
five seconds, you’ll hear another click and the striker
plate will no longer open. Then try another fob and
you should be met with a blank wall of silence, and an
Access Denied message printed to the Terminal.
That’s all there is to it! You now have a
sophisticated security system for whichever room
(or cupboard!) that you’re securing. What’s more,


since it uses a Raspberry Pi which can be connected
to the internet, it’s full of possibilities for expansion.
You could write an app to unlock the door remotely
by simply toggling the GPIO pin between high and
low, or you could add a keypad to require a PIN to
accompany the NFC tag presented. If you build one
yourself, be sure to share it with us so we can see
what you’ve come up with!

Left
The Python script
prints text to the
Terminal, showing
when the door is
unlocked and locked
again, as well as
when access has
been denied

Below
Once you’re done,
you can install the
electronic striker
plate into the
doorway that you
want to secure with
NFC fob access!
Free download pdf