At this year’s API Strategy & Practice Conference in Chicago, I had the pleasure of attending Matt Haines’s awesome talk about the “Internet of Things” and the Electric Imp. The Electric Imp is an awesome computing platform that combines a microprocessor and WiFi radio with an embedded OS and a Cloud platform for building and running code both locally and on their servers.
Even better, Matt handed out developer kits after his session. I took that as a personal challenge. After a quick run to a nearby RadioShack for supplies, a plan emerged.
I wanted to build something real-time, and it of course needed to be based of Chicago. I dug around a bit, and found this awesome dataset of real-time traffic congestion around the city. Using the aggregation functions of SoQL, I averaged the traffic at all of the measured intersections to create “average speed” across the whole city:
First, the hardware. In about 45 minutes, using my teeth for wire strippers, I had a basic circuit on my brand new breadboard that connected my giant red and green dome LEDs to pins 1 and 5 on the Imp. They each actually contain six separate LEDs, of which I connected three in parallel through a 330-ohm resistor. A circuit diagram is below, and I apologize if you’re an electrical engineer…
And here it is in copper and silicon:
Yes, I used a conference sticker instead of solder. I was a bit short on resources.
The software is broken into two parts. The first part, the device
code, runs on the microprocessor itself, and controls the pins that light up our LEDs. It has one simple job - listen for commands from the agent
code, and respond by turning the pins on and off.
Pretty simple, eh? All it does is initialize our output pins (green
and red
) and set up event handlers to handle the appropriate events coming from the agent. The agent then runs on the Electric Imp hosts, and thus it can interact with the rest of the web. Our agent code is more complicated, so we’ll break it up in to different parts to describe what it does.
First we start off by simply turning off both LEDs to reset our state:
Next, we define two functions. We’ll be making asynchronous HTTP requests, so we’ll define both a check_traffic()
function to kick off the request, and a handle_response()
function to be executed asynchronously when the HTTP request returns. Let’s first look at the check_traffic()
function:
In this function, we:
handle_response
as the handlerWhen that HTTP response returns,handle_response()
is called with a response object as a parameter. In the handler:
200 OK
Finally, we call check_traffic()
the first time to kick off our loop:
That’s it! The Imp can operate completely disconnected from your computer, all it needs is power and a WiFi connection, so you could easily place this by your front door to make sure you’re not surprised by a traffic jam when you head out on the town.