Monday, November 3, 2014

HC-06 Bluetooth Serial Module

I've been messing around with the ESP8266 module for the last little while - starting with the SDK demos, it's fairly easy to hack the code to connect automatically to a given router and starting sending data over the network to a given target.  Even though only UART and GPIO is routed out in the PCB, the part has support for i2c and SPI and would make a great starting point for a networked device.

Unfortunately, for the specific task I had in mind - getting data out from battery monitor with power budget measured in tens of mA - it doesn't quite fit the bill.  The power consumption is about a factor of 10 too high, and bandwidth requirements (2400 baud uni-directional) and range requirements are also a little off.  So, time to look seriously down the Bluetooth path.  After being amazed with the Chinese ingenuity in ESP8266, I figured I'd look there before grabbing a $35 bluetooth serial module and I came across the world of HC-0# series of serial port profile bluetooth modules available from eBay for low price of about $5.  I ended up ordering the HC-06 which is a consumer level bluetooth serial port slave device, which acts as a transparent serial to bluetooth bridge once paired.

HC-06 Wired Up
There seems to be several different manufacturers of the HC-06 part, none of which provides usable documentation. :p  Fortunately, there are sites that document their experiences with the parts that I've found useful.  In terms of wiring it up, it seems like the module can accept 5V power, though the serial rx/tx lines are limited to 3.3V logic.  My HC-06 module came pre-configured as 9600 8n1 and I created a simple voltage divider to connect 5V serial output to the 3.3V input on HC-06.

HC-0# family of devices also use AT command set, but the HC-06 I found has an interesting quirk - the page above touch on it for a bit, but instead of using \r\n as end-of-command, the HC-06 requires you to enter the entire command within a certain timeframe for it to be processed correctly.  This in essence means that in the serial console, you need to copy and paste the commands (AT+VERSION, for example) sans \r\n to allow commands to be accepted.  Since there's no echo, you basically then sit around waiting for the response to appear (OK, etc).  It's a little awkward, but the AT command based setup just has to be done once, I suppose.  All I had to do is to run AT+BAUD2 to set serial port to 2400 baud to talk to the battery monitor.

I've gotten a NUC to serve as a smart home hub - this was the first time manually pairing a bluetooth device to Linux computer all from command line, and it was surprisingly annoying - there seems to be multiple entry points into bluez - having run through them, I'm surprised that bluetooth works as well as it does on Android!

First, I had to see if I could see the bluetooth device and get the address:
daniel@smarthub:~$ hcitool scan
Scanning ...
20:14:04:29:34:92 HC-06

I then had to edit the rfcomm configuration file to add the entry:
daniel@smarthub:~$ cat /etc/bluetooth/rfcomm.conf
#
# RFCOMM configuration file.
#
rfcomm0 {
        # Automatically bind the device at startup
        bind yes;
        # Bluetooth address of the device
        device 20:14:04:29:34:92;
        # RFCOMM channel for the connection
        channel 1;
        # Description of the connection
        comment "HC-06 Serial Dongle";
}
HC-06 ships with default pairing password of "1234", and I had to run the following command to get the pairing prompt:
bluez-simple-agent hci0 20:14:04:29:34:92
Then start rfcomm:
daniel@smarthub:~$ sudo rfcomm bind rfcomm0
daniel@smarthub:~$ ls -al /dev/rfcomm0
crw-rw---- 1 root dialout 216, 0 Nov  2 23:40 /dev/rfcomm0
Add my user to dialout group so that /dev/rfcomm0 can be accessed without sudo:
daniel@smarthub:~$ cat /etc/group | grep dialout
dialout:x:20:daniel
Then finally connect!
daniel@smarthub:~$ screen /dev/rfcomm0 2400

I still haven't quite figured out how to collect and store all the data, but it seems like at least the pieces are (finally) coming together!  And as for the Broadcom WICED SENSE module - I might simply end up using it as a fancy and somewhat limited weather station - it's got temperature and pressure covered, but maybe I can glue it on a wind wheel and use the accelerometer and gyro to figure out the wind speed. :-)

No comments:

Post a Comment