ritter.vg
tech > code > adventures in code > freaking out a roommate
04 Apr 2009 16:17:00 EST
so I got an rfid kit, right?
If you don't care about code and just want to see the video, click here!

I saw this RFID kit on Thinkgeek and I was like awesome! I want that! Well it took me a few months but I eventually bought it. I was messing around, and what's really cool about it is that the company, Phidgets, supplies code samples in several languages for all platforms. So at first I was writing a reader in C# on windows, then I switched to linux and (after looking at the C code and remembering that C was bloody annoying to write) had it running in python.

Which is not to say the kit is perfect. I was disappointed by a few aspects of that ended up limiting my ideas for what to do with it...

  1. The reader is tuned for only the EM4102 protocol. I'm not an RFID expert, but the practicality of this is that it couldn't read my Smartcard (the PATH train between NJ and NY) or my work access card.
  2. The reader is limited to 3 inches. So I can't scan everything exiting a door for example.
  3. The reader is USB, which apparently degrades unusably after 15 feet. (The book uses a USB to Ethernet to Wireless system to extend range)
So what I could do was limited a bit. I had a couple ideas but ultimately I settled on using the reader not to track when something went into range, but rather out of range.

step 1: disguise the Reader and Tag
Reader and Tag sitting on top of desktop lookin innocent Entire desk
step 2: when they take the tag away
I used the following shell script when the candy was removed:
#!/bin/bash

function printline {
    for i in `seq 1 $1`;
    do
        for j in `seq 1 80`;
        do
            echo -n " " > /dev/tty0
        done
    done
}

function clearcolor {
    setterm -term linux -background $1 > /dev/tty0
    printline 30
}

function print {
    setterm -reset > /dev/tty0
    printline 9
    ./figlet-mod -f banner $1 > /dev/tty0
}

setterm -blank 0 -term linux > /dev/tty0
setterm -reset > /dev/tty0
for i in `seq 1 4`;
do
    clearcolor red
    clearcolor yellow
done
clearcolor black

print "Excuse Me"
sleep 6
print "Please put that back"
sleep 6
print "My candy."
sleep 3
print "I'm waiting."

There's two things I need to note about this code:

step 3: the thank you

The script it ran when you put the tag back. It includes all the functions from before, just omitted for brevity.

setterm -blank 0 -term linux > /dev/tty0
setterm -reset > /dev/tty0
for i in `seq 1 2`;
do
    clearcolor green
    clearcolor blue
done
clearcolor black

killall freak.sh
print "Thank you"
sleep 10
clearcolor black

setterm -blank 1 -term linux > /dev/tty0
step 4: the reader script

This code is just a stripped down, modified version of what it freely available from the Phidgets website. I'm including it for completeness, but all I did was rip out print statements and call the shell scripts.

#!/usr/bin/env python

from ctypes import *
import sys
import os
from Phidgets.PhidgetException import *
from Phidgets.Events.Events import *
from Phidgets.Devices.RFID import RFID

rfid = RFID()
def rfidError(e):
    print "Phidget Error %i: %s" % (e.eCode, e.description)
    return 0

def rfidTagGained(e):
    rfid.setLEDOn(0)
    os.system("./thankyou.sh")
    return 0

def rfidTagLost(e):
    rfid.setLEDOn(1)
    os.system("./freak.sh")
    return 0

#Main Program Code
try:
    rfid.setOnErrorhandler(rfidError)
    rfid.setOnTagHandler(rfidTagGained)
    rfid.setOnTagLostHandler(rfidTagLost)
except PhidgetException, e:
    print "Phidget Exception %i: %s" % (e.code, e.message)
    exit(1)

try:
    rfid.openPhidget()
except PhidgetException, e:
    print "Phidget Exception %i: %s" % (e.code, e.message)
    exit(1)

try:
    rfid.waitForAttach(10000)
except PhidgetException, e:
    print "Phidget Exception %i: %s" % (e.code, e.message)
    try:
        rfid.closePhidget()
    except PhidgetException, e:
        print "Phidget Exception %i: %s" % (e.code, e.message)
        exit(1)
    exit(1)

rfid.setAntennaOn(True)
chr = sys.stdin.read(1)

try:
    rfid.closePhidget()
except PhidgetException, e:
    print "Phidget Exception %i: %s" % (e.code, e.message)
    exit(1)

exit(0)
the result
Comments
Add a comment...
required
required, hidden, gravatared

required, markdown enabled (help)
you type:you see:
*italics*italics
**bold**bold
[stolen from reddit!](http://reddit.com)stolen from reddit!
* item 1
* item 2
* item 3
  • item 1
  • item 2
  • item 3
> quoted text
quoted text
Lines starting with four spaces
are treated like code:

    if 1 * 2 < 3:
        print "hello, world!"
Lines starting with four spaces
are treated like code:
if 1 * 2 < 3:
    print "hello, world!"