Connecting your Bluetooth OBDII Adapter (or other serial port adapters) to a Raspberry Pi 3
So, you decided to build your own car monitoring system based on Raspberry Pi and an OBDII adapter. You realize the first step of getting such a system up and running is connecting your shiny new OBDII Bluetooth adapter to the Pi. After spending a good few hours on this very issue, I hope the following instructions will help you get up and running much faster then it took me.
Step 1: Get Paired
Bluez5.x (Raspbian’s default bluetooth manager) has a whole new CLI utility for managing connections, bluetoothctl, and it’s here to replace the good old bluez-utils package. Pairing a device using bluetoothctl is actually quite a straight forward process:
- SSH to your Pi (or open a terminal window from the Pi GUI) and run bluetoothctl.
- Turn the bluetooth controller on by typing power on and press enter.
- Enable bluetoothctl’s pairing mode by turning on the default agent. Type agent on and press enter, then type default-agent and press enter again.
- Scan for nearby devices by typing scan on and press enter once again. The mac addresses of all the devices your pi can find will start appearing, usually with the nickname of the device next to it.
- Once you spot your OBDII device on the scanned list, you are ready for the holy grail of bluetooth connections —pairing. To pair your device simply type pair followed by the mac address of your device.
- Lastly, type trust followed by the mac address of your device to mark it as trust-able, which will allow us to auto connect to this device on startup in a later stage.
To sum it up, here is the entire process in order:
Step 2: Getting Connected (and send some data just for fun)
At this point, you might be thinking to yourself, “Johnny, why are you bothering me with this post? Since I’m done pairing my device, I’ll just type connect, followed by the mac address of my device and be done with all of this?”
To this I can only say:
If you try using connect you’ll get back a lovely error which reads:
Failed to connect: org.bluez.Error.NotAvailable
Since the OBDII adapter uses serial port, we have to use rfcomm to bind the adapter. To do that, just type the following outside of bluetoothctl:
Now that our adapter is bound, we can communicate with it by using screen or cat.
Let’s take this for a spin! Below is a small example of me requesting the current RPM of my car:
As you might have noticed, the response to my RPM request (010C) is 2 lines of hex data. I’ll dive into explaining the OBDII hex responses in another post, but for now its enough to see that the communication between the Pi and the OBDII adapter works.
Step 3: Auto Bind on Boot
To make the Bluetooth adapter automatically bound on boot, just add the rfcomm bind command from step 2 to the end of /etc/rc.local file and reboot your Pi.
This are the first steps I took when building my Raspberry Pi based car diagnostic system. If you find this interesting and want me to document the rest of the system, please let me know in the comments. 😇