Flex 3 + AIR + RFID + AMFPHP + SMS

13 Oct 2007

I had originally wanted to build this project with my Parallax RFID reader, but since I can't interface it with the Make Controller until Liam at MakingThings fixes the serial BLOB implementation fixed in mchelper 2.0, I decided to complete a proof-of-concept with a Phidgets RFID reader. The application is pretty straightforward - it consists of an RFID tag embedded in a cell phone that is read for its unique ID, prompts a user to enter some data and then remembers their personal data when they return. While the premise is simple, there are a few moving parts required to stitch it together.

But first, before an explanation, here's a demo of the application:

[flash http://vimeo.com/moogaloop.swf?clipid=2164104&server=vimeo.com&showtitle=1&showbyline=1&showportrait=0&color=ffffff&fullscreen=1 w=601 h=453 mode=1]

The Phidgets RFID Reader PhidgetRFID is a EM4102 125Khz protocol reader. This means that it does not have the capability to write to RFID tags, only read from them. While this is just fine, I point it out so that you are aware of this fact if you are new to RFID. It should also be noted that RFID readers like these usually fit into a larger architecture that consists of a database of some sort. This allows RFID tag IDs to be stored along with additional metadata.

This particular reader is nice in that you can connect it directly to a computer (Mac, Windows and Linux support) via USB and simply run a web service that provide connectivity to the board. In this example, that process is abstracted away in the AS3 classes that Phidgets provides (they also have a .SWC if you prefer that), so connecting to it and reading tags from it is really quite easy.

After installing the software from Phidgets, I set up an alias in my bash profile set up to make it easy to fire up the web service: alias phidget='/usr/bin/phidgetwebservice21'. That way, I just type phidget into a terminal window and it starts. After plugging the RFID reader into an open USB port, it's ready to communicate. That's really all there is to setting it up since there is no microcontroller involved.

Flex Builder 3 and AIR I opted to build this as an AIR application because an application like this is most applicable in a kiosk scenario, rather than as a web application. Anyway, connecting to the RFID reader using Flex is quite easy and a majority of the code is devoted to setting up event handlers:

creationcomplete="onCreationComplete()">

<!--[CDATA[

import com.phidgets.*;

import com.phidgets.events.*;

private var devRFID:PhidgetRFID;

private function onCreationComplete():void {

devRFID = new PhidgetRFID();

devRFID.addEventListener(PhidgetEvent.DETACH, onDetach);

devRFID.addEventListener(PhidgetEvent.ATTACH, onAttach);

devRFID.addEventListener(PhidgetErrorEvent.ERROR, onError);

devRFID.addEventListener(PhidgetDataEvent.TAG, onTag);

devRFID.addEventListener(PhidgetDataEvent.TAG_LOST, onTagLoss);

devRFID.open("localhost", 5001, "1234");

}

private function onAttach(pdeEvent:PhidgetEvent):void{

trace(pdeEvent);

devRFID.Antenna = true;

devRFID.LED = true;

}

private function onDetach(pdeEvent:PhidgetEvent):void{

trace(pdeEvent);

}

private function onTag(pdeEvent:PhidgetDataEvent):void {

  trace(pdeEvent);

//This is where all of the good stuff goes...

}

private function onTagLoss(pdeEvent:PhidgetDataEvent):void {

trace(pdeEvent);

}

private function onError(pdeEvent:PhidgetErrorEvent):void {

trace(pdeEvent);

}

]]-->

If you're interested in the rest of the code, download the source. Also, a quick disclaimer that this was just a quick proof-of-concept and is all in a single MXML file for simplicity rather than encapsulating objects and creating components.

AMFPHP Remoting For server/Flash Player communication, I generally opt for remoting due to its ease of implementation and speed, especially if dynamic, database-driven calls are required for the application. In this case, PHP and MySQL will handle user data and authentication as well as consuming SMS services. I could write an entire article about AMFPHP and Flex 3, and probably will, but for now, I'll just mention it and note that it in the source code.

SMS In the demo version (I'm not releasing this portion publicly), I've also rigged up some PHP classes to send SMS messages. I found a service called Clickatell that provides SMS gateway services to a bunch of giant corporations like BBC, Vodafone and Oracle. However, they do actually offer services to substantially smaller entities such as myself, which is pretty cool. Not only that, but they offer a free trial of their services.

I'd recommend them if you only need to send outgoing SMS messages. If you are interested in two-way messaging, be prepared to pony up a large amount of cash for short codes (somewhere in the neighborhood of $1200/month) and message routing. The other alternative is to set up your own SMS gateway. However, keep in mind that while setting up your own gateway is easy, it is not cost effective for sending large numbers of text messages using a normal consumer account. It will end up being cheaper using a bulk service like Clickatell.

Source Click here to download the source for the AIR source and installer, PHP code and MySQL schema.