The purpose of this post is to figure out how to connect a Linux PC to the HC-05 bluetooth module and send text to it through linux commands.
Here's the setup I have for this test:
- Ubuntu 18.04 installed in my laptop.
- HC-05 bluetooth module.
My DIY base board
I bought this module without the base board long time ago because I wanted to take advantage of all the GPIOs it has. When I did the purchase I was still studying at the university. I had this crazy idea of designing and building my own custom base board for the module. I used KiCad to draw the pcb for the base board. It has all the holes for the pin headers and each of them connected to a GPIO on the module.
I made the pcb the old fashion way which is printing the circuit in a piece of paper using a laser printer and then transfer the toner to the copper board using an iron. I failed at least 5 times but at the end I got something decent, at least all the tracks where in place.
Below is a picture of the moment where I was soldering the module to the base board. This step of the process wasn't that difficult as I thought it would be. To my surprise it worked the first time, at that moment I connected it to an Arduino Uno.
Building the circuit
The first step is to check if the module can receive AT commands from the computer.
For my particular case, I had to add a logic level converter since each GPIO on the module only works at 3.3v. I bought a bunch of this cheap modules in Aliexpress, but to be honest the quality leaves much to be desired, so I guess I'll get a new ones from Sparkfun.
The schematic below shows how everything is connected.
You can see the cheap logic level converter module and my DIY base board for the bluetooth. Although the schematic shows a 1-pole dip switch, I'm using a 4-pole dip switch because is the only spare I have at the moment.
Communicate to the module through UART
I used picocom, which is a terminal emulator, to open the serial port. To install it just run the following command:
sudo apt install picocom
The module has two operation modes: Automatic connection and Order-Response. The former only receives a small subset of AT comamnds and the latter is used to send and receive AT commands. To set the latter mode in the module the PIO_11 (GPIO 34) has to be in high level before the module is on.
Once the pin is set high, run the following command to open the serial port:
picocom -b 38400 -c --omap crcrlf /dev/ttyUSB0
Send the following command to get the bluetooth's MAC address:
at+addr?
You should receive something similar to this:
+ADDR:98d3:31:b185dd
Pair HC-05 bluetooth module to the computer
For this step you need bluez-utils, which has many programs that interact with the bluetooth host controller. I didn't have to install anything since I believe this is already installed by default in the OS.
It may happens the bluetooth host controller is off or blocked. In my case it was blocked so to unblock it run the following:
sudo rfkill unblock 1
Verify that the bluetooth controller is finally up and running:
sudo hciconfig hci0
Important: before you pair the bluetooth module you have to set the pin PIO_11 (GPIO 34) to low level and then write the following command, you don't have to power off and on the module:
at+init
Press CTRL+A+X to exit picocom, later we will use it again but with a different baudrate.
Start the following program and the do scanning to discover the device:
sudo bluetoothctl
From the picture above we can observe the controller has found the device. To pair the computer with the bluetooth module run the following. The program will ask for the password, by default it is '1234':
To exit the program just write the command exit.
Connect to HC-05 bluetooth module
Run the following to create a serial device for the bluetooth module, it's necessary the bluetooth's module MAC address:
sudo rfcomm bind hci0 98:D3:31:B1:85:DD
If the commands runs successfully the character device /dev/rfcomm0 is created. In fact this device is a serial port. To send text to the module open the recently created serial port with the program picocom.
picocom -b 115200 --imap lfcrlf --omap crcrlf /dev/rfcomm0
After you open the serial port, the LED connected to PIO_09 (GPIO_32) turns on and the other LED blinks at a slower rate.
In another terminal open the serial port which is connected directly to the module to receive the characters:
picocom -b 115200 --imap lfcrlf --omap crcrlf /dev/ttyUSB0
The picture below shows the text sent from the computer and from the bluetooth module.
Notes about bluetooth kernel module
The RFCOMM is a protocol which emulates a serial port connection. When we run the command rfcomm bind we are telling to the kernel module net/bluetooth/rfcomm/tty to create a character device which is going to be used as a serial port.