Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Chuck Norris and the Internet of Things

DZone's Guide to

Chuck Norris and the Internet of Things

· IoT Zone
Free Resource

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 – theInternet 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 outputLinesmethod mentioned earlier having the responsibility for resetting the flag once the current joke has finished displaying.

http://codocent.com/wp-content/uploads/2015/03/WP_20150316_20_03_40_Pro1.mp4?_=1

This is not Vaporware!

Topics:

Published at DZone with permission of Jim O' Neil, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}