Linux tip, Fedora tip / howto: setting up an OBD2 Bluetooth adapter

back to notes and tips index

Try my online puzzle page with Calcudoku, Killer Sudoku and online Sudoku.

setting up an OBD2 Bluetooth adapter

My car was behaving a bit more sluggishly than I expected, so I decided to investigate. The goal was to plug an OBD2 bluetooth adapter into the OBD2 port of my car (a 2004 Opel/Vauxhall Astra 1.6) and make it talk to an Acer Aspire One laptop running Linux (CentOS 6.3).

The end goal is to get graphs like this one (click on it for a larger version):

We'll work our way from the car "upwards":

  1. the car
  2. the bluetooth adapter
  3. setting up bluetooth
  4. connecting to the adapter
  5. setting up ScanTool software
  6. running ScanTool
  7. processing logged data

1. The car

Most recent cars will have an OBD2 connector. Here's what it looks like on my car:

Check your service manual or look online to find the location of the connector in your car. In my case I had to use a flat head screwdriver to flip open a panel below the handbrake to expose the connector.

2. The OBD2 bluetooth adapter

There are very many of these. If you're looking to use one of the several iPad apps for connecting to your car, you may want to look into a Wifi OBD2 adapter instead.

After browsing some reviews, I ended up getting this one: "Supper Mini Bluetooth Car Code Reader / ELM327 Power 2" (for £11.95).

This is what it looks like (note the small bluetooth symbol after the "POWER2"):

And this is what it looks like connected (the red light is always on):

Note: just now I found that my car battery is dead, after not having used the car for a few days, and left the adapter in. I can't be sure it was the adapter that drained the battery (I won't leave it in anymore though).

3. Getting bluetooth to work on my Linux laptop

My very simple Acer Aspire One laptop (1GB memory, Intel Atom 1.6 GHz processor, running CentOS 6.3) does not have bluetooth, so I first had to get a USB bluetooth adapter. I bought this Belkin mini bluetooth adapter for about 13 pounds. Note that it works fine under Linux (the product page only mentions Windows). If you get a different/newer adapter, do verify that it works under Linux (often people mention this in product reviews).

The adapter is tiny (those squares are 5mm x 5mm):

After connecting it, lsusb shows this (the various numbers may be different for you):
Bus 002 Device 009: ID 050d:016a Belkin Components Bluetooth Mini Dongle

I had most success with the blueman bluetooth software. To install, run:
yum install blueman
as root.

The gnome bluetooth software only gave me trouble. You can ditch it by running (as root):
rpm -e gnome-bluetooth gnome-bluetooth-libs --nodeps

Once installed, there should appear a small bluetooth icon in your panel (you can double check you're using the Blueman applet by selecting the "About" option).

4. Connecting to the bluetooth adapter

Get into your car and turn the ignition key to "II" (or whatever setting powers the car without starting the engine).

Turn on the laptop, and once started up and logged in, right-click the bluetooth icon in the panel and select "Setup new device...", then click the "Forward" button.

In the dialog that appears next, a device "OBDII" should show up in the list. Select it, and then click "Forward".

Next, select "Use Custom Passkey", and type "1234" as the key (this number may be different for your OBD2 adapter):

Next, select "Serial Port":

After that, you should get a confirmation like this:

5. Setting up software to read the OBD2 information

What we now need is a piece of software that supports the OBDII protocol and allows us to log the incoming values.

I managed this with the "ScanTool" program, downloaded from the downloads page. Get the source archive.

There may be other, better solutions by now (send me a note using the form below if you know of one :-). For example, freediag looks interesting.

Create a directory for it, and unzip the source:
mkdir scantool
cd scantool
unzip (run yum install unzip as root to install it if needed)

Some changes need to be made to the makefile and source code:

  • in the makefile, replace the two lines that have AL_LIBS = -lalleg with AL_LIBS = `allegro-config --libs` (note the "backticks", maybe you need to yum install allegro allegro-devel as well)
  • before the ifdef MINGDIR (line 17) add a line DEFINES += -DTERMIOS
  • change line 29: LIBS = -ldzcom $(AL_LIBS) to LIBS = $(AL_LIBS)
  • change line 43: BIN = ScanTool.exe to BIN = ScanTool
You can download a copy of the makefile with these changes applied.

In the file globals.h, uncomment line 9:
//#define LOG_COMMS // Uncomment this line to enable serial comm logging

I fixed a bug in the logging code in main.c, and added the logging of timestamps. Download a copy of the updated main.c.

The file serial.c must be changed so it uses termios: there's a patch for this on the ubuntu forums. Alternatively, download an already patched version of serial.c.

Now you should be good to compile, so run make (send me a note if you get any compilation errors).

6. Running ScanTool

Because ScanTool expects to read its serial data from /dev/ttyS3, and the bluetooth software created a serial device /dev/rfcomm0, we should first point one to the other. As root, do:
cd /dev
rm -f ttyS3
ln -s rfcomm0 ttyS3
chmod 766 rfcomm0

When you run ScanTool, it'll show you a warning (just click "CONTINUE"):

Next, click "Sensor Data" to see screens like this one (as you can see from the numbers, I didn't start the engine yet):

7. Processing logged data

ScanTool logs to a file comm_log.txt. With my timestamp update, log lines now look like this:

[Sat Feb 14 21:54:29 2015][TX]010D[/TX]
[Sat Feb 14 21:54:29 2015][RX]41 0D 00 >[/RX]
[Sat Feb 14 21:54:29 2015][TX]0104[/TX]
[Sat Feb 14 21:54:30 2015][RX]41 04 00 >[/RX]
[Sat Feb 14 21:54:30 2015][TX]010B[/TX]
[Sat Feb 14 21:54:30 2015][RX]41 0B 65 >[/RX]
[Sat Feb 14 21:54:30 2015][TX]0110[/TX]
[Sat Feb 14 21:54:31 2015][RX]41 10 00 00 >[/RX]

You can see the incoming data rate is about 2 pieces of information a second. You can set which data ScanTool should query, so bear in mind that if you're reading a lot of them, the resulting resolution will not be great (conversely, if you'd like to log the engine RPM twice a second, for example, enable the reading of that sensor only).

I wrote a small Perl script: that converts the logfile to a data file that gnuplot can use.
Note: edit the date in the regular expression on line 16. Run it with: comm_log.txt
which produces an output file trip.dat. You can then create a graph like the one at the top of this page with:
(download here). This'll create a trip.png image file with the graph.

← back to notes and tips index
Please do not copy the text of this tip (© Patrick Min) to your web site.