Today I will show you how to make the basic example of Internet of Things from my previous video (“Internet of Things — Getting Started”) more interesting by putting some Internet into the mix. This time, I will make the LED blink each time someone gets a like on Facebook.
All the hardware that you need to perform this lab is the same as in the previous video, so please check that out for the details. The difference this time is that I will add a multichannel service to get the number of likes from a Facebook fan-page, and the Arduino program (sketch) will be calling that service and blink the LED accordingly. So the flow is that someone clicks or taps a like button, and Facebook adds that to the fan page likes count, which makes it available to the Graph API. The Arduino queries the omnichannel service for the number of likes, which in turn query the Graph API for all of the fan page info, extract the likes count, and return it to the Arduino. The Arduino then compares the returned likes count with the previous count retrieved, and if the new count is higher, it flashes the LED for half a second.
The code for the omnichannel service (you find it on http://cforsberg.com/?p=159) first saves the Facebook user name parameter. Then it gets the fan-page contents from the Facebook Graph API for the user name specified. The contents is decoded to JSON and if there is a “likes” node, it is retrieved, and returned to the caller. This service can be deployed to a cloud server, with the name likes.php, and I showed how to do that in my previous video named “Omnichannel Services – First Service”.
The Arduino code (sketch) defines a constant for pin 13 is defined. That pin is set up for output, and then we initialize the bridge, which allows communication between the microcontroller (32U4) and the microprocessor (AR9331, with Linux). After setting up a few variables, the eternal loop starts by getting the content from the omnichannel service (and replace 0.0.0.0 with the IP of your server). The code now gets the likes for the Swedish DJ Avicii, but it can easily be changed to any other account with a fan page, even your own. The response is parsed as an integer with the number of likes, the difference since the last request is calculated, and if the difference is positive, the LED is turned on for half a second. Then we wait for two seconds, save the recently retrieved number of likes, and reset the server contents. This way, each time that Avicii gets a like on Facebook, the LED blinks.