Blog

Ship IoT with TI Beaglebone Black and bip.io

Jul 2015/ Posted By: wotio team

<p>At wot.io, we're proud to be able to accelerate IoT solutions with our data service exchange&trade;. We've put together this Ship IoT tutorial to show you how you can use the Texas Instruments based Beaglebone Black board and its ARM Cortex-A8 processor with one of our data service providers: bip.io, giving you access to web API integration and automation with 60+ services.</p>
<p>This blog explains how to use the BeagleBone Black in a project that enables you to send a tweet with the press of a button! After implementing this, you should be ready to replace that button with any sensors you like, and to use bip.io to create interesting work flows with the many web services that it supports.</p>
<h2 id="whatyoullneed">What you'll need</h2>
<ul>
<li>A beaglebone black</li>
<li>An internet connection for it</li>
<li>A breadboard</li>
<li>A pushbutton</li>
<li>A 1kOhm Resistor</li>
<li>Some wires</li>
</ul>
<h2 id="step1thehardware">Step 1 - The hardware</h2>
<p>Let's start by wiring up the button on the breadboard. The image below shows the circuit layout.</p>
<p><img src="http://idfiles.leveelabs.com/55bd0288af0b0930ba599bd0c4b7ca38/resources/img_new/labs_wot_io/IMG_0617-1.JPG" alt="Full Setup" /><img src="http://idfiles.leveelabs.com/55bd0288af0b0930ba599bd0c4b7ca38/resources/img_new/labs_wot_io/IMG_0620-1.JPG" alt="Beaglebone Closeup" /><img src="http://idfiles.leveelabs.com/55bd0288af0b0930ba599bd0c4b7ca38/resources/img_new/labs_wot_io/IMG_0619-1.JPG" alt="Breadboard Closeup" /></p>
<p>The pins on the switch are spaced differently in each direction, so they will only fit in the correct orientation on the breadboard. An astute eye will also notice that a 10kOhm resistor was used in our test setup, as we didn't have any 1kOhm ones lying around!</p>
<p>The two connections on the P8 header are both GND (pins 2 and 12). The connection on the P9 header (pin 3) is 3.3V. What we're doing is pulling down the input pin (P8-12) so it stays at 0V until the button is pressed, at which point it goes up to 3.3V.</p>
<p>If you'd like to know know what all of the pins on the board do, check out <a href="http://stuffwemade.net/hwio/beaglebone-pin-reference/">http://stuffwemade.net/hwio/beaglebone-pin-reference/</a>.</p>
<h2 id="step2connectingtoshipiotlite">Step 2 - Connecting to Ship IOT Lite</h2>
<p>Now, before we get into writing any code, let's set up a Ship IOT Lite account, along with a bip that tweets. You'll need a twitter account to complete this step, so go to <a href="http://twitter.com">twitter.com</a> and sign up if you don't have one. Then you can go to <a href="https://shipiot.net">shipiot.net</a> and follow the instructions in the video below.</p>
<p><iframe src="https://www.youtube.com/embed/KKzD8xivxoQ" frameborder="0" width="560" height="315" allowfullscreen="allowfullscreen"></iframe></p>
<p>And you're done! You can test the endpoint by entering your username, API token, and bip name into the following URL and using</p>
<pre><code>$USERNAME='shipiot_username'
$APITOKEN='api_token'
$BIPNAME='bbbtweet'
curl https://$USERNAME:$APITOKEN@$USERNAME.shipiot.net/bip/http/$BIPNAME/ -H 'Content-Type: application/json' -d {"title":"BBB", "body": "Check it out - I just tweeted from ShipIOT Lite."}
</code></pre>
<p>Then, in a few seconds, you should see the message pop up in your twitter account! Note that twitter has a spam-prevention feature that prevents you from sending duplicate message, so if you want to test it multiple times, make sure you change the message body each time.</p>
<h2 id="step3thesoftware">Step 3 - The software</h2>
<p>For the device code, we're going to write a simple application in Python. If you don't know Python don't be afraid to give it a shot - it's a very straightforward and easy to read language, so even if you can't program in it you should still be able to understand what's going on.</p>
<p>Going through the basics of how to use the Beaglebone Black are out of the scope of this tutorial, but as long as you can SSH into it and it has an internet connection you are good to go. You can check out the <a href="http://beagleboard.org/getting-started">getting started</a> page for help with that. We will be listing the instructions for both Angstrom (the default BBB Linux distro) and Debian, which is a bit more full-featured.</p>
<p>First, we're going to install the required libraries. To do this we'll use <code>pip</code>, Python's package manager. But first, we'll need to install it.</p>
<p>On Angstrom (the default Linux distro, if you haven't changed it), run:</p>
<pre><code>opkg update &amp;&amp; opkg install python-pip python-setuptools python-smbus
</code></pre>
<p>On Debian, run:</p>
<pre><code>sudo apt-get update
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus libffi-dev libssl-dev -y
</code></pre>
<p>Now, on either distro, install the dependencies that we will be using in the code:</p>
<pre><code>pip install Adafruit_BBIO # A library from Adafruit that provides easy access to the board's pin data.
pip install requests # A library to make HTTP requests simpler
pip install requests[security] # Enables SSL (HTTPS) support
</code></pre>
<p>Now, the code. For now, let's just get it to run, and then we can go through it and investigate about how it works.</p>
<p>Create a new file called <code>wotbutton</code> and paste the following code into it:</p>
<pre><code>#!/usr/bin/env python

###############################
### SHIP IOT ACCOUNT DETAILS ###
###############################
shipiot_username = "wotdemo"
shipiot_token = "5139354cedaf7252c776ecf793452344"
shipiot_bip_name = "bbbdemo"
###############################

import Adafruit_BBIO.GPIO as gpio
from time import sleep
import json, requests, time, sys

def on_press():
##############################
###### THE SHIP IOT CALL ######
##############################
## This is the important part of the integration.
## It shows the single HTTP call required to send the event data
## to bip.io, which then acts upon it according to the `bip`
## that was created. Note that since we are using twitter in our
## demo, and twitter has an anti-spam feature,
############################
r = requests.post(
"https://%s.shipiot.net/bip/http/%s/" % (shipiot_username, shipiot_bip_name),
auth=(shipiot_username, shipiot_token),
data=json.dumps(
{"title": "BBB", "body": "Beaglebone Black Button Pressed!\n" + time.asctime(time.localtime(time.time()))}),
headers={"Content-Type": "application/json"}
)
############################
############################
if r.status_code != 200:
print "Ship IOT Lite connection failed. Please try again"
else:
print "event sent!"

# Prepare to read the state of pin 12 on header P8
gpio.setup("P8_12", gpio.IN)

notifyWaiting = True
oldState = 0
# Program loop
while True:
if notifyWaiting:
print "Waiting for button press..."
notifyWaiting = False
sleep(0.01) # Short delay in the infinite reduces CPU usage
if gpio.input("P8_12") == 1:
sys.stdout.write('Pressed button...')
notifyWaiting = True
on_press() # Calls Ship IOT Lite, as detailed above
while gpio.input("P8_12") == 1:
sleep(0.01)
</code></pre>
<p>Now, at the command prompt, type:</p>
<pre><code>./wotbutton
</code></pre>
<p>and, after a few second of loading libraries and initializing inputs, you should get the prompt <code>Waiting for button press...</code>. Now press the button, and check out your tweet!</p>