Blog

Ship IoT with Electric Imp and bip.io

Aug 2015/ Posted By: wotio team

<p>When we got our hands on a couple of <a href="https://electricimp.com">Electric Imps</a> 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&lt;-&gt;device communication going. Cool! That was simple enough, so onto something <em>slightly</em> more interesting:</p>
<p>In the software world, every example starts with printing 'Hello World'. Well in the device world the equivalent is the famed <em>blinking LED</em>. So for that we'll need:</p>
<ul>
<li>an Electric IMP</li>
<li>a 330Ω resistor</li>
<li>a breadboard</li>
<li>some jumper wires</li>
<li>an LED, of course.</li>
</ul>
<p>We're using an IMP card and the standard <a href="https://electricimp.com/docs/gettingstarted/devkits/">April Breakout Board</a> for our little project. You can purchase one as well through that link. Wiring up the board as shown here <br /><img src="http://idfiles.leveelabs.com/55bd0288af0b0930ba599bd0c4b7ca38/resources/img_new/labs_wot_io/imp2.jpg" alt="" /> and again grabbing some <a href="https://github.com/electricimp/examples">example code</a> 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! <br /> 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 <em>respond</em> to events sent over the internet. So, onto setting up our <em>agent code</em> to handle incoming web requests:</p>
<pre><code>function requestHandler(request, response) {
reqBody &lt;- 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);
</code></pre>
<p>and our <em>device code</em> *:</p>
<pre><code>led &lt;- 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);
});
</code></pre>
<ul>
<li>note that Imp code is written in <a href="https://electricimp.com/docs/squirrel/">Squirrel</a>. You can also learn more about the Squirrel language <a href="http://squirrel-lang.org/">here</a></li>
</ul>
<p>now we can send an example request: <code>curl -X GET -d '{"led":1}' https://agent.electricimp.com/XJOaOiPDb7UA</code></p>
<p>That's it! Now whenever we send a web request with <code>{"led": 0 | 1}</code> as the <code>body</code> of the message, we can send voltage through to <code>pin9</code> and control the state of our LED (on or off), <em>over the internet!</em> <br /> Pretty cool.</p>
<p>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 <em>blinking LED</em> you'll absolutely want to do.</p>
<p>With the Electric Imp we're essentially demonstrating that it is now possible <em>with just a few lines of code</em> to remotely manage and control a physical <em>thing</em> 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 <em>anything</em> to the Imp, and have that <em>thing</em> now be a real 'connected device'.</p>
<h5 id="tails">Tails</h5>
<p>One other cool offering from Electric Imp is their <a href="https://electricimp.com/docs/tails/">Tails</a> 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. <br /><img src="http://idfiles.leveelabs.com/55bd0288af0b0930ba599bd0c4b7ca38/resources/img_new/labs_wot_io/imp3.jpg" alt="" /> Some quick changes to the agent code:</p>
<pre><code>function HttpGetWrapper (url, headers) {
local request = http.get(url, headers);
local response = request.sendsync();
return response;
}

function postReading(reading) {
headers &lt;- {}
data &lt;- reading["temp"] + "," + reading["humidity"] + "," + reading["light"] + "," + reading["pressure"] + "," + reading["timestamp"];
url &lt;- "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);
</code></pre>
<pre><code>&lt;device code omitted for brevity&gt;
</code></pre>
<p>And with that we are setup to handle reading sensor data and sending it over the internet. But to where?</p>
<h5 id="shipiot">Ship IoT</h5>
<p>The <strong>url</strong> line from above is where we want to point our data to, e.g.</p>
<p><code>url &lt;- "http://teamiot.api.shipiot.net/bip/http/electricimp" + "?body=" + data;</code></p>
<p>which is an incoming web-hook URL we setup in <a href="https://shipiot.net">bip.io</a> to <em>receive</em> our sensor data into the cloud. Once we have the url setup in bip.io, we can start to realize the <strong>value</strong> of that data by connecting easily to a range of web-services.</p>
<p>Let's setup that URL:</p>
<ol>
<li>Create an account on bip.io through Shipiot.net</li>
<li>Set up our bip. The following video shows us how: <br /><iframe src="https://www.youtube.com/embed/rZ9FdvF_6OQ" frameborder="0" width="560" height="315" allowfullscreen="allowfullscreen"></iframe> 3. There is no step three! We're done.</li>
</ol>
<p>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.</p>
<p><img src="http://idfiles.leveelabs.com/55bd0288af0b0930ba599bd0c4b7ca38/resources/img_new/labs_wot_io/Screen-Shot-2015-08-03-at-2-11-48-PM.png" alt="" /></p>
<p>There are <a href="http://wot.io/partners/">dozens of different services</a> you could also connect your device data to from within bip.io- it all depends on what use case makes sense for you.</p>
<h5 id="wotioelectricimp">wot.io + Electric Imp =</h5>
<p>With the recent release of Electric Imp's <a href="https://electricimp.com/docs/buildapi/">BuildAPI</a> 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:</p>
<ul>
<li>
<p>Send commands to activate and control a particular device. e.g:</p>
<p><code>["send_data", &lt; Agent Id &gt;, "{\"led\":1}"]</code></p>
</li>
<li>Send commands to read data off of any specific Imp (or read data from all of them).</li>
<li>List all of the devices - and query their current state - registered under each account.</li>
<li>Review the code running on some (or all) of the Imps.</li>
<li>Remotely update the code running on some (or all) of the Imps.</li>
<li>Remotely restart some (or all) of the Imps.</li>
<li>View the logs of every device</li>
<li>and more...</li>
</ul>
<p>which, when connected to the range of data services offered through the wot.io <em>Data Services Exchange</em> really starts to unlock the potential value of amassing connected-device data on an industrial scale.</p>