Python -> Adafruit nrf8001 bluetooth low energy breakout in 20 minutes (Ubuntu 14.04)

As part of my work on Swarmbuddies (robots that dance to music and create formations)

we decided to use bluetooth low energy for smartphone support, but also needed it to work on the desktop side for our computer vision software to work.
Here are the steps we needed to take to get it working.

1) Install Ubuntu 14.04

This is the easiest path. Really recommended, since bluez-5.20 wants some new version of dbus which can be installed on 12.04, but which will crash your computer incredibly hard when you reboot.

2) Install the latest version of bluez, bluez v 5.20 (or check http://www.kernel.org/pub/linux/bluetooth/ for the latest version), and uninstall your current version. Major help from jaredwolff.com/blog/get-started-with-bluetooth-low-energy for this step!

sudo apt-get remove bluez
sudo apt-get remove bluez-cups
sudo apt-get remove bluez-hcidump
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.20.tar.xz
 tar -xvf bluez-5.20.tar.xz
 cd bluez-5.20/
 sudo apt-get install libudev-dev libical-dev libreadline-dev libdbus-1-dev
 ./configure --enable-library --disable-systemd
 make
 make check
 sudo make install
 sudo cp attrib/gatttool /usr/bin/

3) On the hardware side, your arduino should be hooked up and programmed as per Adafruit’s tutorial.

Let’s test your connection. Plugin your CSR 4.0 dongle ($6 on ebay) if your laptop doesn’t support bluetooth 4.0 (or even if it does, we’ve found the dongle to be more reliable):

sudo hcitool lescan

If this succeeds you should see a bunch of scrolling information, including the MAC address of the nrf8001 breakout, which should be something like “EF:FC:D3:56:41:B7”. If it says file descriptor not found or otherwise exits immediately, use

$ dmesg | tail

to check that your dongle is being recognized by your computer.

4) Open Arduino and the serial monitor. Now try writing wirelessly to the Arduino with gatttool

$ sudo gatttool -b EF:FC:D3:56:41:B7 -I -t random
> connect

The white characters should turn blue. Now try writing to the UART service:

char-write-cmd 0xb FF00FF

5) You should see “3 bytes received” and your command on the Arduino.

6) Now to script it!

a) We need to change the Arduino code to do what we want. Here is an example of how to take the . A small explanation: If you don’t cast it to byte, the buffer is an array of characters which go to 128 instead of 255. I haven’t figured out how to check if it’s from the desktop or the smartphone, so once you cast to byte you lose compatibility with the default nRF UART application on Android and your Arduino will only process computer commands correctly.

https://gist.github.com/nouyang/2a6a733d8facd23115a4

Insert the arduino.ino code there or similar code into your Arduino IDE and upload it.

b) Create a folder and download the files at  https://gist.github.com/nouyang/2a6a733d8facd23115a4 somewhere. Modify NUMBOTS in constants.py to the number of breakouts you want to connect to.

If you’re looking at the btle-server.py code, please note that reading the pipe and setting the pexpect delay to 0 are critical for your bluetooth connection to not lag or take a while between commands.

self.con = pexpect.spawn('gatttool -b ' + self.ble_adr + ' -I -t random')
self.con.delaybeforesend = 0 #THIS LINE IS SUPER IMPORTANT
self.con.read_nonblocking(2048,0) #flush the read pipe!! SUPER IMPORTANT

c) Look at the processing or else the python code.  Modify it to your use.

Usage

Tab 1

$ sudo python btle-server.py

(hit “y” if your dongle doesn’t show up the first time, I only wait a second so sometimes the dongle doesn’t catch your device. If you try a few times and it doesn’t work, check sudo hcitool lescan to make sure your device is advertising. Try hitting reset on it.)

Tab 2

$ python python-client.py

Tada! That’s it! Here it is working on the computer:

I had to simplify the code a lot from our current structure, so let me know if it makes sense or doesn’t run.

For more information about how I figured all of this stuff out, see:

https://forums.adafruit.com/viewtopic.php?f=19&t=56332&sid=1423cb2d05389f479c2d5dd164d14e35

http://stackoverflow.com/questions/24564587/communicate-between-a-processing-sketch-and-a-python-program/24565160?noredirect=1#comment38049611_24565160

Like this tutorial? Want to take a class on programming your very own swarm of robots or have future tutorials like it? Back us on kickstarter!

6 thoughts on “Python -> Adafruit nrf8001 bluetooth low energy breakout in 20 minutes (Ubuntu 14.04)”

  1. Dear Nancy, I am very impressed with:
    https://gist.github.com/nouyang/2a6a733d8facd23115a4

    I too am doing similar things, but for BLE healthcare sensors. I have it working with one sensor but need to connect with more than two sensors, hence my interest in the above.

    I tried to run it, but I cannot find blescan from “import constants, blescan”. It is a python code file? If so can you please send it to me, and/or add it to this instructions web page.

    Yours
    Simon

      1. After I execute this cmd (python python-client.py) in a new terminal I got this error:

        python python-client.py
        Traceback (most recent call last):
        File “python-client.py”, line 7, in
        self.sock.send(message)
        NameError: name ‘self’ is not defined

        Any idea why is that?

Comments are closed.