BrewPi on Bluetooth

Over on Homebrew Talk, @day_trippr shared a way to connect the Arduino to your Raspberry Pi over Bluetooth. That was December 14, 2014 and a lot pf people have been successful with that. I personally found a couple of the steps a little challenging. To be fair, that’s a personal shortcoming and not one of the original article. Still I searched for a way to make it a little easier and I think I’ve found it. I’ll share here what I’ve come up with in the hopes that someone else will find it useful.

This tutorial is for Windows users. I expect Mac and Linux users will have to use the “old fashioned” way.

On Amazon I found a package with an HC-05 Bluetooth Module, a case for that module, Dupont jumpers, and a CP2102 USB to TTL Converter for $12.99 on Prime. That seems like a pretty good deal and provides some pretty good value for a person new to it. When you get a drawer full of “crap”, the Dupont wires certainly have less value, but I kinda like the case. So, I’m writing this with that USB to TTL Converter being used:

You can find it on Amazon here. Buy that and come back.

Hardware Setup

Without further adieu, let’s get started. Don’t plug the adapter in your computer yet. You’ll need to download some software to get going. You’ll probably need the CP2102 drivers, and to follow this you will definitely need the DSD TECH Bluetooth Tools Software. Download and install both of these (actually the “Bluetooth Tools” doesn’t install, just unzip to a handy directory):

Don’t plug the converter into the USB on your computer yet. Connect the TTL converter and the BT module like so:

I actually used the 3.3v the first time, the module will run on 3.3-6v for VIN I believe, but the data channel needs to be 3.3 volts. No worries, the USB to TTL converter handles that for you. Here’s the connections in text form:

Converter PinHC-05 Pin
3v3VIN
TXDRXD
RXDTXD
GNDGND

Basically, remember to connect transmit to receive and vice versa. When you are ready and the jumpers are connected properly, connect the device and set programming mode (called “AT mode”) by doing the following:

Input low level to PIN34, supply power to the module, input high level to PIN34, then the module will enter to AT mode.

Just kidding, I have no idea what that means either. I promised this would be for mortals. 🙂

  1. Hold the button down on the Bluetooth Module (note about some of the modules sold elsewhere which are insulated with shrink-tube: You may have to cut the tube away from the button in order for it to function correctly)
  2. Plug in the USB to TTL Converter
  3. The light will come on for about a second, and then shut off
  4. Let go of the button on the Bluetooth Module

If you have done this correctly, the module should be flashing on and off slowly. Done incorrectly, the device will be flashing rapidly. If you don’t get it the first time, unplug the converter and try again.

Now run the “DSD TECH Bluetooth Tools Software” you unzipped. You will be executing “SHTester.exe”.

  1. Select the last tab which is “HC-05”.
  2. Drop down the UART box and select the port. On most systems there will be only one. If you have some other COM port device installed there may be more than one listed. If this is the case you will need to open Windows’ “Device Manager”, look under “Ports (COM & LPT)”, and see what COM port is associated with the “Silicon Labs CP210x” device.
  3. Set Baud Rate to 38400, then click “Open”.
  4. Click the “Test” button, and in the status windows you will see the “AT” command being sent and “OK” received. This will not work unless you are in AT mode (slow flashing). If you don’t get anything back it’s likely a Baud Rate mismatch. Click on “Close”, select a different Baud Rate, “Open” and try testing again till you get an “OK” back.
  5. Now set “Bluetooth Name” to whatever you’d like it to be, Baud Rate to 57600, PIN (really no reason to change the PIN but if you do, remember it), and set Role to “Slave”. Click “Set” after each change.
  6. Now “Close” the UART up top again with the red “X” and you can close the app.

If you’d like, you can test the device over Bluetooth from your computer:

  • Unplug the USB dongle and unplug the two TX and RX wires.
  • Plug it back in and now the Bluetooth Module will be flashing rapidly indicating it’s ready to be paired.
  • Open the Windows Bluetooth settings and click “Add a device.”
  • Select the name you gave the module above and enter the PIN you used.
  • Click “Connect” and if successful, the device will flash briefly every two seconds or so.

Congratulations! You can unplug the device now, it’s ready to be connected to your Arduino.

This is where you can go a couple different ways. In order to connect to your Arduino you do need to either use a voltage splitter to step the voltage down to 3.3v, or if you are using @CadiBrewer’s shield, this is done for you. This has to be done because the logic on the HC-05 module can only handle 3.3 volts, and the TX channel on your Arduino is at 5 volts.

If you are using the shield version 1.1, connect as follows:

Shield PinHC-05 Pin
VCCVIN
TXDRXD
RXDTXD
GNDGND

If you are using the shield version 1.2, reportedly the RX/TX line up directly so connect as follows:

Shield PinHC-05 Pin
VCCVIN
TXDTXD
RXDRXD
GNDGND

If you are going all rogue and want to do it without a shield, at least you were able to skip the whole Arduino sketch setup thing initially. You will need the following:

  • 1 x 1K ohm resistor (1/8W axial)
  • 1 x 2K ohm resistor (1/8W axial)
  • Assorted Dupont jumpers

Wire them according to this diagram so that the voltage into the RXD pin on the module is cut to 3.3v:

Software Setup

Setting this all up changed since the article @day_trippr posted. I am NOT sure when this changed. @day_trippr is running Wheezy and Jessie and his original article uses the “old” way of setting up a Bluetooth device. I’m using Stretch with bluetoothd 5.43 and the method by which you set up /dev/rfcomm* devices with the rfcomm.conf file in /etc/bluetooth in Wheezy and Jessie is no longer supported. I’m sure someone somewhere could tell me WHY this is better. I’m sure we’d hear some crap about “cloud-enabled” like they tell me with all my favorite Ubuntu items that no longer work.

Anyway, here’s how you can get the devices enabled with Stretch. To start with, make sure your HC-05/6 is powered up in its production configuration (i.e. baud rate and slave mode as detailed above). At this point it’s probably flashing 2-3 times per second. Then, prove to yourself that there’s no rfcomm* devices:

pi@brewpi:~ $ ls -al /dev/rfc*
ls: cannot access '/dev/rfc*': No such file or directory

Next, execute the new CLI for bluez, “bluetoothctl”:

pi@brewpi:~ $ sudo bluetoothctl
[NEW] Controller XX:XX:XX:XX:XX:XX brewpi [default]

The controller listed is your local Raspberry Pi’s controller. We need to turn on and configure the “agent” which is the process that allows you to enter the pairing codes with Bluetooth devices when required:

[bluetooth] # agent on
Agent registered
[bluetooth]# default-agent
Default agent request successful

The next thing to do is allow the system to scan for local devices. If you have a “busy” area bluetooth-wise, you may get a lot of spam. Just let it run about 10 seconds and if you are living a clean life you will see your target Bluetooth device and it’s MAC address. After that you can turn off the scan.

[bluetooth]# scan on
Discovery started
[CHG] Controller XX:XX:XX:XX:XX:XX Discovering: yes
[NEW] Device XX:XX:XX:XX:XX:XX XX-XX-XX-XX-XX-XX
[NEW] Device XX:XX:XX:XX:XX:XX [TV] Living room
[NEW] Device XX:XX:XX:XX:XX:XX Chamber 1
[…]
[bluetooth]# scan off
[…]
Discovery stopped
[CHG] Controller XX:XX:XX:XX:XX:XX Discovering: no

You may have to scroll up to find it, but if you gave your HC-05 a friendly name when you set it up (“Chamber 1” in my case), you should see the MAC followed by that name. Next, you will pair with your device with the pair command followed by the MAC address. It will prompt you to enter the code which is generally 1234 or 0000 unless you changed it:

[bluetooth]# pair XX:XX:XX:XX:XX:XX
Attempting to pair with XX:XX:XX:XX:XX:XX
[CHG] Device XX:XX:XX:XX:XX:XX Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX ServicesResolved: yes
[CHG] Device XX:XX:XX:XX:XX:XX Paired: yes
Pairing successful
[CHG] Device XX:XX:XX:XX:XX:XX ServicesResolved: no
[CHG] Device XX:XX:XX:XX:XX:XX Connected: no

Next you will “trust” the HC-05:

[bluetooth]# trust XX:XX:XX:XX:XX:XX
[CHG] Device XX:XX:XX:XX:XX:XX Trusted: yes
Changing XX:XX:XX:XX:XX:XX trust succeeded

At this point your BT dongle is probably flashing once every two seconds. It’s paired and trusted as you can see by issuing the ‘info’ command:

[bluetooth]# info XX:XX:XX:XX:XX:XX
Device XX:XX:XX:XX:XX:XX
Name: Chamber 1
Alias: Chamber 1
Class: 0x001f00
Paired: yes
Trusted: yes
Blocked: no
Connected: no
LegacyPairing: yes
UUID: Serial Port (00001101-0000-1000-8000-00805f9b34fb)

I noticed that if you power-cycle the Bluetooth device at this point, it will go back to flashing 2-3 times/second but that doesn’t seem to affect things. You can exit or quit back to the shell prompt:

[bluetooth]# exit
Agent unregistered
[DEL] Controller XX:XX:XX:XX:XX:XX brewpi [default]
pi@brewpi:~ $

So, it’s paired and trusted but still not visible in the device list as you can see if you list it out:

pi@brewpi:~ $ ls -al /dev/rfc*
ls: cannot access '/dev/rfc*': No such file or directory

To add it to the device list, we need to bind it to a device with the following command:

pi@brewpi:~ $ sudo rfcomm bind 0 XX:XX:XX:XX:XX:XX 1 > /dev/null 2>&1 &
[1] 3345

It seems like the command runs till something touches the device (which is why we background it with the trailing ‘&’.) In that command the ‘0’ is the rfcomm device we want to assign (i.e. /dev/rfcomm0), the MAC address should be obvious, and the 1 is the channel. Unless you know why you want to change the channel, use ‘1’. Now prove there is a device in the list (and see the background process exit):

pi@brewpi:~ $ ls -al /dev/rfc*
crw-rw---- 1 root dialout 216, 0 Mar 16 13:31 /dev/rfcomm0
[1]+ Done sudo rfcomm bind 0 XX:XX:XX:XX:XX:XX 1 > /dev/null 2>&1
pi@brewpi:~ $

Power-cycling the BT device at this point proves the device will reconnect after a power failure after about 10 seconds. What does not happen however is the connection will not re-establish to the device after a Raspberry Pi reboot or power failure. We need a way to issue the rfcomm command after every reboot (and after the bluetoothd starts.) Create a file named ‘rfcomm0.service’ in the ‘/etc/systemd/system/’ directory:

sudo nano /etc/systemd/system/rfcomm0.service

Add the following information:

[Unit]
After=bluetooth.service

[Service]
ExecStart=/usr/bin/rfcomm bind 0 XX:XX:XX:XX:XX:XX 1 > /dev/null 2>&1 &

[Install]
WantedBy=default.target

This will allow systemd to execute the rfcomm command binding that MAC to rfcomm0 after the bluetooth.service starts. To enable this unit file, issue the commands:

pi@brewpi:~ $ sudo chown root:root /etc/systemd/system/rfcomm0.service
pi@brewpi:~ $ sudo chmod 664 /etc/systemd/system/rfcomm0.service
pi@brewpi:~ $ sudo systemctl daemon-reload
pi@brewpi:~ $ sudo systemctl enable rfcomm0
Created symlink /etc/systemd/system/default.target.wants/rfcomm0.service → /etc/systemd/system/rfcomm0.service.
pi@brewpi:~ $

You can start it with the ‘sudo systemctl start rfcomm0’ command, but there’s no reason to since it’s already bound (assuming you ran the bind command above and have not rebooted – it will not hurt if you run it again). If you check the status of this daemon after system reboot it will not be running, because it ran once and exited – which is all we needed it to do:

pi@brewpi:~ $ sudo systemctl status rfcomm0
● rfcomm0.service
Loaded: loaded (/etc/systemd/system/rfcomm0.service; enabled; vendor preset:
Active: inactive (dead) since Sat 2019-03-16 13:43:59 CDT; 40s ago
Process: 290 ExecStart=/usr/bin/rfcomm bind 0 XX:XX:XX:XX:XX:XX 1 > /dev/null 2>&1 & (code=exited, status=0/SUCCESS)
Main PID: 290 (code=exited, status=0/SUCCESS)
Mar 16 13:43:59 brewpi systemd[1]: Started rfcomm0.service.

That’s how you set up your BT devices now in Stretch! You’ll notice that using the graphical interface is also not needed. Using this method, no additional packages are required as a matter of fact, all the packages are part of the stock Raspbian Stretch distribution.

If you have multiple Bluetooth devices, you’d change the above steps using 1, 2, 3, etc., instead of 0. You would also need a systemd unit file for each device named appropriately.

Testing

If you want to give it a test outside of BrewPi, you will need to install a program called “screens”:

sudo apt install screen

Next, run ‘screen’ and connect to that device with the proper baud rate:

sudo screen /dev/rfcomm0 57600 

If you are living a clean life, you are now connected to your Arduino. Issue the command ‘l’ (lower-case L) and the controller should print the LCD screen information; something like this:

L:["Mode   Off          ","Beer   60.7  --.- �F","Fridge 61.2  --.- �F","Idling for     03m08"

You need to type “ctrl-a” and then “k” to kill the screen session.

Configure BrewPi

In order to connect this to BrewPi, you will edit (or create) your config.cnf file to look something like the following:

scriptPath = /home/brewpi
wwwPath = /var/www/html
port = /dev/rfcomm0

This works in Legacy BrewPi as well as all versions of BrewPi Remix.