Ubuntu Hacking the HC 06 bluetooth module

Ubuntu Hacking the HC 06 bluetooth module


A detailed post about a cheap BlueTooth <-> UART module, often called HC-06, BC417, BT0417C, EGBT-046S, Bluetooth bee, etc.

(Windows user? I have a dedicated post for you, however it only covers the PC-side of the communication. Keep reading this post, and Ill tell you when to switch.)

If you are looking for a cheap and simple way to communicate wirelessly a microcontroller to a PC or to a smartphone, then you should definitively look at the HC-05/HC-06 bluetooth modules, these module are widely available at eBay and DealExtreme, so you can get one shipped to wherever you are.


Both modules have the same hardware, but different firmware. The HC-06 is a slave device, i.e. it cant start the communication (this process is know as pairing in bluetooth jargon) with another device, it needs a master (PC/Smartphone) that pairs with it. On the other hand, the HC-05 is a master/slave device, i.e. its capable of peer to peer communication with other HC-05 modules, it can be the master of an HC-06 module, it can be a slave of a PC/Smartphone, etc.

Both modules are 3.3V powered and have an 3.3V UART interface, to which we can hook any 3.3V microcontroller or an USB->UART device. 

HC-06 setup and configuration


This time, Ill show you how to interface a Ubuntu PC to a HC-06 a.k.a. "linvor" module, which I got from DealExtreme.

 
Recommended HC-06 Setup (Datasheet)

Upon powering on the module, the LED will start blinking (if there is another bluetooth device in automatic search mode), telling you that the module is alive.

We can now do some configuration via the UART pins (in my case the default baud rate was 9600, in other modules it might be 38400) sending AT commands, the complete list is shown below:

CommandResponseComment
ATOKDoes nothing!
AT+VERSIONOKlinvorV1.5The firmware version
AT+NAMExyzOKsetnameSets the module name to "xyz"
AT+PIN1234OKsetPINSets the module PIN to 1234
AT+BAUD1OK1200Sets the baud rate to 1200
AT+BAUD2OK2400Sets the baud rate to 2400
AT+BAUD3OK4800Sets the baud rate to 4800
AT+BAUD4OK9600Sets the baud rate to 9600
AT+BAUD5OK19200Sets the baud rate to 19200
AT+BAUD6OK38400Sets the baud rate to 38400
AT+BAUD7OK57600Sets the baud rate to 57600
AT+BAUD8OK115200Sets the baud rate to 115200
AT+BAUD9OK230400Sets the baud rate to 230400
AT+BAUDAOK460800Sets the baud rate to 460800
AT+BAUDBOK921600Sets the baud rate to 921600
AT+BAUDCOK1382400Sets the baud rate to 1382400

The AT commands are NOT "NULL" terminated neither need a "RETURN" character at the end. They must be sent as shown in the table, i.e. "AT" command are 2 characters, "AT+VERSION" command are 10 characters, and so on.

If you are using a USB->UART device + minicom or similar to send the AT commands, you are going to run into problems, as the HC-06 expects a full command in less than a second, i.e. you need to type "AT+VERSION" in less than a second. I recommend qSerialTerm instead of minicom, as you can send a full string at once.

OK, thats all we can do with an unpaired module. Lets proceed to pair it with an Ubuntu PC and establish a virtual serial port between the PC and the module.

(Windows users: Go to this post, for a proper configuration on the PC).

Pairing with an Ubuntu PC


First, lets find out the MAC address of the device, using the following command on the terminal:

hcitool scan

The output will look like this:

11:22:33:44:55:66       linvor

Where linvor would be the name of your HC-06 and 11:22:33:44:55:66 would be its MAC address.

We must now add this information to the rfcomm.conf file, rfcomm is a protocol of serial port emulation of the Bluetooth standard. To do so, type the following command in your terminal:

gksudo gedit /etc/bluetooth/rfcomm.conf

In the text editor that appeared, insert the following text (dont copy the line numbers):

Instead of 11:22:33:44:55:66 use the MAC address of your HC-06 module. You can also use any other rfcomm port like the rfcomm1, rfcomm2, etc.

You should reboot your PC to make all the changes to the rfcomm.conf file effective.

After the reboot you can pair your PC to the HC-06 module, using the following command:

sudo rfcomm bind rfcomm0

Where rfcomm0 is the port you configured in the rfcomm.conf file.

This will pair your PC to the HC-06, you can now use rfcomm0 as a serial port, via minicom or qSerialTerm (more details below), to send/receive data to/from the HC-06 module. As soon as you open the rfcomm, the HC-06 led will stop blinking, and stay ON. On this point, the communication becomes a transparent asynchronous serial communication.

Check this post on how to open/use serial ports without sudo.

When you are done with the communication and have closed the rfcomm port, you can use the command:

sudo rfcomm release rfcomm0

To unpair the module and the PC, otherwise the link will stay up until the next shutdown, reboot, logout.

Using rfcomm on qSerialTerm


qSerialTerm relies on the QtSerialPort library, and uses two of its classes: SerialPort and SerialPortInfo. The SerialPortInfo contains methods that detect the available ports, however it doesnt detect the rfcomm ports at this point. The SerialPort works flawlessly with the rfcomm ports, although some manual configuration is needed as the SerialPortInfo class isnt providing information about these ports.

qSerialTerm (rfcomm branch)

I have modded the qSerialTerm to add support for the rfcomm ports, all the new source code is available in the rfcomm branch (Repository). (The SerialPortInfo issue has been fixed, and the rfcomm branch has been deleted, use the master branch instead)


Now, Ill take a deeper look at the SerialPortInfo class issue. See you later.

I have commited a fix to the QtSerialPort library. Lets see if the reviewers accept the commit.

My commit has been accepted by the reviewers, after lot of nagging from both sides. Now, the master branch of qSerialTerm works with rfcomm devices.

Here is my full testing setup of the HC-06.

HC-06 testing setup. The HC-06 modules Tx/Rx pins are shorted (yellow wire).

Here is my testing session of qSerialTerm with the aforementioned fix applied to the QtSerialPort library.

Testing the qSerialTerm (master branch).


visit to link download