To create bluetooth connection, we need to do 3 things:
- Wire the Bluetooth Module to our Arduino and enter AT+Commands to ensure everything is configured properly
- Ensure bluetooth connectivity is available on the Raspberry Pi and successfully connect to the Bluetooth Module powered by the Arduino
- Create a script on the Raspberry Pi to listen to and respond to Bluetooth Serial communication.
NOTE: Watch this video for more information, and for an explanation for why I chose the HC-05 over the HM10 knock-off Video: Arduino and Raspberry Pi Communication
Disclaimer: A lot of trial-and-error went into establishing the connection, so these steps may not be 100% complete; however, they are materially accurate, and should provide enough guidance to troubleshoot issues you encounter.
You can find my Arduino Code and the fritzing wiring diagram here
Information about HC05 accessing AT+Commands on HC05:
-
We do not need the state pin
-
Do not connect the VCC. Hold the button on the module while uploading your sketch. Once uploaded, then plug in the VCC. You should be in AT mode.
-
Advertised as 9600 BAUD rate but really 38400 when using AT Mode this way
-
This is considered “mini AT Mode” and certain commands are not available.
-
To use all of the commands, you either need the Arduino to control the module, or you can hold the button in while sending a command (easier method)
-
HC-05 commands are different that BLE CC41a (HM10 knockoff). Here is a table of commands:
This part can be really easy or tricky depending on your setup. If you are using a Raspberry Pi 3, you will already have a graphical Bluetooth interface that can help with this step. There are 3 things to remember:
- You need to know the MAC Address of your Bluetooth Module
- You need to pair to the Bluetooth Module
- You need to open an RFCOMM channel so Raspberry Pi knows it should be communicating. It is not enough to simply pair the devices, you need an RFCOMM channel open.
If you are using the graphical interface, you should be able to find your device and locate its unique MAC address. It will look something like: 98:D3:32:20:82:9D
If you do not have the graphical interface, or are having problems you can follow these steps:
- Type
sudo bluetoothctl
into a terminal and input the admin password if prompted - Type
agent on
and press enter. Then typedefault-agent
and press enter. - Type
scan on
and the raspberry pi will scan for broadcasting Bluetooth devices nearby. - Type
pair 98:D3:32:20:82:9D
replacing the MAC Address with that of your bluetooth module - Type
info 98:D3:32:20:82:9D
to see information about the device.
Once you have successfully paired the bluetooth module to your Raspberry Pi, you will need to bind to an RFCOMM channel before they can communicate:
sudo rfcomm bind hci0 98:D3:32:20:82:9D 1
will bind the bluetooth device to the RPi bluetooth modulesudo rfcomm release 0
will release the bluetooth module from rfcomm0
The Raspberry Pi code I used can be found in this here.