Internet of Things – Like Blink

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.

This is he code for the omnichannel service…

…and first it saves the Facebook user name parameter (on line 2). Then it gets the fan-page contents from the Facebook Graph API for the user name specified (on line 3). The contents is decoded to JSON (on line 4) and if there is a “likes” node, it is retrieved (on line 5), and returned to the caller (on line 6). 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”.

Here is the Arduino code (sketch)…

…and after the initial includes, a constant for pin 13 is defined (on line 4). That pin is set up for output (on line 8), and then we initialize the bridge (on line 9), which allows communication between the microcontroller (32U4) and the microprocessor (AR9331, with Linux). After setting up a few variables (on line 12-16), the eternal loop starts by getting the content from the omnichannel service (on lines 20-25, and replace 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 (on line 26), the difference since the last request is calculated (on line 27), and if the difference is positive, the LED is turned on for half a second (on line 30-32). Then we wait for two seconds (on line 35), save the recently retrieved number of likes (on line 36), and reset the server contents (on line 37). This way, each time that Avicii gets a like on Facebook, the LED blinks.

So there you have a more interesting example of how to apply the Internet of Things with omnichannel services.