Using a Parallax 28340 RFID reader on the Raspberry Pi


Recent­ly, a fried told me he was hav­ing trou­ble get­ting a Par­al­lax RFID reader work­ing on a Rasp­berry Pi for a project he was work­ing on. I won­dered how hard it could be so I got one of the read­ers 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 did­n’t know what I was doing.

When the reader is con­nected to the com­put­er, it is auto-­mounted as a ser­ial port at /dev/ttyUSB0. You might think that because the let­ters ‘tty’ are in the device name that this is a TTY device, but it turns out that TTY devices are just con­nected over ser­ial ports. This was not actu­ally a TTY device. Once I under­stood that, it turns out that con­nected to a ser­ial port on Linux though Python is actu­ally rather sim­ple. One just needs the pyserial library.

Here are some instruc­tions and sam­ple code to get this device work­ing with a Rasp­berry Pi:

First install pyserial:

sudo pip install pyserial

Then you can con­nect 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 per­mis­sions (IE, with sudo), or change the own­er­ship of the device before run­ning the script.

The full script bel­low, will save each tag id read to a sqlite data­base on the Rasp­berry Pi. Each time it does so, It will blink the light on the read­er. To read the data­base, you’ll want to install the sqlite3 client program:

sudo apt-get install sqlite3

Here’s the full sam­ple 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")

    Last update: 11/01/2016

    blog comments powered by Disqus