Tag Archives: Uncategorized

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!

Bluetooth Low Energy (4.0) on Ubuntu 13.10: Advertisements, Sending and Receiving

Here is a quick tutorial on how to send and receive data without pairing with ubuntu 13.10 and two CSR 4.0 BTLE dongles ($12 including shipping from ebay for both dongles). Surprisingly straightforward.

  • install things

$ sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev bluez wireshark

  • plug in the first dongle (it should show up in hciconfig as “hci0”)
  • start LE scan capture

$ sudo hcitool lescan (you’ll see it spit out things such as “00:1A:7D:DA:71:0D (unknown)”)

  • start wireshark as root

$ sudo wireshark

  • In wireshark, start capture on bluetooth0
  • plug in the second dongle, should show up as hci1
  • program hci1 with hciconfig

$ sudo hciconfig hci1 noleadv (sometimes you can skip this step, sometimes the next step, leadv, will throw up “LE set advertise enable on hci1 returned status 12” if you don’t do noleadv first)

$ sudo hciconfig hci1 leadv

$ sudo hciconfig hci1 noscan

$ sudo hcitool -i hci1 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00

  • you’ll see the terminal spit out:

< HCI Command: ogf 0x08, ocf 0x0008, plen 32 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00

> HCI Event: 0x0e plen 4 01 08 20 00

  • On Wireshark, you should now get a packet and see the above data in it.

 

Screenshot from 2014-06-29 02:16:20

Python

Right now, I have only figured out how to read the advertisement data, not set it.

This is using code on stackoverflow: http://stackoverflow.com/questions/23788176/finding-bluetooth-low-energy-with-python which I saved as ble-python.py. If you add this line, it will print out the payload:

print(':'.join("{0:02x}".format(x) for x in data[44:13:-1]))

$ sudo python3 ble-python.py

00:1a:7d:da:71:09 c8:00:00:00:00:61:a9:07:ad:d1:17:2f:a1:c4:4b:f5:73:f4:39:0a:e2:15:02:00:4c:ff:1a:1a:01:02

You can see that both the manufacturer ID and the advertising payload is now printed out.

UPDATE 30 June 2014

Here is how to write data using the second CSR4.0 dongle and python:

import subprocess
dev = 'hci1'
adr = '0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 05 06 07 08 C8 00'
cmd_cc = "hcitool -i %s cmd %s" % ( dev, adr )
subprocess.Popen(cmd_cc.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()

and if you want to convert strings to hex,

s = 'some data'
" ".join("{:02x}".format(ord(c)) for c in s)

References

http://www.warski.org/blog/2014/01/how-ibeacons-work/ http://en.wikipedia.org/wiki/IBeacon http://hackaday.com/2013/12/05/turning-a-pi-into-an-ibeacon/ https://learn.adafruit.com/pibeacon-ibeacon-with-a-raspberry-pi/adding-ibeacon-data http://stackoverflow.com/questions/22568232/how-to-retrieve-advertising-payload-from-ibeacon-ble
http://stackoverflow.com/questions/23788176/finding-bluetooth-low-energy-with-python
https://www.bluetooth.org/en-us/specification/adopted-specifications, Core_v4.1.pdf, pg 2023 (Bluetooth 4.0 Core Spec. Volume 3, Part C, Section 11.1.4 or 11.1.10)

How do I become a sink for STEM education research, like I am a sink for engineering information?

How do I find the right circles to be as “in” in STEM education as I am in engineering?

Today I want to discuss something that’s been frustrating me for a while: the disparity in my information sources for engineering questions and for STEM education questions.

Here’s what a contrived day looks like (these things have all happened, but not all in one day):

  • Wake up, check my email. Oh, there’s a cool robotics conference coming up soon. And someone sent me a link to the latest hexapod (six-legged robot).
  • Check facebook. Charles posted pictures. “Catalyzed Destruction of a Poly(lactic acid) 3D Printed Bunny (15 photos)”. Huh, I didn’t realize PLA melted like that.
  • Go to work. Work on engineering most of the day with three other engineers. Why aren’t the stepper motors working?? So glad we bought an oscilloscope. After checking stackoverflow, still have no idea how to implement bluetooth low-energy (BLE). Ask friends on google talk and the MITERS mailing list for help. Earlier asked on the Artisan’s discuss and putz-course-6 (the hall I lived on during undergrad’s CS majors list) for BLE help. Crap, no FTDI cable. Facebook chat Charles and get permission borrow one.
  • Chill, read a few friends’ blog posts about their latest technical accomplishment. Oh look, one of my friends is on Hackaday.
  • Grab a snack with a friend after work so that he can help me with our PCB layout software, Diptrace
  • Go to a meeting for contract engineering work. Talk about the latest quadcopters and their costs and how this impacts STEM workshops for high schoolers.
  • Go to MITERS. Rant with my friends about the need for a usable open-source CAD program, and also debate ways we could make a local printed circuit board same-day turnaround manufacturing service.
  • Oh, it’s dinner time, walk with friends over to H-mart and discuss the latest crazy idea we have and bemoan the startups invading our space
  • Meet my friends who stay up until 3 am helping me figure out how to implement bluetooth low energy on our robot.
  • Go home and sleep.

As you can see, if I need engineering help, even just to borrow tools, I have several options:

  • Chat friends a few years older than me or who are more specialized in the field. I have at least ten people I gchat periodically with engineering questions and feel comfortable doing so
  • Email out to engineering lists I’m on, such as MITERS and Artisan’s Asylum discuss and putz-course-2/6, for help
  • Meet people in person for help

I’m also a sink for engineering information. If I do nothing, I still receive tons of information about the latest engineering doodads.

I wish this were the same way regarding information and available resources in STEM education, specifically diversity in STEM education. I want to be a sink for this information. Right now, if I have questions about what’s an effective way to teach pulse-width-modulation, I have no one to gchat. If I have questions about whether my theory that having people creatively build their own things is a good idea and whether it’s worked in the past, I have no one to email. If I need a lengthy strategic session to figure out how I can best help diversify STEM education, there is no one to have lunch with. When I check facebook, I’m not inundated with information or links to videos of the latest STEM education research.

Instead, I have to expend effort and time trying to find all these resources.

My question to you, dear reader, is how can I change this? How do I find the right circles to be as “in” in STEM education as I am in engineering?