Chuck Norris and the Internet of Things

In my last post I covered my experiences setting up the Intel Edison board and interfacing with the LCD and temperature sensor, but ran into a #fail when trying to make a REST call on site at the Intel IoT Roadshow in Somerville.

The issue was certainly network related and site-specific – a redirection to what would probably have been a network credentials dialog – but it wasn’t happening to everyone.  My resolution then was to call it a day and give it a shot at home… where it just worked.

Chuck NorrisIt occurred to me though that getting the current temperature from a web service was booooring, so rather than show you that, I opted to build the first ever (?) Chuck Norris IoT device!

ICNdb

These days there’s an API for everything, and who’d dare deny Mr. Norris his spot in the cloud! Yes indeed, there is an API targeting – what else – the Internet Chuck Norris Database. It’s a simple RESTful API that requires no registration or API keys making it quite easy to access. For instance, in your browser just navigate to

http://api.icndb.com/jokes/random?exclude=[explicit]

and you should get back a bit of JSON that includes a short joke. Feel free to leave off the query parameter if you’re ok with potentially NSFW material being returned.

Invoking the API in Node.js

Node junkies know this stuff like the back of their hand, but I had to reacclimate myself to the purely async nature of the execution flow. Basically, you provide the URL you want to fetch and then a callback routine that processes once the request has completed.. like:

  • linifyJoke takes the joke string and divides it into an array of strings that are no more than 16 characters each to facilitate display on the LCD.
  • outputLines takes that array and the current line to be output and successively writes the lines to the LCD with a delay so you can read through the joke.

There’s nothing magical about that code (and I covered writing to the LCD in my last post), but if you want to view all the gory detail, I’ve included the entire script in this gist.

On-demand Joke

Once you’ve heard one Chuck Norris joke, you’ll find you can’t get enough of them, so I realized I need to beef up my implementation to allow you to request joke after joke.

Enter the button sensor from the Grove Starter Kit. The button returns a high voltage (1) when pressed and low (0) when released, so getting the button state is a simple read from the GPIO context that’s exposed by the mraa interface.

You’ll need to continuously poll for the press though, and when pressed carried out the desired action – perhaps ignoring, as I do, subsequent presses until the current request is complete.

Here, processingRequest is a sentinel value that prevents reentry to showJoke, with the outputLines method mentioned earlier having the responsibility for resetting the flag once the current joke has finished displaying.

This is not Vaporware!

My IoT Weekend – Part 1

What better way to spend Super Pi Day than with 100+ technology geeks at a Hackathon… and that’s just how I started out this past weekend. Intel is hosting an IoT Roadshow at a number of cities across the world, and they were in Somerville this weekend at Greentown Labs for the Boston edition. It was a full house with many – me included – drawn by the free Edison board and Grove-Starter Kit offered to the first 100 attendees in line.

It's mine, ALL MINE!!!

I arrived at just past 8 a.m. and was about 50th in line… a line that didn’t start moving until they opened the space at 9. The photo below was taken about 1 p.m., at which time everyone had boards out, and table space was at a premium.

I’ve noted before a major irony of the wireless (and now IoT) age is that you need more room and more wires to get everything up and running or charged.  The Edison board, for instance, needs two USB cables and a power cord when you’re flashing the firmware.

Full house at Greentown Labs

This event was a hackathon, with cash prizes of up to $1500 being offered, but I knew I wouldn’t have the entire weekend to dedicate. Nor did I have any really inspirational ideas, and none of the few pitches I heard was overwhelmingly compelling to me. For my first foray into this hardware hacking, I was kind of itching to go it alone anyway, and luckily I had a duo of experienced Arduino makers next to me to get me over the few bumps in the road I encountered.

Setting up the board was a great deal simpler than I expected, and Intel has assembled a pretty good set of tutorials and references on their Google docs site to follow. For Windows users, it turns out there’s a new installer that actually makes it even easier to flash the board, set up the drivers on your laptop, etc. – a fact that wasn’t clear until I was 75% through the manual process.

That said, the 90 minutes it took for me to work through the manual process gave me a much better understanding of how it all works together, from flashing the board, to using PuTTY and SSH to connect, to setting up ethernet over USB, to connecting the board to the XDK development environment (which, by the way is pretty slick, and has progressed nicely since I used it over a year ago for cross-platform mobile development demos).

Blinky Lights

XDK comes with a number of template applications for the board, so I started with the on-board LED Blink app,

XDK Templates

which consists of the following bit of Node.js code (yes, it’s JavaScript, Python, or C++ if you’re talking to the Edison):

Blinky Light Code

The mraa reference may look a tad arcane: it’s simply a C++ library with bindings to JavaScript (and Python) that enable low level communications with the board components. Upm is another interface, built on mraa, that provides a higher level interface, with methods and properties that are more specific to the type of sensor. Upm is what’s used in most (if not all) of the other samples on Intel’s Google Docs site).

To get from code to execution, it’s a simple matter of building and uploading the project to the board, which XDK makes pretty easy via its UI (below).

XDK Deployment

The end result of the app isn’t exactly photoworthy, so I’m sparing the bandwidth. Suffice it to say, ahem, the light bulb finally went on (and off… and on… and off).

Using the Temperature Sensor and LCD

The Grove starter kit comes with about a dozen sensors, so I thought I’d kick things up a notch by connecting the (analog) temperature sensor and displaying the current temperature on the LCD (I2C) sensor.

Not much code needed here:

to produce :

Temperature Sensor w/LCD

Adding Internet to my Things

My next step was to use one of the freely available web APIs out there to get the current temperature outside, compare it to the temperature inside, and then display the difference on the LCD.  There are host of APIs and API curators out there (Mashery, Programmable Web, Apigee, etc.), but the free one that caught my eye was WeatherUnderground.  You make a simple REST call and you get back some JSON with a bevvy of information about the current conditions at your location (specified via city name, id or lat-long). Yeah, you do need to sign up for a free account, but that’s pretty much par for the course these days.

The code to make a GET call in Node.js is pretty simple if you use the Request package… or so I thought!  My Node skills are admittedly rusty, but when the “Hello World” example wasn’t working, I knew it wasn’t me, and in fact there seemed to be a network issue that affected some -but not all – of the attendees. Every request being made from my board was getting redirected to what appears would have been a splash screen looking for network credentials, so while my HTTP requests were returning a success code, they weren’t really hitting the intended service.

After about an hour of back and forth with the awesome Intel guys on site, reflashing the board, and waving dead chickens, I decided to call it a day and hope for better connectivity at home.