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.
(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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiPBxjJfex6Bqx00waN59yMUrTQMwLUrr-H7Enqltc-q2aj1jtt9r-YDvyQy8i7QLV5yvHAr74rnAnX_jWeoChGNylSBFSQkzy1CGGMCiTi2YIZPR_3l42N4iNJze5eUGbpklFO6l9abg/s320/HC-06.png)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinml-oatPihbLJRx1rz8pl6uyDrIjMR_z3wyVMUfyLqVz-vNCH_xG_mde3Z_zDFLc962O0Brf1Xa7XaEvggp8RndnxwIWylxWqMx-8ku99VDsAaMDdlTeiYAwtGCXoDthjfRYXHBMY72w/s320/HC-06+Pinout.png)
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:
Command | Response | Comment |
---|---|---|
AT | OK | Does nothing! |
AT+VERSION | OKlinvorV1.5 | The firmware version |
AT+NAMExyz | OKsetname | Sets the module name to "xyz" |
AT+PIN1234 | OKsetPIN | Sets the module PIN to 1234 |
AT+BAUD1 | OK1200 | Sets the baud rate to 1200 |
AT+BAUD2 | OK2400 | Sets the baud rate to 2400 |
AT+BAUD3 | OK4800 | Sets the baud rate to 4800 |
AT+BAUD4 | OK9600 | Sets the baud rate to 9600 |
AT+BAUD5 | OK19200 | Sets the baud rate to 19200 |
AT+BAUD6 | OK38400 | Sets the baud rate to 38400 |
AT+BAUD7 | OK57600 | Sets the baud rate to 57600 |
AT+BAUD8 | OK115200 | Sets the baud rate to 115200 |
AT+BAUD9 | OK230400 | Sets the baud rate to 230400 |
AT+BAUDA | OK460800 | Sets the baud rate to 460800 |
AT+BAUDB | OK921600 | Sets the baud rate to 921600 |
AT+BAUDC | OK1382400 | Sets 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.
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.
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjsvr_ekF4DaB3yUZOvg685vt1PCmlnMwC2WrmOxWA3RSm7Hu9GRjSzE0ZzoIyChZIBfj0sVrXVD2PSjwfbjF7KDv7xh1wjKs60R0NoeJOePJOX13QMUbi6alXFMtMhSUuAd3S9AN0Wns/s320/qSerailTerm-rfcomm.png)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB-HIWV4LaTGrri_mfK_ca09WopKT-r5a8_wDhgu4_O37JI-CgI0JNFDzUBqsHx1WEPqDlVsNMROu26YJiZHrbeNaC8NKlnUdK1HOpjJaqTjO_bCQm_uPiD-dgkIrAJopW-0JnJZm96-k/s320/bluetoothSetup.jpg)
Here is my testing session of qSerialTerm with the aforementioned fix applied to the QtSerialPort library.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi447BU227Z6_4PZ_TaICkImyaQuWCczL9L68nNUjZK8A_FqgTXREW6WsZEu_aBvMNCKvZLjR6EcMvzcnK5uxjPV6jK5ANX9j52XWn0DFUE5tXEqTTL1PWioTJuK0FA0SVctGm3_u1sab8/s320/bluetoothTest.png)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjsvr_ekF4DaB3yUZOvg685vt1PCmlnMwC2WrmOxWA3RSm7Hu9GRjSzE0ZzoIyChZIBfj0sVrXVD2PSjwfbjF7KDv7xh1wjKs60R0NoeJOePJOX13QMUbi6alXFMtMhSUuAd3S9AN0Wns/s320/qSerailTerm-rfcomm.png)
qSerialTerm (rfcomm branch)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB-HIWV4LaTGrri_mfK_ca09WopKT-r5a8_wDhgu4_O37JI-CgI0JNFDzUBqsHx1WEPqDlVsNMROu26YJiZHrbeNaC8NKlnUdK1HOpjJaqTjO_bCQm_uPiD-dgkIrAJopW-0JnJZm96-k/s320/bluetoothSetup.jpg)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi447BU227Z6_4PZ_TaICkImyaQuWCczL9L68nNUjZK8A_FqgTXREW6WsZEu_aBvMNCKvZLjR6EcMvzcnK5uxjPV6jK5ANX9j52XWn0DFUE5tXEqTTL1PWioTJuK0FA0SVctGm3_u1sab8/s320/bluetoothTest.png)
Testing the qSerialTerm (master branch).