Using RFID in an ActionScript Application

http://www.vimeo.com/2473196

In order to receive tags, the RFID reader has to be activated through a click on the image. After that, the different RFID tags (sorry for my tiny tags) are used to create different forms.

The previous post was all about wiring the RFID reader ID-12 and an Arduino board. Now I want to show you how to use this setup within an ActionScript application.

As you don’t have the possibility to access the hardware respectively the usb/bluetooth serial port directly, you have to use a Socket server which redirects the network socket connections to or from the serial ports. Fortunately there are already ready-to-use solutions like serproxy or TinkerProxy (win, osx) available. Personally I prefer serproxy, but if you want to have a nice GUI I would recommend TinkerProxy.
These proxy servers are not made specifically for the use with Flash and therefore they don’t implement a cross-domain-policy file. As you probably know this is needed for not getting a security error in your flash application. As a workaround for that problem you can get lessrains policyserver and start it on port 5334 (java -jar policyserver.jar 5334). It will return an appropriate policy file when requested by the flash application.

With these things in mind we can go for some code now.

...
public function connect() : void
{
	if(_state != DISCONNECTED) return;
	_state = CONNECTING;
 
	addSocketEvents();
 
	// load policy file from localhost, port 5334
	Security.loadPolicyFile("xmlsocket://" + SOCKET_HOST + ":" + String(POLICY_PORT));
	// connect to proxy localhost, port 5333
	_socket.connect(SOCKET_HOST, PROXY_PORT);
}
 
public function disconnect() : void
{
	if(_state != CONNECTED) return;
	_state = DISCONNECTING;
 
	sendDisconnect();
	processDisconnect();
}
 
public function finalize() : void
{
	...
}
...

My RFID class implements basically 3 public methods + the EventDispatcher stuff.

...
private function connectSocket(event : Event) : void 
{   
	// send ping to arduino
	_pingTimer = new Timer(100, 0);
	_pingTimer.addEventListener(TimerEvent.TIMER, handleTimer);
	_pingTimer.start();
}
 
private function handleTimer(event : TimerEvent) : void
{
	sendPing();
}
...

The Socket class dispatches Event.CONNECT when the connection to the proxy server is established but that doesn’t mean that the hardware is connected as well. To really know when the arduino is listening I use a timer to send a ping to the Arduino board. When I get the ping back, I know that the Arduino is ready to use.

...
private function processRfidTag() : void
{
	_cachePosition = _cache.position - 1;
	var bytesIn : int = 0;
	var val : int;
	var rfidTag : String = "";
 
	while (_cache.bytesAvailable > 0 && bytesIn < 10) 
	{                        				
		val = _cache.readByte();
 
		// if ETX or STX before the 10 digit reading -> stop reading
		if((val == STX) || (val == ETX)) break;
 
		// Do Ascii/Hex conversion:
		if ((val >= 0x30) && (val < = 0x39)) 
			val -= 0x30;
		else if ((val >= 0x41) && (val < = 0x46))
			val = 10 + val - 0x41;
 
		rfidTag += val.toString(16).toUpperCase();
 
		// ready to read next digit
		bytesIn++;                                
	}
 
	// read complete
	if (bytesIn == 10) 
	{ 
		if(rfidTag != "0000000000" ) _tag = rfidTag;
		else _tag = null;
 
		_cachePosition = -1;
		dispatchEvent(new RFIDEvent(RFIDEvent.TAG_CHANGED));
	}
}
...

When I receive some data from the socket, I look for the STX byte which is the header or marker of the RFID tag and get the tag via processRfidTag(). Beyond that I don’t read the bytes directly from the socket but write all the incoming data in a cache first to not loose any information coming in in different ProgressEvent.SOCKET_DATA events.

Download the full source of my ID-12 demo and don't forget to replace my tag ids with yours otherwise your RFID tags won't draw any form.



11 Responses (Add Your Comment)

  1. Hi Martin,

    I’m struggling a bit to get this working. I feel close but think maybe I have things filed in the wrong places. I’m a bit new to all this. I have set up the Arduino OK and can read the tags in Arduino’s serial window. If I then shut that window, run the serproxy and the policyserver.jar and then double click the RFIDTestMain.swf (after having entered my RFID tag ID numbers in the TestRFID.as file) and click on the grey square to get it to black…..nothing happens! Please can you help me by explaining what I’m doing wrong? or can you please guide me through the process?

    Many thanks,

    Joe

  2. uhmm, seems like flash can’t connect to the arduino or the proxy correctly. hard to guess what’s going wrong, maybe you try another proxy first,
    e.g. TinkerProxy .. links are in the post. tell me more about your setup, osx win, arduino ….

  3. Thanks for getting back to me Martin. I persevered some more last night and managed to get it so that the square went from grey to black, but then no shapes appeared. I entered my RFID tag ID numbers in the TestRFID.as file overwriting your ones, but no luck. In the nest of files and folders in your download I can’t open the .project file or the .as3_classpath What program were these file created in? – I have managed to get my RFID reader talking to my Arduino and that talking to Flash in a simpler way. What you have created looks really good, but might be a bit more than I need. I would still like to understand it though. My setup is Windows XP and Arduino Diecimila. Thanks again for your help.

  4. .project and .as3_classpath shouldn’t be in there, so just ignore them. I would recommend to trace the tagID or go to the RFID Class line 289 and trace the incomming bytes, just to see if there is processed anything.

  5. I added
    trace (“this is the value of the byte coming in” + val); to line 290 in the RFID.as file but if anything is tracing I can’t see where. Am I doing something really daft?

  6. hie there …
    im doing a rfid application and will be used with action script. the thing is, i won’t be using the RFID reader ID-12 and the Arduino board. i am using a different rfid reader. could this work with the guidance u gave above?

    what does that arduino board do anyway?

    thanks

  7. the process (RFID -> Serial Proxy -> Flash) will be pretty much the same and with a few minor changes you could probably use the AS3 classes.

    Basically the arduino isn’t needed here, you could hook up the ID-12 directly to the serial port (via USB or Bluetooth) and get the tag. I wanted to have a little more control over the reader, so the arduino is used for things like enabling/disabling or reseting the ID-12.

  8. ohhh … but i won’t be using the ID-12 reader … im using this locally made rfid reader that outputs its data onto window’s hyperterminal … any idea how i can relate them?

    the ID-12 uses hyperterminal to output its data too?

  9. actually I’m not into all those windows stuff, so I don’t even know what hyperterminal is … basically you will need some kind of server (Socket server) which can be accessed by flash.

  10. hi martin,

    i think i got the click of it on how your thing works. but 1 question, how do i check whether the things from my rfid are actually going into the port?

    im using the tinkerproxy program. the program sends the serial comm port data to this 5331 port. but how do i check them? are they really going into the port?

    thanks

Trackbacks:

Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Other Entries

About

Martin Rädlinger is an Interactive Developer & Designer. He specializes in interactive coding predominantly with ActionScript, but every new challenge is welcome. If you like his work feel free to get in touch with him. At the moment he's available for freelance work.

Contact

Martin Rädlinger
mail: mr [at] formatlos.de
web: www.formatlos.de
xing: Martin Rädlinger
linkedin: Martin Rädlinger
skype: martinraedlinger