Blog

Ship IoT with Electric Imp and bip.io

August 3, 2015 / Posted By: wotio team

When we got our hands on a couple of Electric Imps here in the office, we set about to see how we could use their ARM Cortex processors and WiFi capabilities. Within minutes of plugging the Imps in and loading up some example code in their cloud-based IDE, we had our first successful internet<->device communication going. Cool! That was simple enough, so onto something slightly more interesting:

In the software world, every example starts with printing 'Hello World'. Well in the device world the equivalent is the famed blinking LED. So for that we'll need:

  • an Electric IMP
  • a 330Ω resistor
  • a breadboard
  • some jumper wires
  • an LED, of course.

We're using an IMP card and the standard April Breakout Board for our little project. You can purchase one as well through that link. Wiring up the board as shown here
and again grabbing some example code that we dropped in the IMP cloud IDE, we were able to then push the code to the device. Sure enough, our LED began to blink!
Manually configuring the LED to loop on and off every 5 seconds is neat 'n all. What would be more interesting, though, is to have our Imp respond to events sent over the internet. So, onto setting up our agent code to handle incoming web requests:

function requestHandler(request, response) {  
    reqBody <- http.jsondecode(request.body); 
    device.send("led", reqBody["led"]);

    // send a response back to whoever made the request
    response.send(200, "OK");
}

http.onrequest(requestHandler);  

and our device code *:

led <- hardware.pin9;  
led.configure(DIGITAL_OUT);

agent.on("led", function (value) {  
  if (value == 0) led.write(0); // if 0 was passed in, turn led off
  else led.write(1);            
});
  • note that Imp code is written in Squirrel. You can also learn more about the Squirrel language here

now we can send an example request: curl -X GET -d '{"led":1}' https://agent.electricimp.com/XJOaOiPDb7UA

That's it! Now whenever we send a web request with {"led": 0 | 1} as the body of the message, we can send voltage through to pin9 and control the state of our LED (on or off), over the internet!
Pretty cool.

We'll leave securing the control of our device to some application logic that you can write into the agent code, which for anything more than a blinking LED you'll absolutely want to do.

With the Electric Imp we're essentially demonstrating that it is now possible with just a few lines of code to remotely manage and control a physical thing that you've deployed out in the field somewhere. We've attached a simple red LED to one of the GPIO pins, but of course you can start to imagine attaching anything to the Imp, and have that thing now be a real 'connected device'.

Tails

One other cool offering from Electric Imp is their Tails extensions which make it super-easy to start reading environmental data. Ours has sensors to read temperature, humidity, ambient light, and even the barometric pressure. They're designed to work with the April dev board, so we grabbed one and connected it up as well.
Some quick changes to the agent code:

function HttpGetWrapper (url, headers) {  
  local request = http.get(url, headers);
  local response = request.sendsync();
  return response;
}

function postReading(reading) {  
    headers <- {}   
    data <- reading["temp"] + "," + reading["humidity"] + "," + reading["light"] + "," + reading["pressure"] + "," + reading["timestamp"];
    url <- "http://teamiot.api.shipiot.net/bip/http/electricimp" + "?body=" + data;

    HttpGetWrapper(url, headers);
}

// Register the function to handle data messages from the device
device.on("reading", postReading);  
<device code omitted for brevity>  

And with that we are setup to handle reading sensor data and sending it over the internet. But to where?

Ship IoT

The url line from above is where we want to point our data to, e.g.

url <- "http://teamiot.api.shipiot.net/bip/http/electricimp" + "?body=" + data;

which is an incoming web-hook URL we setup in bip.io to receive our sensor data into the cloud. Once we have the url setup in bip.io, we can start to realize the value of that data by connecting easily to a range of web-services.

Let's setup that URL:

  1. Create an account on bip.io through Shipiot.net
  2. Set up our bip. The following video shows us how:
    3. There is no step three! We're done.

Open up the sheet in Google Drive and you'll see we that our Imp is sending its four (4) sensor readings (temperature, humidity, ambient light, and barometric pressure) right into our bip.io 'bip' which automatically handles sending and recording the data to a Google Spreadsheet.

There are dozens of different services you could also connect your device data to from within bip.io- it all depends on what use case makes sense for you.

wot.io + Electric Imp =

With the recent release of Electric Imp's BuildAPI we were also able to set up an adapter to be able to securely command-n-control our entire collection of Imps from within the wot.io Operating Environment (wot.io OE), including:

  • Send commands to activate and control a particular device. e.g:

    ["send_data", < Agent Id >, "{\"led\":1}"]

  • Send commands to read data off of any specific Imp (or read data from all of them).
  • List all of the devices - and query their current state - registered under each account.
  • Review the code running on some (or all) of the Imps.
  • Remotely update the code running on some (or all) of the Imps.
  • Remotely restart some (or all) of the Imps.
  • View the logs of every device
  • and more...

which, when connected to the range of data services offered through the wot.io Data Services Exchange really starts to unlock the potential value of amassing connected-device data on an industrial scale.