Gatttool, ubuntu, and Adafruit’s Bluefruit LE – NRF8001 Bluetooth low energy breakout in 20 minutes

Phone control

Thanks to Nancy for sending in her tutorial from her blog orange narwhals. Check out more here.

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
./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.

Read more.


Featured Adafruit Product!

NewImage

Bluefruit LE – Bluetooth Low Energy (BLE 4.0) – nRF8001 Breakout: Our Adafruit Bluefruit LE (Bluetooth Smart, Bluetooth Low Energy, Bluetooth 4.0) nRF8001 Breakout allows you to establish an easy to use wireless link between your Arduino and any compatible iOS or Android (4.3+) device. It works by simulating a UART device beneath the surface, sending ASCII data back and forth between the devices, letting you decide what data to send and what to do with it on either end of the connection. Read more.


Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards

Join Adafruit on Mastodon

Adafruit is on Mastodon, join in! adafruit.com/mastodon

Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.

Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7pm ET! To join, head over to YouTube and check out the show’s live chat – we’ll post the link there.

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Join over 36,000+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython – The easiest way to program microcontrollers – CircuitPython.org


Maker Business — “Packaging” chips in the US

Wearables — Enclosures help fight body humidity in costumes

Electronics — Transformers: More than meets the eye!

Python for Microcontrollers — Python on Microcontrollers Newsletter: Silicon Labs introduces CircuitPython support, and more! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi

Adafruit IoT Monthly — Guardian Robot, Weather-wise Umbrella Stand, and more!

Microsoft MakeCode — MakeCode Thank You!

EYE on NPI — Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey

New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — #NewProds 7/19/23 Feat. Adafruit Matrix Portal S3 CircuitPython Powered Internet Display!

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !



1 Comment

  1. This was very helpful, if you do not have an iOS od Android device availble.
    Thank you!

Sorry, the comment form is closed at this time.