-
1Watch an In-Depth Video Tutorial
-
2Install and Configure the LCD Touchscreen for the Raspberry Pi
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.
-
3Install 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.
-
4Install 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.
-
5Make 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.
-
6Connect 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.
-
7Connect 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.
-
8Prepare 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…
-
9How 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.
-
10Connect 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!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.