<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tender Lovemaking &#187; nfc</title>
	<atom:link href="http://tenderlovemaking.com/category/computadora/nfc/feed/" rel="self" type="application/rss+xml" />
	<link>http://tenderlovemaking.com</link>
	<description>The act of making love, tenderly.</description>
	<lastBuildDate>Sun, 15 Jan 2012 04:36:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ruby and RFID tags</title>
		<link>http://tenderlovemaking.com/2009/09/19/ruby-and-rfid-tags/</link>
		<comments>http://tenderlovemaking.com/2009/09/19/ruby-and-rfid-tags/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 05:52:26 +0000</pubDate>
		<dc:creator>Aaron Patterson</dc:creator>
				<category><![CDATA[computadora]]></category>
		<category><![CDATA[nfc]]></category>

		<guid isPermaLink="false">http://tenderlovemaking.com/?p=358</guid>
		<description><![CDATA[It&#8217;s been forever since I&#8217;ve written a blog entry, so LETS DO THIS. I want to talk about reading RFID tags with Ruby. I am a nerd, so even though I can&#8217;t think of a good application, I am compelled to be able to read RFID tags. I love programming Ruby, so of course, I [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been forever since I&#8217;ve written a blog entry, so <strong>LETS DO THIS</strong>.  I want to talk about reading RFID tags with Ruby.  I am a nerd, so even though I can&#8217;t think of a good application, I am <em>compelled</em> to be able to read RFID tags.  I love programming Ruby, so of course, I have to do this with Ruby.</p>
<h3>Getting an RFID Reader</h3>
<p>First thing to do, is buy an RFID reader.  After searching around, I found the <a href="http://www.touchatag.com/">touchatag reader</a>.  I bought the <a href="http://store.touchatag.com/usshop/acatalog/touchatag_starter_pack.html">touchatag starter pack</a>.  It&#8217;s only $40, USB, and comes with 10 RFID tags.  Most importantly, it works well with <a href="http://www.libnfc.org/">libnfc</a> (more about that later).</p>
<p><a href="http://www.flickr.com/photos/aaronp/3935563715/" title="IMG_0315 by fakebeard, on Flickr"><img src="http://farm4.static.flickr.com/3436/3935563715_1f799ab95b.jpg" width="500" height="333" alt="IMG_0315" /></a></p>
<p>The tags that come with the reader have an adhesive back, so you can stick them to stuff.  They also have the unique identifier printed on them so that you can make sure your program output is correct.</p>
<p><a href="http://www.flickr.com/photos/aaronp/3936348912/" title="IMG_0317 by fakebeard, on Flickr"><img src="http://farm4.static.flickr.com/3529/3936348912_36d1b2fe2a_m.jpg" width="240" height="160" alt="IMG_0317" /></a><a href="http://www.flickr.com/photos/aaronp/3936350030/" title="IMG_0318 by fakebeard, on Flickr"><img src="http://farm3.static.flickr.com/2471/3936350030_ea34963cb7_m.jpg" width="240" height="160" alt="IMG_0318" /></a></p>
<h3>Interfacing with the reader</h3>
<p>Now that we&#8217;ve got the reader, let&#8217;s do something with it!  I mentioned earlier that the touchatag reader works with <a href="http://libnfc.org/">libnfc</a>.  Libnfc is a C library that knows how to work with NFC devices (nerd talk for &#8220;RFID readers&#8221;).  I&#8217;ve written a gem called <a href="http://github.com/tenderlove/nfc">nfc</a> that wraps up the C library in to something we can use in Ruby.</p>
<p>First thing we need to do is install libnfc.  I use macports with OS X.  With macports, installing libnfc is quite easy:</p>
<pre>
    $ sudo port install libnfc
</pre>
<p>Installing on linux should be just as easy, but you&#8217;ll need to consult your package manager.  Make sure to install the devel packages too!</p>
<p>After that, simply install the nfc Ruby gem:</p>
<pre>
    $ sudo gem install nfc
</pre>
<p>Now that that is out of the way, we can actually read an RFID tag.  Here is our code:</p>
<pre class="brush: ruby; title: ; notranslate">
require 'rubygems'
require 'nfc'
# Find a tag
NFC.instance.find do |tag|
  # Print out the tag we find
  p tag
end
</pre>
<p>That&#8217;s it!  Run the code, then touch a tag to the reader, and boom!  We have output.  With the tag I&#8217;m using, the output looks like this:</p>
<pre>
$ ruby -I lib test.rb
(NFC) ISO14443A Tag
 ATQA (SENS_RES): 00  44
    UID (NFCID1): 04  D7  62  91  21  25  80
   SAK (SEL_RES): 00
</pre>
<p>The important part of this output is the UID field.  That field is the unique identifier for this tag.  The identifier comes back as a list of integers, but they are printed on the tag as hex.  We can adjust the program just a little bit to see that list, or to get the same string that&#8217;s printed on the tag:</p>
<pre class="brush: ruby; title: ; notranslate">
# Find a tag
NFC.instance.find do |tag|
  # Examine the raw numbers
  p tag.uid
  # Get just the UID as a string
  puts tag.to_s
end
</pre>
<p>The output looks like this:</p>
<pre>
$ ruby -I lib test.rb
[4, 215, 98, 145, 33, 37, 128]
04D76291212580
</pre>
<p>That&#8217;s pretty much it.  Unfortunately, I can&#8217;t think of anything fun to do with my tags, but maybe you can!  <a href="http://www.flickr.com/photos/aaronp/3804698617/">I hooked my tags up to the &#8220;say&#8221; command that comes with OS X and made each tag say something different</a>.</p>
<h3>Non-Blocking NFC interaction</h3>
<p>Our previous example blocked until an RFID tag was read.  If you run the program without having an RFID tag on the reader, it will just sit there until it can read a tag.  Sometimes we might want to tell whether or not there is a tag on the reader <em>right now</em>.  In other words, we <em>don&#8217;t</em> want our program to block.</p>
<p>Calling find without providing a block will return immediately:</p>
<pre class="brush: ruby; title: ; notranslate">
p NFC.instance.find.to_s
</pre>
<p>You&#8217;ll get a return value immediately.  The tag returned will either contain a blank uid, or an actual UID.  Here is the output run once with a tag sitting on the reader, and once without a tag:</p>
<pre>
$ ruby -I lib test.rb
"04D76291212580"
$ ruby -I lib test.rb
""
</pre>
<h3>Conclusion</h3>
<p>That&#8217;s pretty much it.  Interacting with the touchatag reader is quite simple and straight forward.  Currently the nfc gem supports reading ISO1443A tags (the tags that come with the reader).  The reader should be able to read other tag types, but I haven&#8217;t had a chance to get other tags to test.</p>
<p>Touchatag provides an <a href="http://www.touchatag.com/developer/docs/guide">official API</a> for their readers.  But the API seems difficult and is dependent on a network connection.</p>
<p><a href="http://www.flickr.com/photos/aaronp/3804698617/">Here is a video of me reading some tags</a>.<br />
<a href="http://gist.github.com/164896">Here is the code from the video</a>.<br />
<a href="http://www.flickr.com/photos/aaronp/tags/touchatag/">Here you can find more photos of the reader</a>.<br />
Finally, <a href="http://github.com/tenderlove/nfc">here is the source of the NFC gem</a>.</p>
<p>Have fun reading some RFID tags!</p>
]]></content:encoded>
			<wfw:commentRss>http://tenderlovemaking.com/2009/09/19/ruby-and-rfid-tags/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

