Posts Tagged ‘parallax’

iPad to prop: bluetooth part 1

February 20, 2012

I recently started a quest to create an awesome (and universal) human interface for all my propeller projects (robots, home automation, data logging to name a few).  I settled on the iPad iOS device due to the large screen, low power consumption, wifi and bluetooth, audio / video capability, and a ginormous supported SDK.

This first post is a guide to getting a propeller and iPad talking over bluetooth.  The apps featured here are not functional for control purposes yet; there is plenty to learn just getting them to talk.

Bluetooth Serial Port Profile (SPP) vs other profiles

This guide uses a seed studio bluetooth bee, which only supports UART communication from propeller to bluetooth.  The iOS side uses btstack, an open source user-space bluetooth stack that also supports L2CAP and HID.   I chose UART for the propeller side because the modules are cheap ($15-$20) and I have no reason to use a lower level protocol.  You could easily modify this example to use L2CAP with a more expensive bluetooth module.

What you need to follow this guide

  • jailbrokeniOS device (examples here are tested on an iPad running iOS 5 only)
    • openSSH access to your jailbroken device (or some other way to copy the app files)
    • btstack installed from Cydia on your iOS device
  • a propeller
  • seed studio bluetooth bee (you can use a different device and provide your own propeller code)
  • My prop code and iPad app
If you want to modify source code you also need:

Wait, I need a jailbroken device?

Unfortunately the imperial overlords at apple will not let us access their bluetooth stack without paying them a lot of money joining the MFI program (you need at least an LLC and legal counsel to be approved). In order to use our own bluetooth stack, we must jailbreak the device.  I won’t go into how, but you can google it.

The propeller code

The prop code contains a spin object for the bee and a demo program.  The UART to SPP operations are pretty simple; if you send a recognized command string it is parsed by the module, anything else is sent to the SPP tx buffer (if you have already established a connection).

The spin object is dumb, it sends commands, waits for the OK string (with a timeout), and flushes the remaining input (status codes from the bee).  The only exception is the “scan” method, which consumes the bee response to capture the mac addresses of scanned devices.

note on baud: By default the bee is 38400, but the screenshots show a higher baud rate because I set mine higher then reconnected.  I updated the code to use the default 38400.

step 1 – connect the module

prop                bluetooth bee
pin21 *-----------* PIO0 (optional)
pin22 *-----------* DIN
pin23 *-----------* DOUT
3.3v  *-----------* VCC
GND   *-----------* GND

step 2 – pair a device (I had to pair my iPad using this method before I could connect to btstack)

Turn bluetooth on under general settings in your iPad.  In BluetoothBeeDemo, comment out the code such that only this part runs. Remember to add the mac address of your device:

When you run this code you should get a pairing request on your device. Enter the code (1234). When your device reports that it’s paired, you can turn off / reprogram the prop.  Important – turn OFF bluetooth under general settings in your iPad now, or btstack won’t be able to establish a connection in the next steps. 

step 3 – run the demo code that will send data to ipad, again make sure you set the mac address for your device

This sample code just transmits the current cnt counter every 1 second. Now that we have the prop ready to roll, we need to set up the iPad application.

The iPad code

step 1 – prepare your iPad

  • It must be jailbroken
  • Turn bluetooth OFF on your device (btstack can’t take over if it’s already on)
  • Install btstack from Cydia on your device:

  • Set up openSSH per the instructions on the Cydia home page (or find some other method that lets you place code in the /Applications folder)

step 2 – download and unzip the application

step 3 – use scp (secure copy) to copy the app folder to your device:

step 4 – either power cycle your iPad, or use ssh to log in and restart springboard:

step 5 – open BTStackDemo on your iPad and start listening for connections:

step 6 – now power on the propeller using the code labeled “send data via serial”, if everything is working you should see the connection established and numbers being printed in the iPad app:

step 7 (optional) – if you want to modify the iPad source code, there are a few things you need to know

  • To build the app in xode you must self-sign it, instructions are here
  • When re-deploying to the iPad, you must ssh to the device and rm -rf /Applications/BTStackDemo first

Wrap Up

This post is just to get you started.  It exposes the minimum amount of code required to integrate btstack into a working iOS app and communicate with it via SPP.  My next post will demonstrate a simple command protocol over SPP that will be the basis for my robot control UI.