Bluetooth Low Energy, Arduino, and Xcode

In some ways Apple signaled what it sees as the future for home automation when it introduced HomeKit with support for 802.11 (Wifi) and Bluetooth Low Energy (also referred to as BTLE or Bluetooth Smart) as transport protocols. Notably absent were both Z-Wave and Zigbee. Sure, Apple provided for the implementation of a “bridge” device to these existing protocols, but every product announcement since then touting HomeKit support has been a Wifi or BTLE device. Don’t take my word for it, browse through this article outlining the devices showcased at CES this year. A brief lineup:

and more. Neither Z-Wave nor Zigbee is mentioned, which is not to say that either protocol or the the thousands of devices that are out there will simply go away, only that it’s clear Apple sees BTLE and Wifi as the future for home automation control. Whether that is true remains to be seen.

But this article isn’t about HomeKit, it’s about Bluetooth Low Energy itself and how to quickly integrate an Arduino-powered Adafruit BTLE board with your iPhone, all using an Arduino Xcode project. So let’s get started.

In our previous post we covered how to create an Arduino sketch using Xcode and the embedXcode project. The outcome of that process was a simple Arduino application that blinked two LEDs back and forth, and while cool, not exactly ground breaking. Let’s take it to the next level and integrate an Adafruit nrf8001 BTLE Breakout board with the Arduino and talk to it with our iPhone. Moreover, we’ll do all of this with our favorite IDE (well, mine at least), Xcode.

Before we get started, you’ll need to have the following on hand:

  • Arduino Uno board and USB programming cable
  • Breadboard and 8 male-to-male jumper cables
  • Adafruit nrf8001 BTLE Breakout board
  • Arduino application installed on your Mac
  • embedXcode installed on your Mac
  • an iPhone and access to the AppStore

For the last two items, see our previous post for instructions on installing.

The nrf8001 breakout board does not come out of the box with the header pins soldered on, so you’ll need to do that step. Luckily the instructions are provided by Kevin Townsend’s excellent nrf8001 article.

Assuming you the breakout board soldered and in your breadboard, wire it per the instructions found on Adafruit:

adafruit_products_8001Final

From the Adafruit nrf8001 article

btlewiring

Don’t take wiring lessons from me

Once the wiring is completed, we need to install the Adafruit BLE UART software into our Mac’s Arduino user library folder. There is a distinction between system libraries and user libraries that will become apparent in a moment.

To install the Adafruit BLE UART software as a user library click on this download link. A folder named Adafruit_nRF8001-master will be created in your Downloads folder. Rename this folder to Adafruit_BLE_UART and then drag-and-drop it into your ~/Documents/Arduino/libraries folder:

arduinolibraries

Now, let’s create our Xcode project. Open Xcode and use File – New – Project to create a new embedXcode Sketch. We’ve named our project nrf8001 and targeted the Arduino Uno board.

nrf8001_options

To walk this through in stages, open the nrf8001.ino file in your project and delete its contents and then add following to it:

This code is actually found in the Adafruit_BLE_UART folder you downloaded earlier, under the examples folder in a file called echoDemo.ino.

In Xcode click the Run (play) button using the All target. You should get a Build Failure with an error:


Shell Script Invocation Error SPI.h: No such file or directory

Recall that we are dealing with C++ when working with Arduino, and a header inclusion failure is likely the result of failing to specify that we are using some library. In this case we are wanting to use the Arduino SPI system library and didn’t tell Xcode in our Makefile. So open the Makefile and find the lines:

Change the APP_LIBS_LIST line to APP_LIBS_LIST = SPI and try to compile again. The error now should be a failure to find Adafruit_BLE_UART.h, which is a part of a user library. Looking back at the Makefile we see a USER_LIBS_LIST definition, so change it to

Run again and you should see a terminal screen pop up like this:

adafruitterminal

If you are seeing the same output, congratulations! You’re almost there.

Now, let’s grab an application for our iPhone to connect to our Bluetooth module! On your iPhone search for the nRF UART application from Nordic Semiconductor. Download and open the app on your iPhone and, of course, ensure Bluetooth is enabled!

The nRF UART app isn’t fancy – a button to Connect to the Adafruit BTLE board, a Console showing you what’s going on, and a text field for sending text.

IMG_8800

Bring your Adafruit terminal log into focus on your Mac and then press the Connect button on the nRF UART app on your iPhone. If everything is working properly (all of your pins are wired to the right spot and the application is running) you should see Connected! on your Mac terminal and the console log on the iPhone indicating that a connection was successful.

IMG_8801

Now, type some text in the textfield on the iPhone app and press Send. If everything is working properly you should see the text you typed in your phone show up in your Arduino application console!

IMG_8802

From the iPhone…

adafruit_browncow

To Arduino!

Get the Code

The complete Xcode project for working with the Adafruit nrf8001 is on Bitbucket. Remember, you must have the Adafruit_BLE_UART library installed in your Arduino user library folder.

Acknowledgements

This post is really made possible by Kevin Townsend and his article on Adafruit. As I’m quickly learning, much of Arduino magic comes from the fact that it just works. For example, although I know SPI and some BTLE protocol details, I didn’t have to lean on that knowledge to put this tutorial together or get my iPhone and Arduino/Adafruit BTLE communicating!

Leave a Reply

Your email address will not be published. Required fields are marked *