Recently, a fried told me he was having trouble getting a Parallax RFID reader working on a Raspberry Pi for a project he was working on. I wondered how hard it could be so I got one of the readers for myself and hooked it up to a Pi. It turns out that it was harder than I thought it would be, but only because I didn’t know what I was doing.
When the reader is connected to the computer, it is auto-mounted as a serial port at /dev/ttyUSB0
. You might think that because the letters ‘tty’ are in the device name that this is a TTY device, but it turns out that TTY devices are just connected over serial ports. This was not actually a TTY device. Once I understood that, it turns out that connected to a serial port on Linux though Python is actually rather simple. One just needs the pyserial
library.
Here are some instructions and sample code to get this device working with a Raspberry Pi:
First install pyserial
:
sudo pip install pyserial
Then you can connect to port by putting this in your python code:
import serial
port = serial.Serial('dev/ttyUSB0', 2400, timeout=1)
You’ll either need to run your script with root level permissions (IE, with sudo
), or change the ownership of the device before running the script.
The full script bellow, will save each tag id read to a sqlite database on the Raspberry Pi. Each time it does so, It will blink the light on the reader. To read the database, you’ll want to install the sqlite3 client program:
sudo apt-get install sqlite3
Here’s the full sample program:
#! /usr/bin/python
# To use this code on the Raspberry PI, you'll need to install the pyserial module.
# You'll also want to install the sqlite3 package. To install these run these commands
# from the terminal on the Raspberry PI:
#
# sudo pip install pyserial
# sudo apt-get install sqlite3
#
# If you have trouble installing sqlite, try first running:
#
# sudo apt-get update; sudo apt-get upgrade
#
import serial
import time
import sqlite3
# open sqlite database file
db = sqlite3.connect('tagreads.db')
cursor = db.cursor()
# check that the correct tables exists in database; create them if they do not
tables = cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND (name='tagreads');").fetchall()
if len(tables) == 0 :
cursor.execute("CREATE table tagreads (timestamp, tagid)")
# connect to serial port on which the RFID reader is attached
port = serial.Serial('/dev/ttyUSB0', 2400, timeout=1)
lastid = ""
try:
while True:
# attempt to read in a tag
tagid = port.read(12)
# if tag read success full, save it to database and wait half a second;
# else try again
if(len(tagid) != 0) and (len(lastid) == 0):
# close and open the port to blink the light and give visual feedback
port.close()
tagid = tagid.strip()
timestamp = time.time()
cursor.execute("INSERT INTO tagreads VALUES (?,?);", (timestamp, tagid))
db.commit()
print("Time:%s, Tag:%s" % (timestamp,tagid))
# if you want to make this script trigger something on log in, this would be a
# good place to insert that code
time.sleep(.5)
port.open()
lastid = tagid
except KeyboardInterrupt:
port.close()
db.commit()
db.close()
print ("Program interrupted")