Let’s honor the future

January 9 2015

0 comments 0

This post is about fulfilling some expectations created by sci-fi movies, using Arduino and Nobel to expose RESTFul APIs for controlling physical devices.

The early-arriving future disappointment

This is the year that, according to some Hollywood prophets, cars will be finally flying, clothes will adjust their sizes to our bodies, trash will be combustible, and houses will be intelligent – though I never understood why they thought that an intelligent house would have a fax hanging on the wall. Come on? FAXES!

The truth is that there are thousands (maybe even millions) of people disappointed about this. And how could I blame them? The expectations were set, but how many of these things have you actually seen? Does your skateboard fly? Have you ever been threatened with destroying the space-time continuum?

The geek and DIY responsibility

Well, to be fair, intelligent houses are popping up. They might not be mainstream yet – and still lacking for standards connecting devices from different vendors – but we have all the pieces we need to make intelligent houses happen.

So, I started working on an ambitious and still very home made project: To prototype some little, cheap gadgets that could help me control the devices that I already own. Yes, that’s important: having an intelligent house shouldn’t be expensive, since we don’t do it because we need it, we don’t do it because we want to, we do it because we can.

So, before showing how you could start seriously controlling some devices, let me introduce you to Arduino:

You probably already know it, but just in case, “Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects.

In other words, Arduino is a micro-controller capable of running programs written in C and interacting with the physical world by using inputs and outputs.

You can connect different gadgets to your Arduino board (sensors, lights, servos, engines, etc), and you can also extend its functions by using “shields”. This last detail is a key factor – thanks to Ethernet or the WIFI shields, you can connect the board directly to a network (even internet).

Turn the lights on (Arduino’s “Hello World”)

Let’s start with the most basic program you can write and deploy on an Arduino board, and add on to it until it’s an Internet of Things project. Remember: Arduino is mostly for prototyping and not for building real devices or products.


The sketch above shows the “output 13” connected to a 220 Ohm resistor and then a led that will end in the “ground” connector. This basic circuit, will make possible that the led turns on and off once we write a program intended to do so.

The following code will make the led blink:

Remotely controlling the led

Ok, now things might get a little bit trickier. We just need to expose an API to control our led. Let’s try to make it style. To do this, we need to:

  • Develop a lightweight web server that can run on an Arduino board (some Arduino boards are extremely limited in regards to memory and processor)
  • Code a web application that routes the http requests to some handlers
  • Fill the handlers with some code that turns on and off the lights.

Yes, it seems to be trickier, and you might be remembering Doc. Brown: “I’m sure that in 1985, plutonium is available in every corner drugstore, but in 1955, it’s a little hard to come by“.

Well, this is actually much simpler, and luckily, already resolved. You can just define your API with RAML and ask for the Arduino code to be automatically generated.

Done! Nobel will do it for you (actually, it’ll finish coding the empty handlers, and you’ll need to complete these with the logic you want).

Step by Step

Assuming you already have the Arduino software on your computer and you know how to deploy your programs…

  • Acquire an Ethernet or WiFi Shield for your Arduino board (Nobel won’t do that for you)
  • Mount the shield properly (it’s just making the inputs and outputs match)
  • Build the circuit on top of the shield (remember, shields extend your board functionalities while allowing you to preserve the original ones)
  • Install Nobel on your computer: https://github.com/nohorbee/nobel#installation
  • Write the RAML for your API (remember, it’s just turning on and off a light):
  • Save your RAML file and scaffold your application:

    or

    if you need Nobel to install the required libraries for you (only the first time)

 

 

Your Arduino board is now literally exposing a REST API capable of handling the states of your led.

Build your own projects

Now go for it! Build your intelligent house – DIY style! And let’s not limit ourselves to nice menus for turning lights on. Here are some ideas (I’m working on some of these already):

  • Build a mobile app that uses your GPS to detect when you are going back home and turns your air conditioner on when you are only 15 blocks away.
  • Build a pet feeder that you can control from a web application.
  • Would you dare to warm up the car from your couch?
  • Build your own remote controlled robot

Have fun and Happy Hacking!

 


We'd love to hear your opinion on this post