First steps with the HC-05 on the BeagleBone Black

hc05_logo
Wireless communication between an embedded device and PC or smartphone is easily accomplished using the HC-05 bluetooth module. The BeagleBone is a great tool to explore the features of it and one can even settle with bluetooth in his final application.

Enabling the UART5

I decided to connect the HC-05 module to the UART5 of the BeagleBone. To make this possible, I had to disable the HDMI output, since they are sharing the pins, but I’m not planning to use a dedicated display anyhow. Disabling the HDMI output and enabling the UART5 can be done in a single step by editing the boot options for the cape manager in the uEnv.txt. This is a bit tricky, because you may find several uEnv.txt’s on your board, the correct one for me was in /boot/uboot. I’ve installed the Ubuntu image on an SD card, and still have the Angstrom on the onboard flash. So in my case this is what the output of mount looks like:

root@beaglebone:~# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,relatime,size=10240k,nr_inodes=63164,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620)
tmpfs on /run type tmpfs (rw,nosuid,relatime,size=101692k,mode=755)
/dev/mmcblk0p2 on / type ext4 (rw,noatime,errors=remount-ro,data=ordered)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,relatime)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,relatime,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=24,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
securityfs on /sys/kernel/security type securityfs (rw,relatime)
tmpfs on /run/user type tmpfs (rw,nosuid,nodev,noexec,relatime,size=102400k,mode=755)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/mmcblk0p1 on /boot/uboot type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)

The last line is a seperate small partition on the SD card, this is the one the BeagleBone booted from. (First I tried modifying the uEnv.txt in /media/BEAGLEBONE with no luck, but a quick Google search revealed the problem.)

In the proper uEnv.txt I already found the extra lines I needed, just had to remove the comments and set the parameters accordingly.

##Disable HDMI
cape_disable=capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN

##Example
#cape_disable=capemgr.disable_partno=
cape_enable=capemgr.enable_partno=BB-UART5

Save it and don’t forget to reboot. After that you can check whether it worked by having a look at the slots:

root@beaglebone:~# cat /sys/devices/bone_capemgr.9/slots
0: 54:PF---
1: 55:PF---
2: 56:PF---
3: 57:PF---
4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
6: ff:P-O-- Bone-Black-HDMIN,00A0,Texas Instrument,BB-BONELT-HDMIN
7: ff:P-O-L Override Board Name,00A0,Override Manuf,BB-UART5
8: ff:P-O-L Override Board Name,00A0,Override Manuf,ADAFRUIT-SPI0

In slots 5 and 6 the lack of L means that it is not loaded, in slot 7 we find the UART5 we needed.  (In slot 8 we see the SPI override I needed to communicate with the ADS1248.) As expected and desired, the /dev/ttyO5 node appears as well:

root@beaglebone:~# ls -l /dev/ttyO*
crw-rw---- 1 root tty 248, 0 May 15 04:23 /dev/ttyO0
crw-rw---T 1 root dialout 248, 5 May 15 04:23 /dev/ttyO5

Wiring the module to the BeagleBone

We get the supply voltage from the P9 header, and connect the TX to RX and vice versa to the UART pins as it is illustrated below. It’s optional to connect the LEDs to the HC-05, but to get in AT mode, we will need to drive the pin 34 high. This can be accomplished using a button, as the label suggests, or can be connected to a GPIO. For this tutorial I’ve temporarily soldered a jumper wire to it which I can connect to the supply voltage.

hc05

According to the HC-05 manual, there are 2 ways to get in AT mode, the first one is pulling the pin 34 high while the module is powered, the second one is to power it on while it is pulled high. In the latter case, which I chose, the Baud rate will be  38400 bps. (The pin must be kept high in both methods to have all AT commands available.) Here is my messy setup with the jumper wires. The breadboard has the ADS1248 on its back.

IMGP0936

Opening a terminal

To send commands to the bluetooth module, we need to open a terminal for the serial port. For linux, minicom is a nice client with a text-based user interface. Start with minicom -s to get in setup mode. Select the serial device, set the Baud rate, set 8 data bits, no parity, stop bit:1, no flow control. (To make the characters of the UI look as they supposed to look like, I had to set latin-1 encoding in putty.)

minicom1

In the Screen and keyboard menu you can enable local echo and add automatic linefeed character.minicom2 After you select exit in the menu, you are already able to send commands over the open serial port. Test the connection with the “AT” command:

Welcome to minicom 2.6.1

OPTIONS: I18n
Compiled on Feb 11 2012, 18:45:56.
Port /dev/ttyO5

Press CTRL-A Z for help on special keys

AT
OK
OK
OK
OK
OK
OK

As you can see, the device acknowledged the command by sending “OK”, but it didn’t stop sending it until I pressed enter again. This is a minor annoyance for me, and since I don’t want to use minicom on the long term, just for testing, I didn’t bother to look into it in detail. However, I believe that the origin of the problem is that minicom doesn’t send “\r\n” as it should, for some reason, even though the “add linefeed” option is set. (Remark: when I first tried to send “AT”, I ended up with ERROR:(0) responses, because I used putty over the virtual serial port of the BeagleBone (“Gadget serial”), and it sent some additional characters along with the command string. After changing to ethernet this problem was gone.)

Simple Python program

There is an example program for the Adafruit BBIO library which I quickly modified to address the linefeed issue. The Python code for sending the “AT+VERSION?\r\n” command and then reading one line from the device:

import Adafruit_BBIO.UART as UART
import serial

UART.setup("UART5")

ser = serial.Serial(port = "/dev/ttyO5", baudrate=38400)
ser.close()
ser.open()
if ser.isOpen():
   print "Serial is open!"
   ser.write("AT+VERSION?\r\n")
   resp=ser.readline();
   print resp;
ser.close()

The result I got:

root@beaglebone:/home/debian/serial# python test.py
Serial is open!
+VERSION:2.0-20100601

Then running the same code without the ser.write() function, first I got:

root@beaglebone:/home/debian/serial# python readline.py
Serial is open!
OK

After running it for the second time it just hangs, meaning that there is no string to read, as it should be:

root@beaglebone:/home/debian/serial# python readline.py
Serial is open!

Terminal connection over Bluetooth to a Windows PC

To test some real bluetooth connection, set the HC-05 module to slave and “connect the module to any address” with “AT+ROLE:0\r\n” and “AT+CMODE:1\r\n”. Then reboot the module with pin 34 floating, and try to add a device in Windows. By default the device will have the name HC-05, and the pairing password will be 1234. After pairing wait until Windows installs the driver and the serial port appears.

hc05_dev

Then open the serial port on both the BeagleBone with minicom and on Windows with putty, for example (here the baud rate is 9600 bps by default). Now if you type anything in either terminal, it will appear in the other one.

Alternatively, you can set the module to slave loopback mode with “AT+ROLE:2\r\n” and “AT+CMODE:2\r\n”, in this mode it will just send back the data it receives from the master (looks like a local echo).

References

HC-03/05 Embedded Bluetooth Serial Communication Module AT command set
HC Serial Bluetooth Products

Advertisements

One thought on “First steps with the HC-05 on the BeagleBone Black

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s