Blog

Ship IoT with TI Beaglebone Black and bip.io

July 1, 2015 / Posted By: wotio team

At wot.io, we're proud to be able to accelerate IoT solutions with our data service exchange™. 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.

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.

What you'll need

  • A beaglebone black
  • An internet connection for it
  • A breadboard
  • A pushbutton
  • A 1kOhm Resistor
  • Some wires

Step 1 - The hardware

Let's start by wiring up the button on the breadboard. The image below shows the circuit layout.

Full SetupBeaglebone CloseupBreadboard Closeup

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!

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.

If you'd like to know know what all of the pins on the board do, check out http://stuffwemade.net/hwio/beaglebone-pin-reference/.

Step 2 - Connecting to Ship IOT Lite

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 twitter.com and sign up if you don't have one. Then you can go to shipiot.net and follow the instructions in the video below.

And you're done! You can test the endpoint by entering your username, API token, and bip name into the following URL and using

$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."}  

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.

Step 3 - The software

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.

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 getting started 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.

First, we're going to install the required libraries. To do this we'll use pip, Python's package manager. But first, we'll need to install it.

On Angstrom (the default Linux distro, if you haven't changed it), run:

opkg update && opkg install python-pip python-setuptools python-smbus  

On Debian, run:

sudo apt-get update  
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus libffi-dev libssl-dev -y  

Now, on either distro, install the dependencies that we will be using in the 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  

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.

Create a new file called wotbutton and paste the following code into it:

#!/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)

Now, at the command prompt, type:

./wotbutton

and, after a few second of loading libraries and initializing inputs, you should get the prompt Waiting for button press.... Now press the button, and check out your tweet!