I built an arduino project that monitor’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’t tell you who is who because there’s no text display. I suppose you could make little stickers. In the end, it’s a simple little project that provides a benefit of being able to have a small secondary display of who’s on and not. Is the little device a keeper? Would people want it? Probably not. This was a test.
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’s API. So this is an article about how I got it working but doesn’t mean that it’s going to work for you. Things change online and at best this might be a starting point in how to “get your arduino online”. When things change online, this article can get dated.
And about that online bit. The arduino is just a simple microprocessor. I found that a lot of fancy Java features won’t run on it. Strings, System.arraycopy and many network Java packages just aren’t supported. So you’re going to need a laptop or computer hooked up to the arduino to make this work. The arduino doesn’t have a direct network adapter on it, a network stack or a lot of RAM. It’s job is to do the simple stuff like blink LEDs. So I found it’s best to keep the arduino code simple.
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:
`
#0FF = 1st LED full brightness, 1st friend is online
#110 = 2nd LED dimly lit, 2nd friend is idle
#000 = 1st LED off, 1st friend went offline
#2FF = 3rd LED full brightness, 3rd friend came online
`
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.
Dependencies
There are a ton of dependencies that you’re going to need.
Facebook account
A dummy app on facebook, instructions below.
An infinite session key (also called offline access). This allows you to create a desktop app that doesn’t have to be in a web container (nor prompt for a facebook login).
An arduino and some simple electronics components. I ordered a starter kit from Amazon for $40.
A facebook java API from google code. It has some dependancies that should be covered by the below steps.
Wiring the arduino
If you mess with the arduino a lot, it shouldn’t be a problem to wire 3 LEDs. But I’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 - symbol at the top left of the above diagram). I didn’t use the common + on the breadboard because each LED needs to light up independently.
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’s pretty messy in there.
Arduino code
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’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’re good to go.
Now comes the Facebook stuff. This part takes the longest. Sign into facebook (of course you have an account) and go to http://developer.facebook.com/. Create a new app, just follow the wizard steps (you can name it anything). Then write down your API_KEY and SECRET_KEY. They’ll be used quite a bit below.
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.
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.
Infinite session key code
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.
Processing code
Save this sketch but don’t run it yet. We’re missing some libraries.
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
`
activation-1.1.jar jaxb-api-2.1.jar
commons-logging-1.1.1.jar jaxb-impl-2.1.3.jar
facebook-java-api-schema-2.0.4.jar json-20070829.jar
facebook.jar stax-api-1.0-2.jar
`
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.
It should pop up the default Processing window. There’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’ve found that the status isn’t always real-time. But it’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’s profile. For example, Patton Oswalt’s profile page is http://www.facebook.com/profile.php?id=515470367. His ID is 515470367.
Conclusion
It was a fun little project. I’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.
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.