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).

Figure 3: Card reader test setup made out of Lego.

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.

Figure 4: The very simple LED circuit.

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

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/

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.

Figure 5: Our Node-RED smart power switch setup.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content