Add some security to your projects
Test Setup
For a simple test setup we used some Lego to encase the card reader, Pi and a small breadboard, (Figure 3).
You can use simple LED circuit (Figure 4), with the LED wired to GPIO 7 (BCM pin 4), and the other side to GND. Don't forget the resistor or you'll burn the LED.
Python Application
Listing 2 shows some example code to read the RFID card, compare it to a file of valid IDs and then toggle an LED. On lines 1-5 the GPIO pin is set up and defined as an output. Line 8 connects a variable to the RFID card reader input. The strip() function on line 12 is used to remove any white space in the text file. Finally the LED is toggled on line 13.
Listing 2
swipecard.py
01 import RPi.GPIO as GPIO 02 GPIO.setmode(GPIO.BCM) 03 GPIO.setwarnings(False) 04 LEDpin = 4 05 GPIO.setup(LEDpin,GPIO.OUT) 06 while True: 07 print "Swipe RFID card to toggle I/O" 08 card_id = raw_input() 09 print "card id is:", card_id 10 id_file = open("card_ids.txt","r") 11 for line in id_file: 12 if (card_id == line.strip()): 13 GPIO.output(LEDpin, not GPIO.input(LEDpin)) 14 print "Valid Card"
There are a number of ways to autostart a Python applications. The simplest technique is to run raspi-config to set the boot options to Console Autologin. You then add a Pi user login script with:
nano /home/pi/.bash_login
In the login file, you add a call to our Python program:
sudo python /home/pi/swipecard.py
Press [Ctrl]+[X] to save the changes and then reboot the Pi.
If all is working correctly the LED will turn on and off when you use a valid RFID card. Invalid cards will have no effect on the LED.
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 3
(incl. VAT)