Create a Bluetooth Wireless Console Server with Raspberry Pi Zero W

I know it has been a very long time since I have updated this blog but I blame it on 2020. At any rate, I love the engagement that I have been receiving from my readers in spite of my lack of updates. One engagement that I recently had via email from a reader lit a new fire under me to post a follow up article to one of my previous blog posts. Specifically the email said:

Loved your article ‘Create a Wireless Console Server with Raspberry Pi Zero W’. I too would love an Airconsole but can’t see the $$$ and like your idea building a Pi Zero as a console server.

I like this idea but wonder if you can use it to connect from your laptop via Bluetooth to the Pi Zero so you can use your free WiFi on the laptop and use it separately?

I too hate sitting on the floor in closets, currently use a 10ft console cable.

– One of my valued blog readers

I gave this some thought and racked my brain as to why I never thought about using Bluetooth for this application before. Honestly, it is the better of the two wireless technologies for connecting to a Raspberry Pi Zero W because now I can keep my computer/phone connected to my Wi-Fi while still having access to my console server. Also, I no longer have to set up an access point and know the IP address of the console server, but can do an auto discovery via Bluetooth.

I embarked on this journey and after doing a short bit of research, found the following article that was the holy grail to getting this done. So before I provide my write up about how to accomplish this, I want to give a huge shout out to Patrick Hundal for providing this guide to further enhance the capabilities of using a Raspbery Pi Zero W as a console server.

Building the Console Server

Now I am not going to re-invent the wheel on this one. I have already created detailed instructions in my Create a Wireless Console Server with Raspberry Pi Zero W. Please review that article in its entirety for the basic set up around the Raspberry Pi Zero W. This section of the article will focus on the divergence from the original article and focus on the steps to take to make your Raspberry Pi Zero W a bluetooth accessible console server.

Setting The Pi’s Bluetooth Name

The first step to complete once the Raspberry Pi Zero W has been installed is to give it a name that it will display once you search for it via Bluetooth. To do this, we need to create a file called “/etc/machine-info” and set a constant by the name of “PRETTY_HOSTNAME” equal to whatever we want to call our PI. To accomplish this, you can use the following commands:

sudo touch /etc/machine-info
sudo echo "PRETTY_HOSTNAME=raspberrypi" > /etc/machine-info
Figure. – Screenshot of terminal showing contents of /etc/machine-info

For this purpose, I kept things simple and called it “raspberrypi”.

Enable Bluetooth Services

In order to make your Raspberry Pi Zero W advertise itself via Bluetooth, the services that support that function have to be enabled. To accomplish this, first we have to open up the “/lib/systemd/system/bluetooth.service” file in a text editor to be modified. To accomplish this, use the following command below:

sudo nano /lib/systemd/system/bluetooth.service
Figure. – Screenshot of the command to open the bluetooth.service file in nano text editor

Once you have this file open for editing in your text editor, update find the following section:

 ExecStart=/usr/lib/bluetooth/bluetoothd

And replace it with:

ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP
ExecStartPost=/bin/hciconfig hci0 piscan

The resulting file should look like the following:

[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth

[Service]
Type=dbus
BusName=org.bluez

ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP
ExecStartPost=/bin/hciconfig hci0 piscan

NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service
Figure. – Screenshot of the updated bluetooth.service file

Once this has been completed, you can save the file by pressing “Ctrl+X” and pressing “Y” to save your changes.

Creating a Bluetooth Terminal Service

Next, in order to be able to connect to the Raspberry Pi Zero W shell, a serial terminal emulator to be available via bluetooth. To accomplish this, start by creating the “/etc/systemd/system/rfcomm.service” file using the following command:

sudo touch /etc/systemd/system/rfcomm.service

Once this file has been created, add the following text below to it:

[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/rfcomm watch hci0 1 getty rfcomm0 115200 vt100

[Install]
WantedBy=multi-user.target
Figure. – Screenshot of the rfcomm.service within nano text editor

Once this has been completed, you can save the file by pressing “Ctrl+X” and pressing “Y” to save your changes.

Enable the RFComm Service

Once the RFComm service has been defined, enable and start it by using the following commands:

sudo systemctl enable rfcomm
sudo systemctl restart rfcomm

Once you have started this service, the Raspberry Pi Zero W should begin advertising itself for pairing via Bluetooth.

Connecting to the Raspberry Pi Zero W via Bluetooth

This section makes the assumption that you will be connecting from a Windows 10 PC. The instructions that follow this section can likely be adapted for other platforms as well but those platforms are not in scope for this article.

Pairing to the Raspberry Pi Zero W

To pair your Windows 10 computer to the Raspberry Pi, follow the instructions below:

1. Identify the “Type hear to search” at the bottom of the screen

Figure. – Screenshot of the Windows 10 Search Bar

2. Type “bluetooth” | Select “Bluetooth and Other Devices Settings”

Figure. – Screenshot of the “Bluetooth and Other Devices Settings” option

3. Click on “Add Bluetooth or other device”

Figure. – Screenshot of the bluetooth settings dialog box

4. Click on “Bluetooth”

Figure. – Screenshot of the “Add a device” dialog box highlighting “Bluetooth” selection

5. Identify the bluetooth name (i.e. raspberrypi) of your Raspberry Pi Zero W [Please Note: It can take up to one minute for the raspberrypi to be detected by your computer.]

Figure. – Screenshot of the “Add a device” dialog box showing “raspberrypi”

6. Click on the “rasberrypi” and confirm setup

Figure. – Screenshot of “Add a device” dialog box showing successful pairing to raspberry pi

Confirm COM port for Bluetooth Connectivity

Once the Raspberry Pi Zero W has successfully been connected to the Windows computer, a COM port needs to be identified for connectivity to the terminal server on the Raspberry Pi. To determine this, follow the instructions below:

1. Under the “Bluetooth & other devices” dialog box | Scroll to the Bottom | Select “More Bluetooth Options”

Figure. – Screenshot of the “Bluetooth & other devices” dialog box highlighting “More Bluetooth options”

2. When the “Bluetooth Settings” dialog box appears | Click on the “COM Ports” tab

Figure. – Screenshot of the “Bluetooth Settings” dialog box with “COM Ports” highlighted

3. Identify (and take note of) the COM port used for the “outgoing” communication to the Raspberry Pi Zero W. [Please Note: The COM port should be set up automatically, if it does not, un-pair and re-pair your Raspberry Pi Zero W to the Windows PC]

Figure. – Screenshot of the COM Port used for outgoing communication to Raspberry Pi

4. Click “OK” to close the dialog box

Connecting to the Raspberry Pi Console via Terminal Emulator (i.e. Putty)

Once you have identified the COM port that the Raspberry Pi Zero W is connected on, you can use a terminal emulator to connect to it. In this example, Putty is used.

1. Open PuTTY | Select “Serial”

Figure. – Screenshot of PuTTY application with “Serial” option highlighted

2. Set the serial connectivity options depending on what was mapped on Windows | Click “Open”:

In this example:

COM = COM6

Speed = 115200

Figure. – Screenshot of PuTTY with COM settings defined to connect to Raspberry Pi

3. Once the console pops up, log in with the default credentials for Raspberry Pi (pi/raspberry)

Figure. – Screenshot of PuTTY after logging into Raspberry Pi via Bluetooth

At this point, you can continue from the rest of the procedures laid out in the Create a Wireless Console Server with Raspberry Pi Zero W to connect to the console of whatever device you want to manage.

If you have made it this far in the article, here’s your reward. As part of the original article provided Patrick Hundal, he provides a script that you can create and execute on the Raspberry Pi to automatically handle many of the configuration tasks for the Raspberry Pi. Here is a copy of his script below:

#!/bin/bash -e

#Edit the display name of the RaspberryPi so you can distinguish
#your unit from others in the Bluetooth console
#(very useful in a class setting)

echo PRETTY_HOSTNAME=raspberrypi > /etc/machine-info

# Edit /lib/systemd/system/bluetooth.service to enable BT services
sudo sed -i: 's|^Exec.*toothd$| \
ExecStart=/usr/lib/bluetooth/bluetoothd -C \
ExecStartPost=/usr/bin/sdptool add SP \
ExecStartPost=/bin/hciconfig hci0 piscan \
|g' /lib/systemd/system/bluetooth.service

# create /etc/systemd/system/rfcomm.service to enable 
# the Bluetooth serial port from systemctl
sudo cat <<EOF | sudo tee /etc/systemd/system/rfcomm.service > /dev/null
[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/rfcomm watch hci0 1 getty rfcomm0 115200 vt100 -a pi

[Install]
WantedBy=multi-user.target
EOF

# enable the new rfcomm service
sudo systemctl enable rfcomm

# start the rfcomm service
sudo systemctl restart rfcomm

I hope this article further enhances the capabilities of your Raspberry Pi Zero W. Please let me know your thoughts on this in the comments below!

5 1 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ken Skier

Excellent! Beautifully detailed and clear.
I will try this to allow me to interact eith my headless pi zero w’s in the field, from an Android phone…
…without having to start a mobile hotspot on my AndroId phone.
Thank you!