Software apps and online services
Hand tools and fabrication machines
In this DIY gadget build, we’ll be creating a secure door entry system using a Raspberry Pi.
You already may be familiar with two-factor authentication for logging into secure systems like your online banking or your Facebook or email accounts. This door entry system goes one step further and uses THREE factor authentication. To unlock the door, you require;
- An RFID keyfob
- A 6-digit PIN
- A random one-time access code which is generated and sent as a text to your phone.
- A Raspberry Pi. I’m using a Raspberry Pi 3 with built in WiFi and a case which helps with protection and mounting in the wall
- A USB RFID reader. This one simply plugs in via USB and acts like a keyboard inputting the code from the RFID fobs and cards
- A ribbon cable to attach the expansion board
- A female to male ribbon cable for attaching the touchscreen
- A 5V single channel relay switch
- A computer power supply and power lead (to power both the Raspberry Pi and the Electronic Door Strike)
- Some dupont cables for attaching the relay switch
You’ll also need a few tools, so make sure you’ve got all of the following before you get started:
- A multimeter, for checking voltages
- Various sizes of screwdrivers
- Some choc blocks, for connecting wires together
- A glue gun
- A jigsaw
The very first thing we need to do is get our LCD touchscreen working. Depending on which model you purchase for your build, it may just come with drivers to install or it may come with its own Operating System image for you to write to your Raspberry Pi’s SD card. That’s the case with my one, so I need to use Win32 Disk Imager to write the image to my SD card. Make sure you select the correct device to write to, so you don’t accidentally erase any external hard drives!
Once that’s complete, pop the microSD card back into the Raspberry Pi and fit the LCD to the GPIO ports like this, starting at the top end which is by the SD card slot and away from the USB and ethernet ports. Next, take the microUSB power lead and plug it in to boot up the Rapsberry Pi to confirm that the screen – and the touchscreen element of it – are working correctly.
As you’d expect, the orientation of the display is in landscape as standard, but for our virtual keypad we want the display to be portrait so we need to change some config files to achieve this. With your Raspberry Pi connected to your network either via WiFi or an Ethernet cable, shell into it and open up the config.txt file inside the boot directory:
$ sudo nano /boot/config.txt
This file determines the orientation of the display. We’re looking for the line towards the bottom which contains “dtoverlay=waveshare35a” and to that we’re going to add a semicolon followed by “rotate=180”. Save and exit.
Next, we need to rotate the touchscreen to match the display, so we need to open up the “99 calibration” config file:
$ sudo nano /etc/X11/xorg.conf.d/99-calibration.conf
Inside this config file, we first need to change the SwapAxis option from one to zero, and then enter in the following base values for the Calibration options. Once you’ve done that, Save and Exit. Then, you can reboot your Pi, and once it boots back up your display should be rotated to portrait.Step 2: Install MySQL Database & Apache Web Server on the Raspberry Pi
Whilst we’re in the shell, now’s as good a time as any to install the database, which will hold the access control list for our door entry system, as well as record a log of all successful and unsuccessful access attempts. We’ll install a full LAMP stack so that we can use phpMyAdmin to access the database, and this also means you can create your own web interface using PHP to add and delete users to the system easily in the future.
First off, we’ll install the Apache web server with this command:
$ sudo apt-get update $ sudo apt-get install apache2
Once that’s set up, we’ll install PHP, and then MySQL.
$ sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql $ sudo apt-get install mysql-server
As part of the MySQL server installation process it’ll ask you for a nice secure password for the “root” user, so enter one in and make sure you don’t forget it.
Finally, once MySQL is installed we’ll restart Apache and then install phpMyAdmin.
$ sudo service apache2 restart $ sudo apt-get install phpmyadmin php-mbstring php-gettext
As part of the installation process it’ll ask us which webserver we want to configure to use it, so make sure Apache is selected and select OK. We’ll allow phpMyAdmin to install and configure its own database automatically, and then enter the root password we chose when install MySQL to allow phpMyAdmin to create its own database and user. Next, we’ll create a secure password for that user.
Once that’s installed you can type your Pi’s IP address in your web browser, followed by “/phpmyadmin” and then login with the root account you created when installing MySQL.Step 3: Install Python Script and Set up Database Tables
Now it’s time to install the Python script, which is like the glue that binds everything together. It receives the user input from both the keyfob and the touchscreen, it talks to the database to see who’s allowed access, and it uses Twilio to send a SMS to the user.
The Python script and blank database tables can be downloaded from GitHub at: https://github.com/paulfp/Three-Factor-Security-Door
Once you’ve imported the database file via phpMyAdmin you’ll see the database contains 3 tables. The first is called access_list and this table contains the details of everyone with access to the area secured by our locked door. Here you can see I’ve added a couple of users with their name, the RFID code number of their fob, their PIN and their mobile number for the SMS two-factor authorisation code to be sent to. We also include an image file so that the welcome screen can show a headshot. Make sure this is a GIF file and store it in the same directory as the Python script.
Second, we have an access_log table which records all attempts – successful and unsuccessful – to access the system. This records any RFID codes presented to the scanner, whether they were granted at the time, what PIN was entered, which mobile number a one-time code was sent to, and whether it was entered correctly etc. This logging information would be very useful in the real-world both for diagnosing problems with legitimate users accessing as well as gathering evidence of anyone trying to gain unauthorised access.
The last table contains our Twilio account details, which we use to send the text message which enables the final part of the authentication process before the door is momentarily unlocked allowing access to an authorised party.
With the Python script downloaded to the Raspberry Pi user’s /home directory, make sure you install all the dependencies required by the script.Step 4: Make the Python Script Autoload when Your Raspberry Pi boots up
We need to make the script autoload whenever the Raspberry Pi starts up. To do this, we need to add a line to the autostart config file. Type this into your terminal:
sudo nano ~/.config/lxsession/LXDE-pi/autostart
Then add a line like this:
@sudo python lock.py
Save and exit.Step 5: Connect GPIO Expansion Board and Relay Switch
With everything set up on our Pi, it’s now time to disconnect the screen and put the Pi into its protective case. To maintain access to the GPIO ports we use a short 26-pin female to male ribbon cable, and we also need to add in a GPIO expansion board, too. This is because the touchscreen LCD uses up some pins that we need to use in a moment for our relay, namely the 5V pins. If you prefer, you could also power the relay from another 5V plug from the power supply unit, instead. To connect the screen to the board we need another long ribbon cable, and you’ll need to carefully bend a couple of unused pins out of the way in order to make this fit. Then, connect the touchscreen and power up the Pi again just to make sure everything is still working.
The next step is to connect our relay to the GPIO ports. It’s a 5V relay, so the positive pin needs connecting with a dupont cable to a 5V pin on our GPIO expansion board, and the negative can of course go to any ground. The signal pin needs to go to any numbered GPIO pin, and make sure you choose one which isn’t in danger of also being used for anything by the touchscreen; as you can see, I went for Pin 13 here which is well out of the way.Step 6: Connect RFID Reader and Test
Now it’s time to connect everything up, including the RFID reader, and see if it works! Take your RFID fob and hold it to the reader. It recognises that as me, and displays my name and photograph. I then enter my PIN, 1 2 3 4 5 6 and it then generates a random one-time access code to send to my phone as a text message.
As soon as I enter this code you’ll hear the “Click!” of the relay switch coming on, and after a few seconds it “clicks” again to turn off. Later on when we install this into our doorway, the relay switch will be connected to the electronic door strike so will unlock the door.Step 7: Prepare Dual-Voltage (5V / 12V) Power Supply to power Raspberry Pi and Electronic Door Strike Simultaneously
Next up, we need to prepare our power supply to provide power both to the Raspberry Pi (which requires 5V) and to the door lock which requires 12V and is triggered using the relay switch. We want the whole system to be powered using just a single power lead, so we need a single power supply which can provide both 5V and 12V simultaneously, so we’re using a Desktop computer power supply.
At this point it seems prudent to point out that we’re about to be dealing with live electrical voltages. Low voltages, mind you, and not mains current – but the power supply itself does plug into the mains, obviously. Therefore, proceed at your own risk and only if you are confident and sure about what you’re doing. If you are in any doubt whatsoever, do not continue and instead consult an experienced, qualified and competent professional. I can accept no liability whatsoever for your actions! Got that? Right, as we were…Step 7b: How to Make the ATX Power Supply Turn on When Not Connected to a Computer Motherboard
As you’ll know, computers don’t immediately turn on when you plug them in, even with the little switch on the power supply itself in the “on” position – you have to press the “On” button on the front of the computer tower first. Well, in this case we do want our power supply to turn on using just its own switch, so we need to do some electronic whizzardry. Of the myriad cables coming out of the PSU box, you’ll find just one green one; this is the signal cable and it needs to be permanently shorted-out to a black ground cable. Take the green cable and any black cable then snip them with a pair of scissors, then strip a short length from each end to expose the bare wires inside. Twist them together so they don’t fray, and then take a choc block and cut one from the end. Connect the green and black cables together and then test it works by plugging in the mains power and turning on the PSU. Yay, it works!
Now, we need to take one of the SATA power cables which usually provides 5V and 12V to a hard drive, and test the contacts with a multimeter to check which is which. Place your multimeter’s black contact on the black ground wire and then test both the yellow and red contacts to check which is which. As you can see, the yellow is giving us 12V, and the red is the 5V.
Chop off the SATA connector to reveal two pairs of cables; a 5V with ground, and a 12V with ground. Strip the ends off each wire, twist them to prevent fraying and then chop of a 4-way section of chocblocks. Connect the 4 cables into the chock blocks like so, giving us a handy source of both 5V and 12V power.Step 8: Connect Raspberry Pi to Dual Voltage 5V Power Supply
Next, take the power supply which came with your Raspberry Pi. Although it’s a microUSB plug on the end, as it’s only supplying power and there’s no data transfer going on, the cable only has two cores; positive and negative. Snip the cable off the power supply and then separate the two cores for a couple of centimetres. Strip and twist the ends, and then look for the marking on the cable to determine which is the negative. If there aren’t any obvious markings, you’ll have to use your multimeter to do some detective work. Connect the USB power cable into the chocblock we just prepared, making sure you connect it to the 5V supply, which is red in my case.
We can check this works by connecting the power cable to our Raspberry Pi and then turning on our power supply unit. As you can see here, it works just great!Step 9: Test & Connect Electronic Door Strike
Now it’s time to turn our attention to the electric door strike – this is the catch which sits in the door frame and is normally closed, so won’t allow the door to open unless 12V is supplied to the strike, in which case it allows itself to be opened, effectively unlocking the door.
If we open up the strike you can see the two very thin wires which were meant to be soldered to contacts on this black plastic top. On this one, the wires are so thin and flimsy that they’d broken off in transit, so to remedy this we’ll remove the black plastic clip holding the contacts and solder some longer pieces of wire on. We’ve got some handy as we just chopped it off the power supply, so let’s take a couple of lengths of wire from that, snip them and then strip and twist the ends.
Now it’s time to get your soldering iron out and tin the exposed ends of the wires with some solder before bringing over the door strike and soldering our new lengths of wire to the two very fragile exposed wires up top.
Now it’s time to test the door strike so let’s wire it directly into our 12V power supply that we created before and watch what happens when we switch on the power.Step 10: Install Electronic Door Strike into Frame & Electronic Components into Wall
The door strike now needs to be installed into your doorway, in place of your existing Striker Plate. Here, I’m installing the system into this mock-up of a door frame that I’ve knocked together as a demo, so you’ll have to excuse the slightly shoddy woodwork! On mine, the electronics are going to be located within the stud wall, but make sure you comply with all building regulations for your region before continuing.
The first step is to drill a reasonably-sized hole to allow cables to be poked through the timber to the power supply which will sit below. Run the cables for our dual-voltage supply up the vertical beam and drill a small pilot hole through the centre of the choc block and then screw in place. Then, poke the USB power cable up through the hole to provide power to the Raspberry Pi. Next, we need to decide on the arrangement of our components within the stud wall, and I’m using a spare block of wood from a previous project to create a little platform onto which I can mount the GPIO expansion board. The relay switch will sit just here on the vertical beam, next to the electronic strike.
The Raspberry Pi’s case has mounting points on its base, so measure the distance between them and then transfer that measurement to the horizontal beam, marking two points to insert some small screws, nearly all the way down but not quite. With those in place, the Raspberry Pi can be placed on and slid back, holding firmly in place. The power cable reaches neatly to the socket, and can now be plugged in.Step 11: Connect Up Relay Switch, Raspberry Pi & Electronic Door Strike
Secure the mounting block in place, and try not to split the wood like I did, oops! Drill a pilot hole if necessary to avoid that. Then, screw down the GPIO Expansion Board and secure the relay switch in place near the electronic strike. Next, wire in one of the cables directly from the door strike to the NORMALLY OPEN terminal on the relay. Either one will do, as it’s not a polarised component. Then connect a length of cable to the GROUND terminal, poke it through the hole we drilled and then connect it to the negative terminal of our dual voltage power supply. Finally, connect the door strike’s other wire to the 12V positive supply.Step 12: Cut Holes in Wall for RFID Reader & LCD Touchscreen
Next we need to cut the holes in the wall to mount the RFID reader and the LCD touchscreen. I marked the holes out first on a cereal packet to get the placement just right, before using that as a guide to mark out the holes on the wall. Drill a hole in the centre of the area to be cut away, then use a jigsaw to cut out both squares. I used some insulation tape to make the edges look a bit neater on my demo unit, you can of course make yours look much better than mine!
To mount the RFID reader, I’m using some hot glue on the half of the front that doesn’t contain the infographic, and then pressing that up against the wall. I also used some gaffer tape to help secure it in place, as this USB model isn’t one which is specifically intended for wall mounting. I’m mounting my LCD in the same way, which isn’t ideal and I wouldn’t recommend this method in the real-world, but it should work just fine to demonstrate for this build.
Next, connect up the miniUSB cable to the RFID reader and boot up the Pi to test it’s working.
Now it’s just time for the finishing touches, so let’s use some blocks to hold the power supply in place, and then trim down the excess length on all of our cables. Carefully disconnect them one by one, cut to the appropriate length, strip and twist the ends and then reconnect. Then, use tacks to hold them neatly in place.
And we’re done! To summarise our build we have an electronic door strike which is activated by the relay switch here, which in turn is controlled by the output from our Raspberry Pi which denies or grants access using the RFID reader and touchscreen input. It also connects to the internet via WiFi to use a web service to send the one-time passcodes via SMS to the user’s phone, acting as THREE factor authentication. The whole thing is powered by a computer power supply which sits down here inside the wall
So there we are, a ridiculously secure door entry system for that most secret of rooms in your home or office. If you’re going to build one yourself, there are links at the start of this article for all the low-cost components you’ll need to buy, as well as a link to download my Python script to load onto your Raspberry Pi. Make sure you send me a picture or video of your working setup – I’m @paulfp on Twitter.Subscribe to our YouTube Channel!