RFID ac­cess sys­tem

Night watch­man Les Pounder shows us how to make our own se­cu­rity sys­tem us­ing a Rasp­berry Pi, a mag­netic lock and an RFID reader.

Linux Format - - CONTENTS -

It’s lock­down at LXF Tow­ers as Les Pounder wields a Pi, a mag­netic lock and an RFID reader.

For this month’s project we shall build our own door se­cu­rity sys­tem, which can be used to log who en­ters a prop­erty us­ing in­ex­pen­sive RFID (ra­dio fre­quency iden­ti­fi­ca­tion) cards. These RFID cards are read us­ing an RFID reader – the RFID-RC522 – which gives a unique ID for each card. This is checked against a dic­tionary of ac­cepted cards, and if found a re­lay is trig­gered, dis­con­nect­ing a 12V cir­cuit used to power the mag­netic lock. Watch your fin­gers, though: the mag­net is strong and can sup­ply 280kg of force!

High and low

There are two parts to this project: the low-volt­age cir­cuit for the Rasp­berry Pi, and the higher-volt­age 12V cir­cuit for the mag­netic door lock. The Pi is con­nected to the RFID reader us­ing the SPI in­ter­face on the GPIO. Our re­lease but­ton for the door is also on this cir­cuit. We con­nect a re­lay board to the Pi, pro­vid­ing it with 5V and GND, and then con­nect GPIO17 of our Pi to IN1 on the re­lay board. This en­ables the Pi to trig­ger the re­lay and re­main iso­lated from the 12V cir­cuit.

The 12V cir­cuit to the re­lay is the 12V+ power from a 12V DC sup­ply. This is con­nected to the COM ter­mi­nal, the cen­tre ter­mi­nal. And a con­nec­tion is made from the NC (nor­mally closed) ter­mi­nal to the mag­netic door lock. The 12V GND con­nec­tion is made from the DC power sup­ply to the GND of the mag­netic lock. So when the re­lay is trig­gered, the DC12V+ con­nec­tion is made to the door lock, caus­ing the mag­net to en­er­gise and lock the door. See the di­a­gram ( topright).

There are quite a few li­braries that re­quire in­stal­la­tion for this project. The first is SPI-Py, specif­i­cally a fork cre­ated by Si­mon Monk. This li­brary re­quires man­ual in­stal­la­tion via “git clone” be­cause it’s not in the “pip” repos­i­to­ries, so we also need to in­stall logzero, a sim­ple Python log­ging tool that can write to ex­ter­nal files and pro­vide eas­ily iden­ti­fi­able out­put in the Python shell. So to make soft­ware in­stal­la­tion straight­for­ward, we’ve writ­ten a small bash script that be down­loaded and run in the ter­mi­nal us­ing the fol­low­ing: $ curl https://raw.githubuser­con­tent.com/lesp/LXF230-RFIDDoor-Lock/master/in­stall.sh | bash

We also need to en­able the SPI in­ter­face. So on the Rasp­bian desk­top, go to the main menu and nav­i­gate to Pref­er­ences, then se­lect Rasp­berry Pi Con­fig­u­ra­tion. In the new menu, look for In­ter­faces and then en­able the SPI in­ter­face. Click OK to make the change. Now is a good time to re­boot the Pi and return to the desk­top.

Writ­ing the code

In Thonny, click File>Save and call the project RFID-se­cu­rity. py and save the file in the LXF230-RFID-Door-Lock di­rec­tory. Re­mem­ber to save your code as you go.

Our first few lines of Python are the li­braries. First, we im­port the But­ton and Out­putDe­vice classes from GPIO Zero. The but­ton class is self-ev­i­dent, but the Out­putDe­vice is used to con­nect the Rasp­berry Pi to the re­lay. The next two li­braries are Sim­pleMFRC522 and time. Again, time is ob­vi­ous, but the for­mer is used to con­nect and read the data from the RFIDRC522 reader. The fi­nal two im­ports re­late to logzero. This is the Python li­brary that pro­vides an easy way to cre­ate log­files, and to for­mat the data that’s out­put to the Python shell: from gpi­ozero im­port But­ton, Out­putDe­vice im­port Sim­pleMFRC522 im­port time im­port logzero from logzero im­port log­ger

Now let’s cre­ate three ob­jects that we will be us­ing to con­nect our re­lay board to GPIO 17 of the Rasp­berry Pi, then cre­ate a re­lease but­ton on GPIO27, and cre­ate a con­nec­tion to the RFID reader: re­lay = Out­putDe­vice(17) re­lease = But­ton(27) reader = Sim­pleMFRC522.Sim­pleMFRC522()

The next line of code uses the re­lay ob­ject that we’ve just cre­ated. This will turn the GPIO pin on and that will trig­ger our re­lay to click into life. On the other side of the re­lay is our 12V cir­cuit, in this case the 12V + power con­nec­tion, which is con­nected to the re­lay as “Nor­mally Closed”, NC. This means that when the re­lay is turned on, the cir­cuit is ac­tive and power can flow to the mag­netic door lock, trig­ger­ing it to lock. re­lay.on()

Now, to log all of the events for en­ter­ing and leav­ing the build­ing we cre­ate a log­file us­ing logzero. This is called

ac­cess.log and it has a max­i­mum size of 1e6, 1MB. It also cre­ates up to three back­ups of the log via a ro­tat­ing log file: logzero.log­file("ac­cess.log”, maxBytes=1e6, back­upCount=3)

In order to un­lock the mag­netic door lock, we need to write a func­tion that will take the user id for the card pre­sented to the reader and write it to the log. Our func­tion “open­door” takes one ar­gu­ment, the id of the card. This is passed to the log­ger and writ­ten to the log us­ing the for­mat “info” which is colour-coded green in the shell out­put.

We then write the name of the user to the log, us­ing a dic­tionary called “cards” and the key, which is the id of the card. More on this later. The next step is to turn the re­lay off, break­ing the 12V cir­cuit and turn­ing off the mag­netic lock, and al­low­ing the user to en­ter. We then print that the door is open to the Python shell. The code then waits for five se­conds be­fore turn­ing the 12V cir­cuit back on and lock­ing the door.

def open­door(id): log­ger.info(id) log­ger.info("The user is “+str(cards[id])) re­lay.off() print("DOOR OPEN") time.sleep(5) re­lay.on() print("DOOR LOCKED")

We now cre­ate an­other func­tion to han­dle the sit­u­a­tion when the re­lease but­ton is pressed. It will log that the door has been opened, and turn off the re­lay to re­lease the mag­net in the same man­ner as us­ing the RFID card.

def but­ton_open(): log­ger.info("Door Re­lease Pressed") re­lay.off() print("DOOR OPEN") time.sleep(5) re­lay.on() print("DOOR LOCKED")

In order to have a “list” of au­tho­rised users, we’re go­ing to cre­ate a dic­tionary, called “cards”. A dic­tionary is a data stor­age type which uses a key, value re­la­tion­ship. So when we see the user id from the RFID card, it’s used as a key to search the dic­tionary and return the value for that key, in this case the user name. The id num­bers in this dic­tionary are found by scan­ning the tags across the reader, some­thing you can learn more about in the box­out ( above).

cards = {642581926087: “John Smith”,572050203913: “Sarah Rogers"}

Mov­ing on to the main loop of our project. We use a while True loop to con­stantly run the code within. The first task is to check if the re­lease but­ton has been pressed and if so, to run the but­ton_open func­tion we cre­ated ear­lier. No­tice that the func­tion call has no paren­the­ses be­cause that would call the func­tion every time the loop it­er­ates, and not when the but­ton is pressed:

while True: re­lease.when_­pressed = but­ton_open

Next, we in­tro­duce a 0.1 sec­ond de­lay to pace the code, then we cre­ate two vari­ables: “id” and “text”. The id vari­able is used to store the id of the RFID card pre­sented to the reader. We then print the id to the shell:

time.sleep(0.1) id, text = reader.read() print(id)

In order to check the id of the pre­sented card against our dic­tionary, we need to use an “if” con­di­tional test. This will check to see “if the id is in the dic­tionary called cards” and if that’s cor­rect then the open­door(id) func­tion we cre­ated will be run, open­ing the door and log­ging the user de­tails to the ac­cess log. But if the user is un­known then the door re­mains locked and the de­tails are recorded on the log:

if id in cards: open­door(id) else: print("Un­known Card") log­ger.er­ror(id)

With the code now com­pleted, save your work and click Run>Run Cur­rent Script to start the project. Now present one of the cards to the reader and if it’s in the dic­tionary, the door will un­lock. If it isn’t then you can add it – just fol­low the box­out ( above) for help. LXF

The cir­cuit is split into two sec­tions: the low volt­age cir­cuit con­nected to the Pi, and the higher volt­age cir­cuit con­nected to our re­lay and mag­netic door lock. Con­nects to 12V power sup­ply Con­nects to 12V+ ter­mi­nal of the mag­netic door lock Con­nects to 12V GND ter­mi­nal of mag­netic door lock

Re­lays come in all shapes and sizes. We used a four-chan­nel re­lay board that cost just $4.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.