<?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>SQUARISM &#187; Arduino</title>
	<atom:link href="http://squarism.com/category/arduino/feed/" rel="self" type="application/rss+xml" />
	<link>http://squarism.com</link>
	<description>until lambs become lions</description>
	<lastBuildDate>Fri, 03 Feb 2012 05:13:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Lego mount for Arduino</title>
		<link>http://squarism.com/2011/09/09/lego-mount-for-arduino/</link>
		<comments>http://squarism.com/2011/09/09/lego-mount-for-arduino/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 23:13:17 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1345</guid>
		<description><![CDATA[I got to thinking (that&#8217;s another story) about the old Technics set that I had when I was a kid and how useful it would be if I could tear down and build things without having to hacksaw stuff (i.e. like with the Microrax stuff). We had major flooding recently (after an earthquake and a [...]]]></description>
			<content:encoded><![CDATA[<p>I got to thinking (that&#8217;s another story) about the old Technics set that I had when I was a kid and how useful it would be if I could tear down and build things without having to hacksaw stuff (i.e. like with the <a href="http://www.microrax.com/">Microrax</a> stuff).  We had major flooding recently (after an earthquake and a hurricane) and the closed roads funneled me to a toy store.  Was this a sign?  I&#8217;m not sure.  All I know is I have a Mindstorms kit now.  :)</p>
<p>So I built an Arduino frame using nothing but the parts from the Mindstorms kit (it&#8217;s all just Technics parts).  It&#8217;s pretty sturdy and the little rubber ends hold the board down pretty good.  I figure it&#8217;s good enough to put on wheels or a quad-ped bot.  I don&#8217;t know if I&#8217;d send it up in the air.  I&#8217;d probably want to add a few more cross bars in there.  Anyway, I did a lot of refactoring (if you could call it that) and I think this design is pretty good.  Here&#8217;s the money shot with the rest of the build shots so you can copy it.  I&#8217;m fresh brand-new to the Lego scene so leave some comments if I did something stupid.</p>
<p><img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_1-580x433.png" alt="" title="lego_arduino_1" width="580" height="433" class="aligncenter size-large wp-image-1346" /></p>
<p><span id="more-1345"></span></p>
<p>Top down without the board.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_2-580x433.png" alt="" title="lego_arduino_2" width="580" height="433" class="aligncenter size-large wp-image-1347" /></p>
<p>Bottom face without the board.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_3-580x433.png" alt="" title="lego_arduino_3" width="580" height="433" class="aligncenter size-large wp-image-1348" /></p>
<p>Room and support for plugs.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_4-580x433.png" alt="" title="lego_arduino_4" width="580" height="433" class="aligncenter size-large wp-image-1349" /></p>
<p>Doesn&#8217;t shake loose.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_5-580x433.png" alt="" title="lego_arduino_5" width="580" height="433" class="aligncenter size-large wp-image-1350" /></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/09/09/lego-mount-for-arduino/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Arduino Cat Faucet with Mongodb and Rails</title>
		<link>http://squarism.com/2011/03/09/arduino-cat-faucet-with-mongodb-and-rails/</link>
		<comments>http://squarism.com/2011/03/09/arduino-cat-faucet-with-mongodb-and-rails/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 04:01:56 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Noteworthy]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=874</guid>
		<description><![CDATA[I built a robot arm for my cat during a month-long geekcation. :) Here are some shots of the web interface. The graph shows the percentage of the day that she drinks. Background My cat likes to drink fresh and cold water directly from the faucet. We get up and turn on the faucet only [...]]]></description>
			<content:encoded><![CDATA[<p>I built a robot arm for my cat during a month-long geekcation.  :) Here are some shots of the web interface.  The graph shows the percentage of the day that she drinks.<br />
<img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_final-580x412.png" alt="" title="cat_faucet_final" width="580" height="412" class="aligncenter size-large wp-image-999" /></p>
<div id="attachment_997" class="wp-caption aligncenter" style="width: 590px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_final_hw-580x433.png" alt="" title="cat_faucet_final_hw" width="580" height="433" class="size-large wp-image-997" /><p class="wp-caption-text">Final hardware rig</p></div>
<h2>Background</h2>
<p>My cat likes to drink fresh and cold water directly from the faucet.  We get up and turn on the faucet only to leave it running after she&#8217;s jumped down.  It&#8217;s not really a big problem for us but I saw a fun problem that I could work on.  As much as this seems like a weird and freakish oddity, it&#8217;s a potential start of a smarthome sensor network that may provide some utility.  I also saw an opportunity to learn various things such as <a href="http://www.mongodb.org/">MongoDB</a>, mechanical construction with <a href="http://www.microrax.com/">Microrax</a>, Rails3 and more development on <a href="http://www.arduino.cc/">Arduino</a> with an Xbee module.</p>
<p><span id="more-874"></span></p>
<p>Some informal goals I had in mind when starting the project:</p>
<ul>
<li>Extensible &#8211; Something where I can add new sensors of any time of function.  Say I want an ambient temperature history.  Sure a SQL schema can be designed to be extensible and flexible but I wanted something super generic like a big hashtable</li>
<li>Analysis &#8211; I wanted to know what hour of the day my cat drinks the most.</li>
<li>Control &#8211; I wanted a protocol coming from the Arduino that would be flexible enough to not only collect metrics but also accept commands.</li>
<li>Temporary &#8211; Any installation had to be easily undone.  No holes in the wall, no new pipes and nothing I can&#8217;t undo.</li>
</ul>
<p>Video and detail past the break.<br />
<!--more--></p>
<p>Here&#8217;s a video of it in action for the first time.<br />
<iframe title="YouTube video player" width="560" height="349" src="http://www.youtube.com/embed/T0-EQqTW3Og" frameborder="0" allowfullscreen></iframe></p>
<h2>Design</h2>
<p>I spent some time drawing sketches first of the physical problem.  I drew a sink and sketched out some frames around it.  I sketched out the angles and vectors in play.  I played with the sink handle and tried to break down the physical forces and simplify it to it&#8217;s most basic components.  I built a small wooden sink faucet with the same dimensions as my real one so I wouldn&#8217;t have to work in the bathroom.  I took some reference pictures and looked through my sparkfun parts box to see what components I had lying around.</p>
<p>I eventually came up with a small box built around a base platform that would hold both an IR sensor and a servo.  From my wooden sink mock up, I knew that the servo&#8217;s fulcrum would have to be directly over the handle&#8217;s fulcrum.  Anything else would cause sliding or rubbing which would lead to more difficult mechanical problems.  That meant that I needed something pretty fancy and precise in terms of a support structure.  I couldn&#8217;t just cut up some wood and nail it together.  I would later solve this problem with the microrax structure, but I didn&#8217;t know that yet.</p>
<h2>Early prototype and testing</h2>
<p>I built some pretty rough prototypes using the wood faucet and handle.  I first started out by playing with the IR sensor.  I figured out a way to smooth the input values coming from the sensor and create a range window which could equate to &#8220;cat detected&#8221;.  I saved that bit of code off to the side and moved on to playing with the servo.  This was pretty simple to begin with, using the built-in Arduino library, you just create a Servo instance and write an angle to it.  The angle greatly depends on where and how you attach the servo gear so the exact values have to be calibrated for your physical install.  In any event, the code to move a servo looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Servo servo<span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// create servo object to control a servo</span>
<span style="color: #666666; font-style: italic;">// attaches the servo on pin 9 to the servo object</span>
servo.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
servo.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">180</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// turns servo to angle 180 but this has to be calibrated</span>
<span style="color: #666666; font-style: italic;">// sink is on at this point</span></pre></div></div>

<p>It&#8217;s a little more complicated than this (I have the code posted in full) but the idea is pretty simple.  The servo library writes a value as a resistance to the data pin running to the servo and the servo moves.  The servo requires a bit of time to actually move the arm so you have to put a delay() in.  Also, there&#8217;s some jitter and debouncing issues you have to consider, like if the servo can&#8217;t get to it&#8217;s destination then it&#8217;s going to buzz (this was a major problem for me).  As I&#8217;ll talk about later, the code in practice doesn&#8217;t just work, you have to play with it and calibrate it until it works the way you want in the physical world.</p>
<p>So at this point, I had a servo that would move 90 degrees on and off when I put my hand (representing the cat) in front of the IR sensor.  I was moving a pencil attached to the servo gear that was representing a handle.  Later I&#8217;d realize that I didn&#8217;t need to move a full 90 degrees because all I needed was a small stream of water (with about 10 degrees of movement).  There were many other things to do but this was a good first prototype.  I combined all my IR and servo code together and saved it off to the side.</p>
<h2>Microrax Frame</h2>
<div id="attachment_972" class="wp-caption alignright" style="width: 160px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_sketch-150x150.png" alt="Cat Faucet 3d sketch" title="cat_faucet_sketch" width="150" height="150" class="size-thumbnail wp-image-972" /><p class="wp-caption-text">Cat Faucet 3d sketch</p></div>
<p>I needed some way to hover the servo directly over my sink handle.  I needed a mount for the IR sensor.  I needed something to either put on the sink, or on the wall or something.  I considered many options but I eventually found an erector set type product called Microrax.  I ordered a basic starter set with 6 pieces of the piping.  I figured I&#8217;d screw up quite a bit of it so I ordered extra lengths.  Suprisingly, after it arrived, I didn&#8217;t screw up any lengths of it and the whole experience with Microrax was fun as hell.</p>
<p>Microrax lets you create almost any object you can think of, as long as it&#8217;s 45 or 90 degree angles.  Maybe there are other configurations that are possible but the easiest ones are the structures like they have pictured on their website and the structures I have pictured here.  I also ordered a servo bracket which fit perfectly on a Microrax pipe piece.  I drew up some rough 3D sketches on graph paper.  You can see the final one to the right.</p>
<p>From this 3d sketch, I could count how many lengths and pieces I needed to make the frame.  So I made a parts list for myself.  It was pretty funny making a parts list like a <a href="http://en.wikipedia.org/wiki/Bill_of_materials">BOM</a> but it&#8217;s was very necessary.  I had <em>planning-me</em> and then there was <em>hacksaw-me</em>.  Hacksaw me didn&#8217;t care about the plan, he was there to cut.  So it was good to review the segment lengths on paper a few times to make sure I wasn&#8217;t ruining my Microrax parts.</p>
<div id="attachment_974" class="wp-caption alignright" style="width: 160px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_pivot-150x150.png" alt="" title="cat_faucet_pivot" width="150" height="150" class="size-thumbnail wp-image-974" /><p class="wp-caption-text">Showing the pivot point on the handle</p></div>
<p>After I had a rough design, I cut out the pieces for the base of the frame and put together the base.  I then double checked my lengths by putting it on the sink, also to visualize the rest of the pieces.  There were some definite anchoring problems that I was trying to solve because I knew that when the servo pushed on the sink handle, it would push back on the frame and I didn&#8217;t want the frame to move.</p>
<p>I cut the rest of the pieces and eventually got a frame that placed the servo directly over the sink handle fulcrum which is exactly what I wanted.  You can see here (to the right) I&#8217;m illustrating that the servo gear is directly over the handle pivot point.  My screwdriver isn&#8217;t exactly pointing on the pivot point, it&#8217;s just there for depth reference.</p>
<p>My frame was built and I attached a servo bracket to it.  At this point I actually ran out of parts.  So I ordered some more and scavenged microrax pipes from other parts of the frame.</p>
<div id="attachment_992" class="wp-caption aligncenter" style="width: 310px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_frame-300x224.png" alt="" title="cat_faucet_frame" width="300" height="224" class="size-medium wp-image-992" /><p class="wp-caption-text">Microrax frame and base</p></div>
<h2>Arm and grip</h2>
<div id="attachment_970" class="wp-caption alignright" style="width: 160px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_arm-150x150.png" alt="" title="cat_faucet_arm" width="150" height="150" class="size-thumbnail wp-image-970" /><p class="wp-caption-text">Faucet arm and servo gear mount</p></div>
<p>But now I needed to attach an arm to the handle.  The servo attachment is just a little plastic star-shaped gear and I had to somehow attach that to a microrax piece.  I sandwiched two plates together  and eventually got this little piece you see here.  The hole on the black star is where the servo gear threads go.  A screw goes through this and holds this servo sandwich piece in and keeps it from falling in the sink.  This star piece attaches to a Microrax piece that acts as the arm.  I simply made a 90 degree downward post that acts as fingers off the arm.  A lot of this I modeled just by using my real hand on the faucet and trying to reduce the mechanics as much as possible.</p>
<p>I tried a few variables on a grip (you can see the rubber band attempt).  I even tried velcro and some other weird ideas.  But the servo wasn&#8217;t transferring enough force to the handle, or at least seemed like it could be doing better.  Also a lot of calibration came when I went to actually attach the grip.  If you do anything that is physically or deployed, make sure you have some wiggle room to adjust to your target.  Coming from a software world, I was horrified at how imprecise this is.  Automatic deployment like capistrano, chef and other tools obviously have no relevancy here in the physical/hardware world.</p>
<div id="attachment_966" class="wp-caption alignright" style="width: 160px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_grip_attempt-150x150.jpg" alt="" title="cat_faucet_grip_attempt" width="150" height="150" class="size-thumbnail wp-image-966" /><p class="wp-caption-text">Grip attempt with too little leverage</p></div>
<p>Anyway, pontifications aside, the rubber band connection around the handle instead of a hand-life device was transferring too much power to the inner portion of the lever.  That wasn&#8217;t good.</p>
<p>I eventually realized that the most torque I could get was to push at the outer point on the handle (ie: a lever).  So I positioned the grip so that it would squeeze all the way at the tip of the sink handle.  This allowed the servo to easily and smoothly turn the handle.  I put some rubber bands the pieces to give the arm some grippiness and it has worked very consistently for months.  This is the grip I settled on.  Some of the pieces need to be trimmed up but you get the idea.</p>
<div id="attachment_971" class="wp-caption aligncenter" style="width: 310px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_grip-300x224.jpg" alt="" title="cat_faucet_grip" width="300" height="224" class="size-medium wp-image-971" /><p class="wp-caption-text">Final grip with downward &quot;fingers&quot;</p></div>
<h2>Rig Polish</h2>
<p>So at this point, my arm was gripping the handle and my code was turning on the faucet.  That pretty much finished up the hardware portion of the prototype.  After that, it was just polish and finish.  I got a box from sparkfun to mount the Arduino in and I wired it all up under the sink.  I made wire connectors that go to servo extension cables that make the box easy to unplug from the servo and IR hookups.  The wires go down the back of the sink to this box which is holding the Arduino and the Xbee shield.</p>
<div id="attachment_1005" class="wp-caption aligncenter" style="width: 590px"><img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_arduino-580x433.png" alt="" title="cat_faucet_arduino" width="580" height="433" class="size-large wp-image-1005" /><p class="wp-caption-text">Inside the Arduino project box</p></div>
<h2>Software Components</h2>
<p>Before talking about the software components, here&#8217;s an overview of how everything works together.<br />
<img src="http://squarism.com/wp-content/uploads/2011/03/cat_faucet_components-580x317.png" alt="" title="cat_faucet_components" width="580" height="317" class="aligncenter size-large wp-image-1269" /></p>
<p>So let&#8217;s start off by talking about the Xbee component that&#8217;s attached to the Arduino.</p>
<h2>Xbee Integration</h2>
<p>I played around with the usb-serial driver using an Xbee explorer and integrated some code that would print to serial instead of printing to console from the Arduino.  This would allow me to send a message from the sink to a full-on computer with a proper software stack to do something interesting.  In my case, I wanted to send a message when the sink is tripped.  I made up a little protocol based on JSON for the message.  The Arduino can&#8217;t really do JSON per se but it can send plain-text that looks like it.  This is what my Arduino sends over Xbee when the cat jumps up into the sink:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// example JSON message</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;sensor&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sinks&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;basement&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;proximity&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;true&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;running&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;true&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;hash&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;82C61D54A77D6A90219E4E40CE6C8440&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;metric&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Proximity is a bit redundant but I put it in there because the Arduino actually knows proximity before it knows running (debouncing) so in the future, I could have it send a message on every proximity event.</p>
<p>The hash is actually computed on the Arduino using an MD5 library.  This was a b*tch to get working.  I&#8217;d actually drop this functionality right away if I did this over.  All it does is hash the sensor value and the name value together as a checksum.  I didn&#8217;t know how good the Xbee would be.  Definitely overengineered this but I was following serial advice I read on the Arduino forums.  Almost every serial protocol has a checksum or sanity mechanism.</p>
<h2>Ruby Serial Bridge AKA: The Cat Bridge</h2>
<p>So on the other side of the Xbee link is a Linux box that is running a do-forever ruby script.  It has an <a href="http://www.sparkfun.com/products/8687">Xbee Explorer USB</a> dongle and it watches for valid JSON coming in.  I&#8217;m cheating quite a bit on reading the serial and this part could be toughened up against garbage data.  I just look for opening and closing braces (meh).</p>
<p>Regardless of fragility, when a message comes in, it does a bit of sanity checking to make sure the message is valid.  If it is valid, it does an HTTP POST and passes the JSON message along, stripping the hash key.  The MD5 hash is just used for serial, TCP should handle error correction in a real software stack.</p>
<p>Here&#8217;s the function that posts to rails.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># do JSON HTTP post to rails</span>
<span style="color:#9966CC; font-weight:bold;">def</span> post_json<span style="color:#006600; font-weight:bold;">&#40;</span>url, input_json<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># we don't need the serial hash anymore so we</span>
  <span style="color:#008000; font-style:italic;"># can reuse the serial json payload from the sensor</span>
  input_json.<span style="color:#9900CC;">delete</span> <span style="color:#996600;">&quot;hash&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># parse our input url</span>
  uri = <span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># make a new request</span>
  req = <span style="color:#6666ff; font-weight:bold;">Net::HTTP::Post</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri.<span style="color:#9900CC;">path</span>, initheader = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">'Content-Type'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'application/json'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
&nbsp;
  <span style="color:#008000; font-style:italic;"># have to format it as json again with .to_json to avoid a</span>
  <span style="color:#008000; font-style:italic;">#   undefined method 'bytesize' for #&lt;Hash: ...</span>
  <span style="color:#008000; font-style:italic;"># error message.</span>
  req.<span style="color:#9900CC;">body</span> = input_json.<span style="color:#9900CC;">to_json</span>
&nbsp;
  response = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri.<span style="color:#9900CC;">host</span>, uri.<span style="color:#9900CC;">port</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">start</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>http<span style="color:#006600; font-weight:bold;">|</span> http.<span style="color:#9900CC;">request</span><span style="color:#006600; font-weight:bold;">&#40;</span>req<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Response #{response.code} #{response.message}: #{response.body}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>When it goes through, you see this on the console.</p>
<pre>
VALID: &lt;metric&gt;, proximity:0 running:false
Posting to http://localhost:3000/sinks/ --&gt;
{"sensor"=&gt;"sinks",
"name"=&gt;"basement",
"proximity"=&gt;"0", "running"=&gt;"false", "type"=&gt;"metric"}
Response 200 OK : mapped json_request.
</pre>
<p>The response message &#8220;mapped json_request&#8221; is coming from a Rails action which I&#8217;ll talk about next.</p>
<h2>Rails and MongoDB</h2>
<p>The rails app was pretty straight-forward.  I played around with MongoDB<br />
and the <a href="http://mongoid.org/">Mongoid </a>gem (for ORM) for a few days, making some sample projects and reading up about document design (vs schema design).  I&#8217;m not an expert on MongoDB but I got enough working to starting loading data in.</p>
<p>I created a sensor registration page and model where I identify sensors by name, ie: &#8220;sink&#8221;.  And then the json post updates the values for that sensor name and type.  I should really use IDs as keys but this is pretty easy and flexible to just use names.</p>
<div id="attachment_881" class="wp-caption aligncenter" style="width: 568px"><img src="http://squarism.com/wp-content/uploads/2010/12/sinks_sensor_list.png" alt="" title="sinks_sensor_list" width="558" height="187" class="size-full wp-image-881" /><p class="wp-caption-text">Sensor registration</p></div>
<p>The interesting part was flipping on <a href="http://mongoid.org/docs/extras/">versioning in the Mongoid gem</a>.  This gave me automatic versioning on historical states of the sink.  With this enabled, I could make a graph summarizing what hours the cat drinks the most (or whatever report).  Versioning is dead simple to enable but later it would turn tricky to work with because the queries are very different in MongoDB vs a SQL database.</p>
<p>My model with versioning looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Sink
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Mongoid::Document</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Mongoid::Timestamps</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Mongoid::Versioning</span>
&nbsp;
  validates_uniqueness_of <span style="color:#ff3333; font-weight:bold;">:name</span>
&nbsp;
  field <span style="color:#ff3333; font-weight:bold;">:running</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Boolean
  field <span style="color:#ff3333; font-weight:bold;">:proximity</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Boolean
  field <span style="color:#ff3333; font-weight:bold;">:collected_at</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> plot_by_hours
    hours_hash = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># count up our hours</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">versions</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># we only care about when the sink is running to count cat drinking</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> v.<span style="color:#9900CC;">running</span> == <span style="color:#0000FF; font-weight:bold;">true</span>
        hours_hash<span style="color:#006600; font-weight:bold;">&#91;</span>v.<span style="color:#9900CC;">collected_at</span>.<span style="color:#9900CC;">hour</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    hours_hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">collected_at</span>.<span style="color:#9900CC;">hour</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
&nbsp;
    total = <span style="color:#006666;">0</span>
    hours_hash.<span style="color:#9900CC;">each_key</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>h<span style="color:#006600; font-weight:bold;">|</span>
      total <span style="color:#006600; font-weight:bold;">+</span>= hours_hash<span style="color:#006600; font-weight:bold;">&#91;</span>h<span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    hours_percentage = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span>
    hours_hash.<span style="color:#9900CC;">each_key</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>h<span style="color:#006600; font-weight:bold;">|</span>
      percent = hours_hash<span style="color:#006600; font-weight:bold;">&#91;</span>h<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">/</span> total.<span style="color:#9900CC;">to_f</span>
      hours_percentage<span style="color:#006600; font-weight:bold;">&#91;</span>h<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>percent.<span style="color:#9900CC;">round</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">100</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># flot needs an array of arrays for data values</span>
    hours_percentage.<span style="color:#9900CC;">to_a</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The plot_by_hours method returns an array of of values that represents how many times the sink was tripped per hour.</p>
<pre>
 > Sink.first.plot_by_hours
 => [[23, 20.0], [22, 40.0], [18, 40.0]]
</pre>
<p>All this was enough to get a basic interface up and make a graph with <a href="http://code.google.com/p/flot/">flot</a>.</p>
<p>Certainly there&#8217;s a ton of refactoring that could be done (if you look at the code especially).  First, naming the model &#8220;Sink&#8221; is sort of stupid.  It should be called &#8220;Sensor&#8221; or something like that.  Second, I should <strong>not</strong> use <em>font color</em> tags because those <a href="http://www.w3schools.com/HTML/html_fonts.asp">are going away</a>.  Lastly, I should move the do-forever cat bridge serial monitor thing into a God process or something else that starts and ends with rails.  But that would couple it to the webserver.</p>
<h2>Development Problems</h2>
<p>While building this whole thing, I ran into some places where I got stuck.</p>
<p><strong>Servo buzzing</strong><br />
The biggest problem I had was getting the servo under control.  The servo would buzz when moving to a position because the resistance of the handle would not let it get completely to the target position.  For example:  90 is off and 180 is on.  It would try to turn off but get to 91.  At 91, it buzzes but it&#8217;s not moving enough to get to 90.  I solved this by jerking to 20 and then moving to 70,80 and then 90.  Seemed to work but then the microrax rig would eventually de-calibrate my fine tuning.  I was not happy about this and it wasn&#8217;t very elegant.  I tried many times simply to turn off the servo when it was done moving but many attempts would cause the servo to completely reset itself which would jerk the handle violently as it went back to position 0 and then to 90 (or 180 whatever).  For a long time I didn&#8217;t think turning off the servo was actually possible.  I thought the Arduino servo library was either buggy or incapable of doing what I wanted.  I kept playing with it and I really don&#8217;t know exactly how but I managed to get a bit of working code that did turn off the servo after moving.  This way, I don&#8217;t need to jerk to 91,70,80 or anything else weird.  I just move, turn off.  The snippet of relevant code for turning off an Arduino servo is below:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// full code available, this is just an excerpt</span>
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// attaches the servo on pin 9 to the servo object</span>
  servo.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// do forever loop</span>
<span style="color: #000066; font-weight: bold;">void</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// if cat detected, move faucet to on</span>
servo.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// reattach</span>
servo.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">105</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// or whatever angle</span>
servo.<span style="color: #006633;">detach</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this would jerk/spaz the servo out</span>
....
&nbsp;
<span style="color: #666666; font-style: italic;">// cat is gone, move faucet to off</span>
servo.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// reattach</span>
servo.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">85</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// or whatever angle for off</span>
servo.<span style="color: #006633;">detach</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this would jerk/spaz the servo out</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The important thing to get out of the code here is that the servo detaches after moving and only reattaches when it needs to move.  This is way better than just moving and staying attached because it won&#8217;t buzz if it doesn&#8217;t reach its destination.</p>
<p><strong>Frame torque</strong><br />
Torque on the faucet handle was a problem.  When the faucet would turn on, the opposite force would torque the frame.  I had to &#8220;anchor&#8221; it with some hobby putty.  A better solution would be to clamp it down but I didn&#8217;t have a lot of lip around my sink to do that.</p>
<p><strong>Flot</strong><br />
Flot is what I used to draw the graph.  It&#8217;s a client-side javascript graphing library.  Flot is nice but it gave me a lot of trouble trying to figure out exactly what format it needed the data coming from the rails webservice to return.  I had to play with this a lot and sometimes it would silently die (like javascript likes to do).  I was using Chrome to test but I switched to Firefox with Firebug to help catch the javascript errors.  Getting the javascript data from the rails app (really from the DB) to the jQuery graph was a pain.  I got stuck on this for at least a week.  But flot is really cool.  No imagemagick problems.</p>
<p><strong>Deployment Problems</strong><br />
After getting ready for &#8220;deployment&#8221; (where I wanted to take my code from my laptop and put it on my home Linux box), I found that Ubuntu was really out of date on my server.  So I ran a bunch of `do dist-upgrade&#8217; commands on it to update it and that&#8217;s when my server&#8217;s HDD died.  I mean, I guess it&#8217;s a logical time for it to die.  The dist-upgrade thing lights up the disks pretty good.</p>
<p>Luckily, I had the root disks mirrored so I could get all the data off (not much).  The problem was, it&#8217;s software raid and it was using mdadm instead of the newer LVM.  Compounded by that, it was still running some older version of Ubuntu so I couldn&#8217;t just switch to LVM and re-mirror the disks.  A bit of an environmental issue but lesson learned, just spring for <a href="http://www.newegg.com/Product/Product.aspx?Item=N82E16816116042">the 3ware card</a>.</p>
<p>So I ordered a new one and ran into problems there.  The box I am using is a Shuttle SFF thing.  It was supposed to have graphics on it.  Well guess what.  You have to buy a CPU with graphics on it (wut).  The i5-750 doesn&#8217;t have a GPU on it and the Shuttle VGA port is actually just a pass through for the GPU on the CPU.  So I had to buy a i5-650 which fixed this problem.</p>
<p>Then I had a virtual console resolution problem.  Console mode would boot up at 1680&#215;1050 (way too high a resolution and VGA cable noise).  <a href="http://ubuntuforums.org/showthread.php?t=1673551">Setting nomodeset fixed it</a>.  </p>
<p>Then I had a networking problem.  Large secure copies over SSH (or presumably anything) would cause kernel panic with r8169 network driver.  I tried moving to <a href="http://kernel.ubuntu.com/~kernel-ppa/mainline/v2.6.37-rc2-maverick/">2.6.37-20637rc2</a> off ubuntu&#8217;s ppa site but it would still happen.  I wasn&#8217;t even past the OS build at this point.  I was just trying to copy some files over (not even code) and the box would panic.  Awesome.  Well, I&#8217;m glad I found it early.  So eventually I found that the r8169 driver sucks.  Everyone said to use the r8168 driver even if that doesn&#8217;t match the chip exactly.  Apparently the devices are really similar but the drivers are very different in terms of stability.  Ok!  Yak shaving ho!</p>
<p>This solution worked.  It&#8217;s pretty lengthy and I&#8217;ll save it for another post.  There&#8217;s <a href="http://djlab.com/2010/10/fixing-rtl8111-8168b-driver-debian-ubuntu/comment-page-1/#comment-126">some stuff</a> on the web about it.  Basically you use dkms to build a module that supposed to be updated everytime you update the kernel.  It actually worked pretty well for a while but I had to undo it all because of the next problem.</p>
<p>So I had a working box at this point and I built up all the DBs, loaded code and tools (rails3, rvm, etc).  I documented all of the install as I went and made a cheat sheet that&#8217;s come in handy ever since (I&#8217;ll post it later).</p>
<p>Unforunately, I ran into massive problems with the usb-serial port driver.  My bridge script cat_faucet_bridge.rb wouldn&#8217;t start because it can&#8217;t open /dev/ttyS0 or /dev/ttyUSB0.  The new kernel had a problem with the usb-serial driver.  So I was stuck.  I could either have:</p>
<ol>
<li>a box that works with my Arduino but crashes under 90MB of network traffic transfer (or who knows under what load)</li>
<li>a box that doesn&#8217;t work with the Arduino but has a rock-solid network driver</li>
</ol>
<p>Awesome!  Either options are useless because I needed both.  I tried building my own kernel from source (which I&#8217;ve done many times back in the day) but it was turning out to be a lot of work because of how Ubuntu packages it&#8217;s thing and runs grub-update.  Also, picking modules can be tricky.  I didn&#8217;t want to get in my <a href="http://en.wikipedia.org/wiki/Back_to_the_Future">Delorean</a> and be a sysadmin again.  So I gave up.  Which turned out to be the right solution because another Ubuntu PPA hit (2.6.38-020638rc5-generic) that fixed everything.</p>
<h2>False hits</h2>
<p>So now the Arduino is running, sending data to the <em>cat bridge</em> which is posting JSON to rails which is storing everything in MongoDB.  It works great.  Now I can figure out at what hour in the day my cat drinks the most.  Except when I wash my hands in the sink and trip the IR sensor.  Now I have a MongoDB entry in the versions document that&#8217;s a false positive.  I need to go into the DB and remove it manually (or add an interface to do this).</p>
<p>My collection looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;5d5c9d56f2c31e1d0f000001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
<span style="color: #3366CC;">&quot;collected_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Tue Mar 01 2011 18:12:12 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span> 
<span style="color: #3366CC;">&quot;created_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Wed Feb 16 2011 23:00:23 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span> 
<span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;basement&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;proximity&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> 
<span style="color: #3366CC;">&quot;running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> 
<span style="color: #3366CC;">&quot;updated_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Fri Mar 04 2011 19:39:18 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;version&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> 
<span style="color: #3366CC;">&quot;versions&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;version&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;collected_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Wed Feb 16 2011 23:00:32 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;created_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Wed Feb 16 2011 23:00:23 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;basement&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;proximity&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;updated_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Wed Feb 16 2011 23:00:32 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4d6879ddf2c31e2ede000001&quot;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;version&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;collected_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Thu Feb 17 2011 22:16:05 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;created_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Wed Feb 16 2011 23:00:23 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;basement&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;proximity&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;updated_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Thu Feb 17 2011 22:16:05 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4d6879ddf2c31e2ede000002&quot;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;collected_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Thu Feb 17 2011 22:16:13 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;created_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Wed Feb 16 2011 23:00:23 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;basement&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;proximity&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;updated_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Thu Feb 17 2011 22:16:13 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;version&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4d6879ddf2c31e2ede000003&quot;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;collected_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Fri Feb 25 2011 22:56:13 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;created_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Wed Feb 16 2011 23:00:23 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;basement&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;proximity&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;updated_at&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Fri Feb 25 2011 22:56:13 GMT-0500 (EST)&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;version&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4d6879e4f2c31e2ede000004&quot;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So let&#8217;s say that I just tripped the sink.  The sink has a versions[] nested document because Mongoid has its versioning feature set to on.  Version 2 has a running:true attribute, Version 3 has running:false and the current version (5) is false.  That means the sink started off, turned on (by a cat) and turned off.  Then I accidentally turned it on (version 4).  I only graph and care about running:true because I&#8217;m graphing what hour the sink turns on.  So all I have to do is rollback the version in the versions embedded document.</p>
<p>Well it&#8217;s not that easy on the command line.  First of all, I really want to pop off a version and substitute the current version with what I popped off the versions[] array.  I don&#8217;t know how to do that in the mongo shell.  Maybe it&#8217;s possible.  I can explain (however temporary) how I fix problems like this until I do it in the ORM layer (Mongoid) and build a proper UI around it.</p>
<p>First, make a backup.  MongoDB is unforgiving.  The command <code>mongodump -d database_name -o ~/tmp</code> will dump a binary dump that is not plain-text like mysqldump (unfortunately).  I was able to drop and re-import a collection like this:</p>
<pre>
// drop the collection I just eff'd up in the mongo shell
> db.mycollection.remove();

# now I can restore my collection from the Unix shell
$ mongorestore -d database_name -c mycollection ~tmp/database_name/mycollection.bson
</pre>
<p>Of course there are many options other than this.  But now that I have some confidence in messing with my data directly, so I updated the false positive.  To do this, I need to delete 2 versions from my versions[] embedded document and possibly update the current document&#8217;s date.  I don&#8217;t always need to update the data (if it&#8217;s running:false I don&#8217;t care) but I thought I&#8217;d mention how to do it anyway.</p>
<pre>
// rollback the current document's date, replace the string in new Date() with a timestamp
db.sinks.update( { _id:ObjectId("5d5c9d56f2c31e1d0f000001") },
{$set: {'collected_at' : new Date("Tue Mar 01 2011 18:12:12 GMT-0500 (EST)") }});
// delete versions, sets to nulls
db.sinks.update( { _id:ObjectId("5d5c9d56f2c31e1d0f000001") }, {$unset: {'versions.3':1}});
db.sinks.update( { _id:ObjectId("5d5c9d56f2c31e1d0f000001") }, {$unset: {'versions.4':1}});
</pre>
<h2>Lessons Learned</h2>
<ol>
<li>The Arduino has very little memory and is annoying to code on.  This is because I&#8217;m used to rapid high level languages and embedded C is very far away from that.  I&#8217;m not trolling, it was really annoying when doing simple MD5 things took a night or two.  There&#8217;s also memory problems and when things crash they crash hard with no exception handling.  My best advice is to try to get your problems off the Arduino as soon as possible.</li>
<li>The adjustable nature of Microrax means you don&#8217;t have to be 1/16&#8243; accurate on your hacksaw skills.  This is nice.</li>
<li>If you build something intricate with Microrax, <a href="http://www.lynxmotion.com/c-144-microrax.aspx">order lots of</a> extra bolts, back-plates and joint pieces.  The lengths of aluminum channels don&#8217;t go quick but the joint pieces do.</li>
<li>Servo torque changes depending on the voltage given.</li>
<li>Installation is a bit tricky.  Don&#8217;t be afraid to change your design.</li>
<li>Calibration of the physical space is key.  Environmental and slight adjustments will be necessary unless you have many, many safeguards and overlapping functionality.
</li>
<li>Iterate, iterate, iterate</li>
<li>Don&#8217;t over-engineer.  Check your assumptions with simple tests.</li>
</ol>
<h2>The Biggest Lesson</h2>
<p>The biggest thing I learned from all this is how a project like this resonates with people more than software.  When a friend would come over, it was a more interesting presentation because of the physical component versus just a pure software demo.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/03/09/arduino-cat-faucet-with-mongodb-and-rails/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Arduino command protocol</title>
		<link>http://squarism.com/2010/02/28/arduino-command-protocol/</link>
		<comments>http://squarism.com/2010/02/28/arduino-command-protocol/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 16:40:57 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=354</guid>
		<description><![CDATA[UPDATE: Use CmdMessenger instead of Messenger. Here&#8217;s my IRC Arduino Bot. It uses a regular Arduino 328 and an Ethernet Shield both from sparkfun. As for software, I&#8217;m using the Ethernet2 library (see my previous post about this), the WString library and a homerolled IRC protocol parser. The breadboard&#8217;s power is connected to arbitrary pin [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:</strong> Use <a href="http://arduino.cc/playground/Code/CmdMessenger">CmdMessenger</a> instead of Messenger.</p>
<p>Here&#8217;s my IRC Arduino Bot.  It uses a regular <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=666">Arduino 328</a> and an <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9026">Ethernet Shield</a> both from sparkfun.  As for software, I&#8217;m using the Ethernet2 library (see <a href="http://squarism.com/2010/02/06/ethernet2-arduino-library-fix-on-0017/">my previous post</a> about this), the <a href="http://www.arduino.cc/en/Tutorial/TextString">WString library</a> and a homerolled <a href="http://www.ietf.org/rfc/rfc1459.txt">IRC protocol</a> parser.  The breadboard&#8217;s power is connected to arbitrary pin 5 and some resistors to keep the LED from burning out.</p>
<p><img src="http://squarism.com/wp-content/uploads/2010/02/arduino_irc_light-580x435.png" alt="arduino_irc_light" title="arduino_irc_light" width="580" height="435" class="aligncenter size-large wp-image-355" /></p>
<p>Basically, my bot joins an IRC channel and then listens for PRIVMSG commands starting with a password.  It takes those commands and controls an LED.  For example, I&#8217;d send this privately to the Arduino:<br />
<code>command password LEDON</code></p>
<p>And then the red LED comes on.  I tell it &#8220;LEDOFF&#8221; and it turns off.  Ok, it&#8217;s not a new RFC spec worthy of IEEE recognition and international adoption.  But it got the job done in a human-readable manner.  Previously on my facebook status light project, I had done much of the processing on my laptop and only send hex codes to the Arduino to light up LEDs.  The difference now is that the Arduino is doing the processing and no computer is needed.</p>
<p>While I was working on this little project, I had the bot join the channel and announce itself.<br />
<img src="http://squarism.com/wp-content/uploads/2010/02/irc_log.png" alt="irc_log" title="irc_log" width="556" height="372" class="aligncenter size-full wp-image-356" /></p>
<p>At one point, I was working on code and then my bot would disconnect.  I checked the serial monitor and the server seemed to drop me after a few minutes.  The channel would say that I timed out.  I realized that I wasn&#8217;t responding to the PING from the server.  So I threw in some code that checks for anything from the server that starts with &#8220;PING :&#8221;.  I then respond with &#8220;PONG&#8221;.  I remember seeing PING?/PONG! messages in mIRC back in the day.  Now it makes sense why mIRC was doing that in the console window.</p>
<p>It works great and I was excited about how much this little board could do in 14KB.  And then I kept testing it.  After about 7 or 8 &#8220;turn on&#8221; and &#8220;turn off&#8221; commands, the Arduino wouldn&#8217;t do anything anymore.  It&#8217;s like it just froze.  If I typed 5 commands, it&#8217;d stay connected for a long time.  But every time I&#8217;d send it 7 to 8 commands, it would lock up.  And by lock up, I mean the commands wouldn&#8217;t work anymore and it would time out from the server.  WTF.  So close!</p>
<p>So I figure that it&#8217;s something to do with pointers and memory.  I really don&#8217;t have a solid grasp on pointers and C.  I got a lot of this working by iterative experimentation over many days.  So I was looking for a better way to send human readable commands to my bot.  By human readable I mean something that works like a unix command &#8220;command arg1 arg2&#8243;.  Of course this human readable bit introduces strings which is tricky enough in C (for me) and even worse on the Arduino.  I figured this was a problem that someone smarter than me had solved.</p>
<p>I found a library called <a href="http://www.arduino.cc/playground/Code/Messenger">Messenger</a>.  It&#8217;s pretty simple to install, just throw it in your ~/Documents/Arduino/libraries folder on Mac and um &#8230; the equivalent on Windows?  There are examples in the Messenger folder that you can checkout.  HOWEVER the whole point of me posting this big long thing is the following.</p>
<p>The example checkString really threw me for a loop.  It did exactly what I need it do to in a much cleaner way.  I uploaded to the Arduino and then broke out to a shell.</p>
<p><code>$ screen /dev/tty.usbserial-A9005bCr 115200</code></p>
<p>Substitute your virtual usb device for the /dev/tty path.  Note that the sketch uses 115k serial speed.  You won&#8217;t see anything when you type but if you hit &#8220;enter&#8221; (to clear the buffer) &#8220;on[enter]&#8221; in screen LED 13 will turn on.  Type &#8220;off[enter]&#8221; ([enter] means the enter key) and it will turn off.  Great!  Exactly what I need.  But then I tried typing &#8220;on&#8221; then &#8220;off&#8221; then &#8220;muffins&#8221; then &#8220;on&#8221; and the light stayed off.  Any garbage gets the Arduino stuck like my sketch.  Ok, is what I&#8217;m trying to do impossible or is this just coincidence?</p>
<p>I modified the checkString example to look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This example demonstrates Messenger's checkString method</span>
<span style="color: #666666; font-style: italic;">// It turns on the LED attached to pin 13 if it receives &quot;on&quot;</span>
<span style="color: #666666; font-style: italic;">// It turns it off if it receives &quot;off&quot;</span>
&nbsp;
&nbsp;
#include <span style="color: #339933;">&lt;</span>Messenger.<span style="color: #006633;">h</span><span style="color: #339933;">&gt;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// Instantiate Messenger object with the message function and the default separator </span>
<span style="color: #666666; font-style: italic;">// (the space character)</span>
Messenger message <span style="color: #339933;">=</span> Messenger<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// Define messenger function</span>
<span style="color: #000066; font-weight: bold;">void</span> messageCompleted<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// This loop will echo each element of the message separately</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> message.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> message.<span style="color: #006633;">checkString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;on&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      digitalWrite<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">13</span>,HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> message.<span style="color: #006633;">checkString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;off&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      digitalWrite<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">13</span>,LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>       <span style="color: #666666; font-style: italic;">// ADD THIS</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// ADD THIS</span>
    <span style="color: #009900;">&#125;</span>                <span style="color: #666666; font-style: italic;">// ADD THIS</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Initiate Serial Communication</span>
  Serial.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">115200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  message.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span>messageCompleted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  pinMode<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">13</span>,OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The following line is the most effective way of </span>
  <span style="color: #666666; font-style: italic;">// feeding the serial data to Messenger</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> Serial.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> message.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span> Serial.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I added the break and it&#8217;s able to deal with garbage.  I tested more than 20 commands with banging on the keyboard in between and it seems pretty solid.  Now I just need to integrate this with my IRC bot and I might have something that can stay online for a while.</p>
<p>By the way, after you use screen to connect to a serial port, hit &#8220;Ctrl+A, k&#8221; to kill the window and break out of screen.</p>
<p><strong>Update</strong>: People have asked for the code.  It&#8217;s posted after the break.<br />
<span id="more-354"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// UNSTABLE IRC BOT</span>
<span style="color: #666666; font-style: italic;">// TODO: Integrate with Messenger.h for stability</span>
#include <span style="color: #339933;">&lt;</span>Ethernet2.<span style="color: #006633;">h</span><span style="color: #339933;">&gt;</span>
#include <span style="color: #339933;">&lt;</span>WString.<span style="color: #006633;">h</span><span style="color: #339933;">&gt;</span>
&nbsp;
#define STRING_BUFFER_SIZE <span style="color: #cc66cc;">128</span>
<span style="color: #003399;">String</span> buffer <span style="color: #339933;">=</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>STRING_BUFFER_SIZE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// string variable to hold server name detection</span>
<span style="color: #003399;">String</span> hostString <span style="color: #339933;">=</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">byte</span> mac<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// CHANGE ME</span>
<span style="color: #000066; font-weight: bold;">byte</span> ip<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// arduino IP: 1.2.3.4</span>
<span style="color: #000066; font-weight: bold;">byte</span> gateway<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// your router IP: 1.2.3.4</span>
<span style="color: #000066; font-weight: bold;">byte</span> subnet<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// your network mask</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// irc.freenode.net</span>
<span style="color: #000066; font-weight: bold;">byte</span> ircServer<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">74</span>, <span style="color: #cc66cc;">208</span>, <span style="color: #cc66cc;">174</span>, <span style="color: #cc66cc;">239</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// IP of freenode.net</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> responseWait <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// default: 1000</span>
<span style="color: #000066; font-weight: bold;">int</span> joinWait <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// default: 1000</span>
<span style="color: #000066; font-weight: bold;">int</span> pingCheckThrottle <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// check for PING event every 10 seconds once connected</span>
<span style="color: #000066; font-weight: bold;">int</span> reconnectWait <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// CHANGE ME</span>
<span style="color: #000066; font-weight: bold;">char</span> ircNick<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DuinoFace&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span> channel<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;#freebsToTheDee&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">boolean</span> isLoggedIn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
Client client<span style="color: #009900;">&#40;</span>ircServer, <span style="color: #cc66cc;">6667</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// turn on when debugging</span>
<span style="color: #000066; font-weight: bold;">boolean</span> isDebugOn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// logged in (in channel) status light</span>
<span style="color: #000066; font-weight: bold;">int</span> ledPin <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//router time?</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// opens serial port, sets data rate to 9600 bps</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Say hello:</span>
  debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;String Library version: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  debug<span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">version</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Ethernet.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span>mac, ip, gateway, subnet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;client.connect()&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>client.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;client.connect() failed.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// status light</span>
  pinMode<span style="color: #009900;">&#40;</span>ledPin, OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>client.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> client.<span style="color: #006633;">connected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// parse the incoming buffer</span>
    readIRCRequest<span style="color: #009900;">&#40;</span>client<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>client.<span style="color: #006633;">connected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Disconnected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    delay<span style="color: #009900;">&#40;</span>reconnectWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Reconnecting...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    isLoggedIn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> readIRCRequest<span style="color: #009900;">&#40;</span>Client client<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">char</span> inChar <span style="color: #339933;">=</span> client.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>inChar <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    buffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>inChar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Buffer: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isLoggedIn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;NOTICE * :*** No Ident response&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        buffer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
      <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:End of /MOTD command.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        join<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
      <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:End of /NAMES list.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        say<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        isLoggedIn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span> 
    <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      checkCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
      checkPing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">//delay(pingCheckThrottle);</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// clear out buffer for next line read</span>
    buffer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/* We need to send something that looks like this
 *   NICK yaytofu
 *   USER yaytofu 0 * yaytofu
 * Where yaytofu is our nickname
 */</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> msg <span style="color: #339933;">=</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  msg <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;NICK &quot;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ircNick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//if (debug) Serial.println(msg);</span>
  debug<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* wait for a response */</span>
&nbsp;
  msg <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;USER &quot;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ircNick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; 0 * &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ircNick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  debug<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* wait for a response */</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> join<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  delay<span style="color: #009900;">&#40;</span>joinWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JOIN...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>strcat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JOIN &quot;</span>, channel<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> say<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//if (debug) Serial.println(&quot;Sending something to the channel.&quot;);</span>
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sending something to channel.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PRIVMSG #freebsToTheDee :Arduino with Ethernet Shield is here.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> say<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sending something to channel.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PRIVMSG #freebsToTheDee :&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> checkPing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PING :&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GOT PING!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// parse and store hostname in string</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hostString.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      hostString <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>, buffer.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// respond to ping</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PONG!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PONG &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>hostString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> checkCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// :nick!~nick@static-1-13-5-1.domain.com PRIVMSG #freebsToTheDee :wut</span>
  <span style="color: #666666; font-style: italic;">// state codes 0=DNS   1=PRIVMSG   2=channel  3=message</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PRIVMSG&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">int</span> state <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// split on spaces, TODO: state check</span>
    <span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>bufferArray <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">getChars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> buffer.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">char</span> test <span style="color: #339933;">=</span> bufferArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>test <span style="color: #339933;">==</span> <span style="color: #0000ff;">' '</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;found space at:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        debug<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        spaces<span style="color: #009900;">&#91;</span>state<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
        state<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #003399;">String</span> dnsName <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> privMsg <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span>, spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> channel <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span>, spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> cmd <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">2</span>, buffer.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dnsName:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>dnsName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;privMsg:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>privMsg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;channel:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>channel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cmd:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isLoggedIn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Setting status light to ON.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    digitalWrite<span style="color: #009900;">&#40;</span>ledPin, HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Setting status light to OFF.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    digitalWrite<span style="color: #009900;">&#40;</span>ledPin, LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// No carriage return</span>
<span style="color: #000066; font-weight: bold;">void</span> debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// No carriage return overloaded</span>
<span style="color: #000066; font-weight: bold;">void</span> debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Carriage return</span>
<span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Carriage return overloaded</span>
<span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Be sure to change the two sections of CHANGE ME.  Also please note that like I said in my post, I never integrated these two source files together.  So the IRC bot is unstable after about 8 commands.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2010/02/28/arduino-command-protocol/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Basic 555 timer Arduino project</title>
		<link>http://squarism.com/2010/02/20/basic-555-timer-arduino-project/</link>
		<comments>http://squarism.com/2010/02/20/basic-555-timer-arduino-project/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 21:27:30 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=336</guid>
		<description><![CDATA[I put together the arduino protoshield from sparkfun. Excellent instructions at atomicsalad. Even though it&#8217;s a standard through-hole kit, atomicsalad&#8217;s instructions were nice to follow along. After it was put together I had a few built-in LED lights to play with. I had seen this 555 timer in a piece kit I bought and I [...]]]></description>
			<content:encoded><![CDATA[<p>I put together the arduino protoshield from sparkfun.  Excellent instructions at <a href="http://www.atomicsalad.com/archive/2010/02/02/tutorial_sparkfun_protoshield_for_arduino_assembly_updated.php">atomicsalad</a>.  Even though it&#8217;s a standard through-hole kit, atomicsalad&#8217;s instructions were nice to follow along.</p>
<p>After it was put together I had a few built-in LED lights to play with.  I had seen this 555 timer in a piece kit I bought and I wondered what it does.  I found a ton of examples and apparently you can do a zillion things with it.  I wanted to start with the basics so I set out to make a light blink using only hardware.  I&#8217;m only using the arduino for power in this one.</p>
<p><img src="http://squarism.com/wp-content/uploads/2010/02/jump_breadboard_at_30-150x150.jpg" alt="jump_breadboard_at_30" title="jump_breadboard_at_30" width="150" height="150" class="alignright size-thumbnail wp-image-337" /><br />
I found a great tutorial on youtube and modified it to work on a larger breadboard.  I had some trouble because it turns out that <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=112">this breadboard</a> has its power rails split down the middle.  I was confused and stuck on this for a day.  Eventually I found a video that hinted at this fact and connected the two sides like so.  After that, the power rails on each side will work like I expected.  This is by design on these boards so that you can have two different voltages but I found it annoying.</p>
<p>So here&#8217;s the schematic that I followed for the configuration on the big breadboard.<br />
<img src="http://squarism.com/wp-content/uploads/2010/02/555_bb.png" alt="555_bb" title="555_bb" width="349" height="216" class="aligncenter size-full wp-image-340" /></p>
<p>I transferred this layout onto a mini breadboard and it works like a champ.  The protoshield has room for a <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8803">mini self-adhesive breadboard</a>.  Here&#8217;s the actual wiring shot.  This uses a light on the protoshield vs the light on the breadboard schematic.  The concept is the same though.  Pin3 on the 555 is the output which makes the light blink rapidly.  You could use this chip as a cpu clock for example.<br />
<img src="http://squarism.com/wp-content/uploads/2010/02/555_timer_wiring-580x435.jpg" alt="555_timer_wiring" title="555_timer_wiring" width="580" height="435" class="aligncenter size-large wp-image-342" /></p>
<p>You don&#8217;t have to use the breadboard switch (the black thing on the left), it&#8217;s just more convenient to flip a switch rather than plug in/out of the 3v.  Oh yeah it&#8217;s powered by 3v on the arduino.  The cap is 100uf.  You can use 2x orange,orange,maroon (gold) resistors or one of those and a blue,gray,maroon one (sorry I don&#8217;t have the resistance values).  Play with the resistor values to change the blink rate.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2010/02/20/basic-555-timer-arduino-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ethernet2 Arduino Library Fix on 0017</title>
		<link>http://squarism.com/2010/02/06/ethernet2-arduino-library-fix-on-0017/</link>
		<comments>http://squarism.com/2010/02/06/ethernet2-arduino-library-fix-on-0017/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 03:58:46 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=310</guid>
		<description><![CDATA[Ethernet2 lib. It&#8217;s a lighterweight and better Ethernet class. Head into your ~/Documents/Arduino/libraries (or Windows equivalent) and checkout the SVN project: $ svn co http://tinkerit.googlecode.com/svn/trunk/Ethernet2%20library/Ethernet2 Now change your sketch to use Ethernet2.h instead of Ethernet.h. Wondeful? No. You&#8217;ll get this error in 0017. 'EthernetClass' has not been declared Ok, the forums have a fix. But [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2010/02/arduino_0017.png" alt="arduino_0017" title="arduino_0017" width="425" height="300" class="aligncenter size-full wp-image-316" /><br />
Ethernet2 lib.  It&#8217;s a lighterweight and better Ethernet class.  Head into your ~/Documents/Arduino/libraries (or Windows equivalent) and checkout the SVN project:</p>
<p><code>$ svn co http://tinkerit.googlecode.com/svn/trunk/Ethernet2%20library/Ethernet2</code></p>
<p>Now change your sketch to use Ethernet2.h instead of Ethernet.h.  Wondeful?  No.  You&#8217;ll get this error in 0017.</p>
<p><code>'EthernetClass' has not been declared</code></p>
<p>Ok, the forums have a fix.  But it didn&#8217;t work for the longest time because I didn&#8217;t know what they meant by Server.cpp.  There&#8217;s two of them!  The original or the new one?!  If you edit the old one you&#8217;ll get this:</p>
<p><code>Print.cpp:129: first defined here</code></p>
<p>So:<br />
1. Edit Server.cpp in Ethernet2 to include Ethernet2.h and not Ethernet.h.<br />
2. Delete the Print.cpp and Print.h files from Ethernet2.<br />
3. Compile.<br />
4. Have some yay.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2010/02/06/ethernet2-arduino-library-fix-on-0017/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Arduino Sessions</title>
		<link>http://squarism.com/2010/01/23/arduino-sessions/</link>
		<comments>http://squarism.com/2010/01/23/arduino-sessions/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 17:14:07 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=287</guid>
		<description><![CDATA[Hacking around with sparkfun shipments and components. I got a starter set with a box. Included a 7 segment led. Got it to display the number 4. Moved on to a capcitor test. Arduino playground has a read/discharge example. It reads how many milliseconds it takes to charge/discharge. I got it working but don&#8217;t understand [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2010/01/seven_segment_led-225x300.jpg" alt="seven_segment_led" title="seven_segment_led" width="225" height="300" class="aligncenter size-medium wp-image-295" />Hacking around with sparkfun shipments and components.  I got a starter set with a box. Included a 7 segment led. Got it to display the number 4.  Moved on to a capcitor test. Arduino playground has a read/discharge example.  It reads how many milliseconds it takes to charge/discharge.  I got it working but don&#8217;t understand it still.  I did a potentiometer test.  A basic rocker switch test.  Resistors in serial, resistors in parallel.  I made a push switch toggle on/off led light with a transistor (I think).  Transistor testing (those things are weird).  Just kinda went through the box and saw what did what.  I want to understand multiplexing and make a 2 digit or more 7-segment LED display work.  I have a bunch of wire and things to make it work now.  I need a resistor kit, sparkfun was sold out recently but now they have them.  It&#8217;s a kit of every resistor you can think of.</p>
<p><img src="http://squarism.com/wp-content/uploads/2010/01/wingshield-580x435.jpg" alt="wingshield" title="wingshield" width="580" height="435" class="aligncenter size-medium wp-image-296" /><br />
I also put together a wingshield kit.  I makes connecting wires to the arduino a little more stable because you can screw them into the screw terminals.  I seriously screwed up at one point, snipping off the header pins when you aren&#8217;t supposed to snip them, doh (what will plug in to the headers if you snip the pins?).  I had to desolder and use parts from another kit.  Amazingly the desoldering went better (although it takes a long time) and the shield seems to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2010/01/23/arduino-sessions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sparkfun Onslaught</title>
		<link>http://squarism.com/2010/01/07/sparkfun-onslaught/</link>
		<comments>http://squarism.com/2010/01/07/sparkfun-onslaught/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 03:42:12 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=282</guid>
		<description><![CDATA[Sparkfun had their free day today. I tried to nab some stuff but their servers were too busy and I couldn&#8217;t sit there and refresh all day (what am I idle rich?). I just happened to have an order placed the week before and it all came in today. WRL-08664 XBee 1mW Chip Antenna DEV-09063 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2010/01/simon_sparkfun.jpg" alt="simon_sparkfun" title="simon_sparkfun" width="550" height="413" class="aligncenter size-full wp-image-283" /><br />
Sparkfun had their free day today.  I tried to nab some stuff but their servers were too busy and I couldn&#8217;t sit there and refresh all day (what am I idle rich?).  I just happened to have an order placed the week before and it all came in today.</p>
<p><code>WRL-08664 XBee 1mW Chip Antenna<br />
DEV-09063 Arduino XBee Shield Empty<br />
KIT-09285 Beginner Parts Kit<br />
DEV-09282 ScrewShield<br />
DEV-00666 Arduino USB Board<br />
LCD-00258 Serial Enabled LCD Backpack<br />
TOL-09465 Tool Kit - Beginner<br />
TOL-09317 Third Hand<br />
KIT-09343 Simon Game - Through-Hole Soldering Kit<br />
TOL-00298 Wall Adapter Power Supply - 9VDC 650mA<br />
SEN-08958 Infrared Proximity Sensor Long Range - Sharp GP2Y0A02YK0F<br />
SEN-08733 Infrared Sensor Jumper Wire - 3-Pin JST</code></p>
<p>Chief amongst the loot was a soldering iron and a simon game kit.  Really, I could care less if the thing worked.  I wanted to learn soldering.  I seriously have never done it before.  I couldn&#8217;t believe my eyes when the damn thing lit up and worked!  I thought for sure my scorch marks had screwed something up.  When I was soldering the legs on the mega-168 processor the thing got warm.  I was working kind of fast and nervous.  I guess I didn&#8217;t burn anything too bad.  Many of my connections look like shiz and I have pointy parts all over the place.   I was really trying to peer and study how the solder was making the connections.  I&#8217;d shake and bend the pins to see how much strength it had and so on.  I was really surprised at out unintuitive the whole process is.  You&#8217;d think it&#8217;d be harder but after doing the same pins over and over you start to see what a good solder bead looks like.  I just followed the instructions (some of which were a bit off because of a newer board rev) and it wasn&#8217;t too hard when I took my time and read ahead making sure I wasn&#8217;t doing something stupid.</p>
<p><img src="http://squarism.com/wp-content/uploads/2010/01/helping_hands.jpg" alt="helping_hands" title="helping_hands" width="550" height="413" class="aligncenter size-full wp-image-284" />Crazy pants!  I can&#8217;t believe it worked!  The cheapy soldering iron and solder they sent with a starter kit worked really well.  I just held the solder inside the plastic tube it came in.   I thought that the cheapy iron would be crappy but it worked well.  I got some helping hands to help.  They helped.  :D  What was also helpful was having a workbench, damp sponge, helping hands and some youtube videos for instruction.  It&#8217;s just not as hard as I thought it was.  Now SMD soldering, I have no idea how people do that.</p>
<p>So really I was expecting the kit to be a learning experience and I&#8217;d throw it away.  But somehow, despite the weird and PCB-stress-inducing battery clips, the Simon game worked.  When it lit up and I played a little game of simon, I was really surprised.  Kristin has it on the coffee table and she plays it a lot.  I don&#8217;t think she ever played it in the 80s, weird.  I tweeted about my success and the freaking official Sparkfun twitter account saw my #sparkfun tag and congratulated me.  Awesome!</p>
<p>After a holiday break, a sudden rush of software-type-hacky-motivation came to me.  I grabbed my arduino and laptop and checked it out.  First, it didn&#8217;t work.  The laptop is new and I had to set up the dev-env again.  Apparently the default now is the ATmega328 and my Arduino is based on the ATmega168.  After that was settled, I had a led blinking again.  </p>
<p>So I had this Hitachi based LCD panel that I had never done anything with.  I got it powered (3.3v) and was trying to figure out how to run the thing.  I read and read and found that I didn&#8217;t have the right crap on hand.  First, it seems like there&#8217;s two ways to drive it.  You can drive it &#8220;manually&#8221; with a bunch of parallel type wires or you can buy a serial backpack and solder it on (I think).  I tried getting things working parallel but it turned out to be a little more than I was comfortable with.  I don&#8217;t understand multiplexing yet and I actually didn&#8217;t have enough hook up wires to get it done.  So part of the order you see up top is a serial backpack.  Well somehow I managed to get it wired up but it&#8217;d short out my Arduino board, I was just using the board for power.  My LED would fade away, it seems to do that when I short something out.  Thinking that this was a power problem, I plugged in the 9v adapter with the usb (5v).  Well then I started smelling a plastic smell.  I heard a crackle and then I saw a little bubble appear on the backpack chip.  Ow.  I threw it away and I don&#8217;t have a picture of it.  All it reminded me off is when Tim and I burned up a southbridge on my old AMD computer.  Smells like fail.</p>
<p>So I just played around with the broken backpack.  I desoldered some of the headers off for practice.  Desoldering is hard, I burned up the PCB pretty bad but it was a good learning experience.  Then threw it away.  I ordered a proper serial LCD from sparkfun.  I recently got that working electrically although the code side of it I need to clean up a little bit and grok it some more.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2010/01/07/sparkfun-onslaught/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook Arduino Lamp</title>
		<link>http://squarism.com/2009/02/17/facebook-arduino-lamp/</link>
		<comments>http://squarism.com/2009/02/17/facebook-arduino-lamp/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 00:42:36 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://squarism.com/2009/02/17/facebook-arduino-lamp/</guid>
		<description><![CDATA[Overview I built an arduino project that monitor&#8217;s Facebook online status (the chat app in the lower right when you are on the facebook page). It lights up bright when someone is Online, dimly lit when idle and is unlit when someone is offline. I wired 3 LEDs so it can only monitor 3 people [...]]]></description>
			<content:encoded><![CDATA[<h4>Overview</h4>
<p><a href="http://squarism.com/2009/02/17/facebook-arduino-lamp/facebook-chat/"><img src="http://squarism.com/wp-content/uploads/2009/02/Facebook_Chat.png" alt="Facebook Chat" title="Facebook Chat" width="202" height="24" class="alignnone size-medium wp-image-1304" /></a><br />
I built an arduino project that monitor&#8217;s Facebook online status (the chat app in the lower right when you are on the facebook page).  It lights up bright when someone is Online, dimly lit when idle and is unlit when someone is offline.  I wired 3 LEDs so it can only monitor 3 people as of right now.  It won&#8217;t tell you who is who because there&#8217;s no text display.  I suppose you could make little stickers.  In the end, it&#8217;s a simple little project that provides a benefit of being able to have a small secondary display of who&#8217;s on and not.  Is the little device a keeper?  Would people want it?  Probably not.  This was a test.</p>
<p>It was a fun little project but it had a lot of challenges.  First, this is an online app with dependancies on online resources.  I read forums and wikis and saw that much has changed lately with Facebook&#8217;s API.  So this is an article about how I got it working but doesn&#8217;t mean that it&#8217;s going to work for you.  Things change online and at best this might be a starting point in how to &#8220;get your arduino online&#8221;.  When things change online, this article can get dated.</p>
<p>And about that online bit.  The arduino is just a simple microprocessor.  I found that a lot of fancy Java features won&#8217;t run on it.  Strings, System.arraycopy and many network Java packages <a href="http://arduino.cc/en/Reference/HomePage">just aren&#8217;t supported</a>.  So you&#8217;re going to need a laptop or computer hooked up to the arduino to make this work.  The arduino doesn&#8217;t have a direct network adapter on it, a network stack or a lot of RAM.  It&#8217;s job is to do the simple stuff like blink LEDs.  So I found it&#8217;s best to keep the arduino code simple.</p>
<p>Two apps run.  One is the Processing app which connects to Facebook and queries 3 facebook IDs for their status.  One is an Arduino app that simply loads the code onto the Arduino.  The Processing app has to stay open because it queries every 10 seconds and sends special codes to the Arduino.  The codes may look like this:<br />
<code><br />
#0FF = 1st LED full brightness, 1st friend is online<br />
#110 = 2nd LED dimly lit, 2nd friend is idle<br />
#000 = 1st LED off, 1st friend went offline<br />
#2FF = 3rd LED full brightness, 3rd friend came online<br />
</code><br />
The first character, #, is just a marker.  The next character tells the arduino which LED to light up (starts with 0),  The next two characters is a hexadecimal number telling how bright to light up the light 0-255.  So the Processing app queries Facebook and sends a code over serial to the Arduino.  The Arduino parses the code and changes the LEDs.</p>
<h4>Dependencies</h4>
<p>There are a ton of dependencies that you&#8217;re going to need.<br />
- Facebook account<br />
- A dummy app on facebook, instructions below.<br />
- An infinite session key (also called offline access).  This allows you to create a desktop app that doesn&#8217;t have to be in a web container (nor prompt for a facebook login).<br />
- An arduino and some simple electronics components.  I ordered <a href="http://www.amazon.com/Arduino-Duemilanove-Starter-Kit/dp/B001N1EOT8/ref=pd_bbs_sr_1?ie=UTF8&#038;s=miscellaneous&#038;qid=1235100469&#038;sr=8-1">a starter kit</a> from Amazon for $40.<br />
- <a href="http://processing.org/download/index.html">Processing</a><br />
- <a href="http://www.arduino.cc/en/Main/Software">The arduino IDE</a><br />
- <a href="http://code.google.com/p/facebook-java-api/">A facebook java API</a> from google code.  It has some dependancies that should be covered by the below steps.</p>
<h4>Wiring the arduino</h4>
<p><a href="http://squarism.com/2009/02/17/facebook-arduino-lamp/facebook-arduino-lamp-wiring/"><img src="http://squarism.com/wp-content/uploads/2009/02/facebook_arduino_lamp_wiring-300x198.jpg" alt="facebook arduino lamp wiring" title="facebook arduino lamp wiring" width="300" height="198" class="alignnone size-medium wp-image-1301" /></a><br />
If you mess with the arduino a lot, it shouldn&#8217;t be a problem to wire 3 LEDs.  But I&#8217;ll try my best with my non EE background and crappy drawing skills.  The longer lead on the LED is the + side (the signal) and the shorter is the ground.  Put your LEDs anywhere on your breadboard, + on the left.  Run 3 signal wires to your breadboard and connect them to the + side (the left).  Each LED needs to be grounded.  I used a common ground along the top (the &#8211; symbol at the top left of the above diagram).  I didn&#8217;t use the common + on the breadboard because each LED needs to light up independently.</p>
<p><a href="http://squarism.com/2009/02/17/facebook-arduino-lamp/facebook-arduino-lamp-wiring-photo/"><img src="http://squarism.com/wp-content/uploads/2009/02/facebook_arduino_lamp_wiring_photo-300x225.jpg" alt="facebook arduino lamp wiring photo" title="facebook arduino lamp wiring photo" width="300" height="225" class="alignnone size-medium wp-image-1302" /></a><br />
This is the basic approach I took wiring mine.  Now, my green LEDs are weak so I added a few to make it brighter.  My blue LED is blinding so I added a resistor in the lead to it (still is too bright).  It&#8217;s pretty messy in there.</p>
<p>Arduino code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> val <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span> buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span> oldBuffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> pointer <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">byte</span> inByte <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define pins</span>
<span style="color: #000066; font-weight: bold;">int</span> red <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> green <span style="color: #339933;">=</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> blue <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">byte</span> r <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">byte</span> g <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">byte</span> b <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #666666; font-style: italic;">// set pinouts</span>
  pinMode<span style="color: #009900;">&#40;</span>red,OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pinMode<span style="color: #009900;">&#40;</span>green,OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pinMode<span style="color: #009900;">&#40;</span>blue,OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// open serial port</span>
  Serial.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DONE WITH SETUP!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Serial.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// read incoming byte</span>
    inByte <span style="color: #339933;">=</span> Serial.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>inByte <span style="color: #339933;">==</span><span style="color: #0000ff;">'#'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Got something with #&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>inByte, DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>pointer <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        buffer<span style="color: #009900;">&#91;</span>pointer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> Serial.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pointer<span style="color: #339933;">++;</span>
        Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span>pointer<span style="color: #009900;">&#93;</span>, DEC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
     <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'0'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>     
      r <span style="color: #339933;">=</span> hex2dec<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> hex2dec<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>     
      g <span style="color: #339933;">=</span> hex2dec<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> hex2dec<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'2'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>     
      b <span style="color: #339933;">=</span> hex2dec<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> hex2dec<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>     
    <span style="color: #009900;">&#125;</span>
&nbsp;
      analogWrite<span style="color: #009900;">&#40;</span>red, r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      analogWrite<span style="color: #009900;">&#40;</span>green, g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      analogWrite<span style="color: #009900;">&#40;</span>blue, b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      pointer <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>      
      <span style="color: #009900;">&#125;</span>
&nbsp;
      delay<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// convert on HEX characeter into decimal number</span>
<span style="color: #000066; font-weight: bold;">int</span> hex2dec<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&gt;=</span> <span style="color: #0000ff;">'0'</span> <span style="color: #339933;">&amp;&amp;</span> c <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">'9'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> c <span style="color: #339933;">-</span> <span style="color: #0000ff;">'0'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&gt;=</span> <span style="color: #0000ff;">'A'</span> <span style="color: #339933;">&amp;&amp;</span> c <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">'F'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> c <span style="color: #339933;">-</span> <span style="color: #0000ff;">'A'</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://squarism.com/2009/02/17/facebook-arduino-lamp/arduino-ide-menubar/"><img src="http://squarism.com/wp-content/uploads/2009/02/arduino_ide_menubar.png" alt="arduino ide menubar" title="arduino ide menubar" width="209" height="37" class="alignnone size-medium wp-image-1303" /></a><br />
Fire up the Arduino IDE and paste this in.  Hit Command+U (or the menu equivalent) to upload the sketch to the board.  Now click the serial monitoring button (it&#8217;s the one all the way to the right).  This will let you send and receive serial messages.  Try sending #0FF.  The board should light up the first blue LED all the way to full brightness.  Try #1FF and #2FF.  If those work then you&#8217;re good to go.</p>
<p>Now comes the Facebook stuff.  This part takes the longest.  Sign into facebook (of course you have an account) and go to <a href="http://developer.facebook.com/">http://developer.facebook.com/</a>.  Create a new app, just follow the wizard steps (you can name it anything).  Then write down your API_KEY and SECRET_KEY.  They&#8217;ll be used quite a bit below.</p>
<p>First, we need to get an infinite session key from Facebook.  I found some code online that gets part of the way there but it was extremely confusing as to what to do with it.  It took me many hours to realize that what is returned by this class is the infinite session key.</p>
<p>Go to the main method below and change API_KEY, SECRET_KEY and AUTH_TOKEN_HERE.  Compile this code with javac (or in Eclipse).  Now run it.  It will return a raw http response with a session element in it.  It will be a long hash type value.  Copy this, this is the infinite session key.</p>
<h4>Infinite session key code</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* 
 * Web Site: http://as-m-going-on.blogspot.com/2010/05/tiny-java-connector-for-facebook.html
 * Author: Prasanta Paul 
 *---------------------------------------------- 
 *Command Line Option 
 *-Dhttp.proxyHost=your.proxy.dom -Dhttp.proxyPort=XX 
 *----------------------------------------------- 
 * TODO: Write a wrapper for HTTP Post 
 * TODO: XML Parser to parse API output 
 */</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.excoflare.facebook</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.HttpURLConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLEncoder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.MessageDigest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Arrays</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FBAgent <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">URL</span> FB_URL <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> SECRET<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> apiReqParms<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> parmIndex <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> FBAgent<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> numParms<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			parmIndex <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			apiReqParms <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span>numParms<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			FB_URL <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://api.facebook.com/restserver.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error: &quot;</span> <span style="color: #339933;">+</span> ex.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAPISecret<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> secret<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">SECRET</span> <span style="color: #339933;">=</span> secret<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addAPIParm<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		apiReqParms<span style="color: #009900;">&#91;</span>parmIndex<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>key <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> value<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendHTTPRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> reqParms <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> apiReqParms.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			reqParms <span style="color: #339933;">+=</span> apiReqParms<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			reqParms <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// Add Signature</span>
		reqParms <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;sig=&quot;</span> <span style="color: #339933;">+</span> genSig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Data to Reg=&quot;</span> <span style="color: #339933;">+</span> reqParms<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>FB_URL <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Empty URL....&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">HttpURLConnection</span> conn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span> FB_URL.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">setRequestMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">setDoOutput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">setDoInput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span>,
				<span style="color: #0000ff;">&quot;application/x-www-form-urlencoded&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Send Request Parameters</span>
		printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sending Request Parameters...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">OutputStream</span> out <span style="color: #339933;">=</span> conn.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>reqParms.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		out.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Response Code=&quot;</span> <span style="color: #339933;">+</span> conn.<span style="color: #006633;">getResponseCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Get Response...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">BufferedReader</span> rd <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>conn
				.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> line<span style="color: #339933;">;</span>
		<span style="color: #003399;">StringBuffer</span> outputData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> rd.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Process line...</span>
			outputData.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			printLog<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		out.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		rd.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> genSig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Sort Alphabetically</span>
			<span style="color: #003399;">Arrays</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>apiReqParms, <span style="color: #003399;">String</span>.<span style="color: #006633;">CASE_INSENSITIVE_ORDER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span> reqParmsStr <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Concate</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> apiReqParms.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				reqParmsStr <span style="color: #339933;">+=</span> apiReqParms<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			reqParmsStr <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">SECRET</span><span style="color: #339933;">;</span>
			printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sig Input=&quot;</span> <span style="color: #339933;">+</span> reqParmsStr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// Get MD5 Hash</span>
			<span style="color: #003399;">MessageDigest</span> md <span style="color: #339933;">=</span> <span style="color: #003399;">MessageDigest</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MD5&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			md.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>reqParmsStr.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">byte</span> keyB<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> md.<span style="color: #006633;">digest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Key=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>keyB<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Key-UTF=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>keyB<span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> getHexCode<span style="color: #009900;">&#40;</span>keyB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			printLog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in Generating Signature: &quot;</span> <span style="color: #339933;">+</span> ex.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getHexCode<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> byteA<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">byte</span> dig <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">char</span> hexCode<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">'0'</span>, <span style="color: #0000ff;">'1'</span>, <span style="color: #0000ff;">'2'</span>, <span style="color: #0000ff;">'3'</span>, <span style="color: #0000ff;">'4'</span>, <span style="color: #0000ff;">'5'</span>, <span style="color: #0000ff;">'6'</span>, <span style="color: #0000ff;">'7'</span>, <span style="color: #0000ff;">'8'</span>, <span style="color: #0000ff;">'9'</span>,
				<span style="color: #0000ff;">'a'</span>, <span style="color: #0000ff;">'b'</span>, <span style="color: #0000ff;">'c'</span>, <span style="color: #0000ff;">'d'</span>, <span style="color: #0000ff;">'e'</span>, <span style="color: #0000ff;">'f'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Capital doesn't work</span>
		<span style="color: #003399;">String</span> hexVal <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">byte</span> msn, lsn<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> byteA.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			dig <span style="color: #339933;">=</span> byteA<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			msn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>dig <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> 0x0F<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Most Significant Nibble</span>
			lsn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>dig <span style="color: #339933;">&amp;</span> 0x0F<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Least Significant Nibble</span>
			hexVal <span style="color: #339933;">=</span> hexVal <span style="color: #339933;">+</span> hexCode<span style="color: #009900;">&#91;</span>msn<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> hexCode<span style="color: #009900;">&#91;</span>lsn<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> hexVal<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> printLog<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">/*
		 * -------------------------------------------- Auth.createToken
		 * fb.addAPIParm(&quot;api_key&quot;, fb.API_KEY); fb.addAPIParm(&quot;method&quot;,
		 * &quot;auth.createToken&quot;); fb.addAPIParm(&quot;v&quot;, &quot;1.0&quot;);
		 * --------------------------------------------
		 */</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			FBAgent fb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FBAgent<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fb.<span style="color: #006633;">setAPISecret</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SECRET_KEY_GOES_HERE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fb.<span style="color: #006633;">addAPIParm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;api_key&quot;</span>, <span style="color: #0000ff;">&quot;API_KEY_GOES_HERE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// Get auth_token from http://www.facebook.com/code_gen.php?v=1.0&amp;api_key=API_KEY</span>
			fb.<span style="color: #006633;">addAPIParm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;auth_token&quot;</span>, <span style="color: #0000ff;">&quot;AUTH_TOKEN_HERE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fb.<span style="color: #006633;">addAPIParm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;method&quot;</span>, <span style="color: #0000ff;">&quot;auth.getSession&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fb.<span style="color: #006633;">addAPIParm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;v&quot;</span>, <span style="color: #0000ff;">&quot;1.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fb.<span style="color: #006633;">sendHTTPRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error:&quot;</span> <span style="color: #339933;">+</span> ex.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now take your API_KEY, SECRET and AUTH (infinite session key) and put them into the below Processing code.  Also change the Serial.list()[2] line to be your serial port.  On mac, you need to install the usb to serial driver included with the Arduino IDE folder.</p>
<h2>Processing code</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">processing.serial.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.code.facebookapi.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.*</span><span style="color: #339933;">;</span>
&nbsp;
FacebookJsonRestClient facebook<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> interval <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// retrieve every 10 seconds</span>
<span style="color: #000066; font-weight: bold;">int</span> lastTime<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 3 Facebook IDs to watch</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> ids <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>  <span style="color: #cc66cc;">515470367</span>,  <span style="color: #666666; font-style: italic;">// patton oswalt</span>
               <span style="color: #cc66cc;">26670531942</span>,  <span style="color: #666666; font-style: italic;">// mccoy tyner</span>
              <span style="color: #cc66cc;">508305963</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// hugh mcleod</span>
&nbsp;
Serial port<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Customize setup to fit your environment</span>
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  frameRate<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003399;">String</span> apiKey <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CHANGETHIS&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// your api key</span>
  <span style="color: #003399;">String</span> secretKey <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CHANGETHIS&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// your secret</span>
  <span style="color: #003399;">String</span> sessionKey <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CHANGETHIS&quot;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// infinite session key goes here</span>
  facebook <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FacebookJsonRestClient<span style="color: #009900;">&#40;</span>apiKey,secretKey,sessionKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// </span>
  <span style="color: #003399;">String</span> arduinoPort <span style="color: #339933;">=</span> Serial.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  port <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Serial<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, arduinoPort, <span style="color: #cc66cc;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// connect to Arduino</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #000066; font-weight: bold;">int</span> n <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>interval <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>millis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>lastTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">int</span> uid <span style="color: #339933;">=</span> ids<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">String</span> userStatus <span style="color: #339933;">=</span> fetchData<span style="color: #009900;">&#40;</span>uid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* light up arduino based on facebook status
         facebook API says: 
         online_presence string	 The user's Facebook Chat status. Returns a string, 
         one of active, idle, offline, or error (when Facebook can't determine presence
         information on the server side). The query does not return the user's Facebook
         Chat status when that information is restricted for privacy reasons.
         */</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userStatus.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        sendSerial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;FF&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userStatus.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;idle&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        sendSerial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userStatus.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        sendSerial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;00&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userStatus.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;offline&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        sendSerial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;00&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userStatus.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        sendSerial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#&quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;00&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    lastTime <span style="color: #339933;">=</span> millis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003399;">String</span> fetchData<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> uid<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT name FROM user WHERE uid=&quot;</span> <span style="color: #339933;">+</span> uid<span style="color: #339933;">;</span>
        JSONArray resultArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JSONArray<span style="color: #009900;">&#41;</span>facebook.<span style="color: #006633;">fql_query</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        JSONObject result <span style="color: #339933;">=</span> resultArray.<span style="color: #006633;">getJSONObject</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        print<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Querying online status of &quot;</span> <span style="color: #339933;">+</span> result.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT online_presence FROM user WHERE uid=&quot;</span> <span style="color: #339933;">+</span> uid<span style="color: #339933;">;</span>
        resultArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JSONArray<span style="color: #009900;">&#41;</span>facebook.<span style="color: #006633;">fql_query</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> resultArray.<span style="color: #006633;">getJSONObject</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> userStatus <span style="color: #339933;">=</span> result.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;online_presence&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        println<span style="color: #009900;">&#40;</span>userStatus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> userStatus<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> jex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  jex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> sendSerial<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  port.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Save this sketch but don&#8217;t run it yet.  We&#8217;re missing some libraries.</p>
<p>Libraries for the facebook java lib need to go under: ~/Documents/Processing/libraries/facebook/library.  I had the following jars in that folder.  The json jar<br />
<code><br />
activation-1.1.jar			jaxb-api-2.1.jar<br />
commons-logging-1.1.1.jar		jaxb-impl-2.1.3.jar<br />
facebook-java-api-schema-2.0.4.jar	json-20070829.jar<br />
facebook.jar				stax-api-1.0-2.jar<br />
</code></p>
<p>All of these jars should come from the Google Code project I liked at the beginning of this post.  After all these things are ready, get the arduino running and then run your Processing sketch.</p>
<p><a href="http://squarism.com/2009/02/17/facebook-arduino-lamp/facebook-arduino-processing-client/"><img src="http://squarism.com/wp-content/uploads/2009/02/facebook_arduino_processing_client.png" alt="facebook arduino processing client" title="facebook arduino processing client" width="210" height="232" class="alignnone size-medium wp-image-1300" /></a><br />
It should pop up the default Processing window.  There&#8217;s nothing sexy about this app.  It has no GUI and writes simple status lines to the console.  It will run until this window is closed.  Every 10 seconds it should update the LEDs with the status from Facebook.  I&#8217;ve found that the status isn&#8217;t always real-time.  But it&#8217;s close enough for our purposes.  You can change the IDs to monitor in the Processing code.  The IDs can be found on Facebook by looking at someone&#8217;s profile.  For example, Patton Oswalt&#8217;s profile page is http://www.facebook.com/profile.php?id=515470367.  His ID is 515470367.</p>
<h4>Conclusion</h4>
<p>It was a fun little project.  I&#8217;m glad I got it working in my time off from work.  It took me about 6 hours straight and I was really in the zone at the end (1:15am).  The facebook bits took me almost 4 hours just because of the lack of documentation about that freaking infinite session key.  There are a lot of examples on the web on Java in a web container (or PHP) but very few desktop app examples.</p>
<p>Please let me know if you found this interesting, attempted it, ran into problems, have suggestions for improvements or anything else by leaving a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/02/17/facebook-arduino-lamp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

