Countly, Raspberry Pi, and Servos, Oh My!


TL;DR - I got a Raspberry Pi to do cool things when people use my iOS app. Watch the video below.

I can't even remember where I heard about Countly, but I signed up for my beta account with them on Oct 10, 2012, and probably shipped my app using their service shortly after that. One particularly cool feature (this isn't unique to Countly) is that I can track "events" that occur in my app. As I was developing the free version of Easy Grade, I wanted to know how many times users were prompted to buy, but did not buy, the In-App Purchase. So I made an event for this (as well as for how many ads are shown and how many times people actually do buy the In-App Purchase), and began obsessively watching that dashboard once the app was available in the App Store.

EasyGradeFree Dahsboard

As I stared at the dashboard for longer than I care to admit, I looked at my Arduino and thought it'd be cool to make fun things happen, such as raising a flag or flashing an LED, in response to these events that I had defined. I did a quick search and came across a post about ZipWhip, which raises a flag with an Arduino every time they get a new customer ( I wrote some code for the Arduino that pulls from Countly's servers and parses the resulting JSON file, but it proved to be quite a challenge. Maybe I'm just too much of a novice at writing code in Processing (Arduino's language), but it just seemed like more work than it was worth.

Then a coworker got a Raspberry Pi, and I SSH'ed into it (with his permission, of course) and wrote a quick and dirty python script in about 15 minutes that did basically what I wanted, and I was sold. It's amazing how powerful a little tiny Ubuntu server can be! Oh, and Python makes it SUPER easy to parse JSON data. It was even pretty simple to get my servo spinning with the script, and after a bit of tweaking, I finally have something that is presentable. I've posted the code to GitHub here. It's still rough, so be kind! (and please offer suggestions for improvement)

Right now, my code is pretty specific in its use case. I don't have any immediate plans to make it more generic, but who knows what will happen. For now, though, it pulls the count for specific Events that I have defined in Countly, and takes certain actions based on what events are incremented:

Event Name: bannerViewDidLoadAd
iAds are successfully loaded - currently no action, since this number pretty much is always increasing

Event Name: clickedTheIAPCancelButton
Someone is presented with my In-App Purchase dialog and declines it - the servo spins for a time that is relative to the number of times this Event happens

Event Name: transactionComplete
Someone actually purchases the In-App Purchase - the servo spins, like before, but the Raspberry Pi also plays a "chaching" sound file through the audio output.

Here's a video of it actually working. Here it turns the flag and plays a sound, indicating that someone bought my In-App-Purchase: