What is the OBD port and how to find it
The OBD (On Board Diagnostics) port is used by garages to test the electronics of a car. It is mandatory on every car since the 90s. It is easily accessible and generally located under the steering wheel. In order to find the port in my car (a Mercedes Benz B Class), I just googled "OBD port Mercedes B class" and found several pages providing instructions including pictures and even a couple of videos.
Finding the OBD scanner
There are several cheap commercial readers available, I decided to go for a bluetooth enabled ELM327
Reading from the OBD scanner in Python
As I am working on a Raspberry PI I needed to develop in Python. I have found the code developed by martinohanlon which is available on github and that is able to read from the OBD scanner. The code is created for a motorbike but it works for cars as wells. Also it was originally created for a wired scanner. However if you pair the scanner with the Pi as explained below, it will also work with bluetooth.
Preparing the Pi
The Pi will need a bluetooth scanner. There are several commercial ones. I went for a USB one. Installing it is very easy. Just plug, update the libraries and play. ModPy provides an excellent tutorial. I then connected a GPS to the pi. A brilliant tutorial is provided by @dmandle.
Pairing the PI and the OBD scanner
As mentioned in the ModPy's tutorial, the bluetooth OBD adaptor needs pairing with the Pi. To get the address of the adaptor run in the Pi's shell:
# hcitool scan
it will return:
# Scanning ...
# <bluetooth address> OBDII
The scanner will be called OBDII (or similar name). Its bluetooth address will be something like 00:0D:18:3A:67:89.
To pair it with the Pi, I used the following command:
# echo 1234|bluez-simple-agent hci0 <bluetooth address>
Note: this supposes that the PIN code of the scanner is 1234, which is generally the case. If different, change appropriately.
If the Pi complains that bluez-simple-agent is not found, then go back to the ModPy's tutorial and install bluez (with sudo apt-get install bluetooth bluez-utils blueman).
In some adaptors the bluetooth connection drops after few seconds after pairing. if so, in a separate shell run
# sudo rfcomm connect 0 <bluetoothaddress> 1
e.g. sudo rfcomm connect 0 00:0D:18:3A:67:89 1
The command will regularly poll the scanner so to avoid dropping the connection. Please note that the command will hang the shell, so add an '&' if you do not want that.
Putting everything together
At this point I just had to make sure that every time the OBD port was polled, the GPS was polled as well. The results were saved as a csv log file. I then created a python script taking the csv file and turning it into a kml file as required by Google Earth. Here is an example of the result (speed is in miles per hour).