Read in OBD2-values to the Raspberry Pi with Python

Connecting Raspberry Pi to OBD2

The Raspberry Pi can be connected to the OBD2 connector of the car with an EML327 compatible OBD2-USB or OBD2-Bluetooth connector.

I chose the USB version because the Raspberry Pi will be integrated into the car later.

Establish connection

The ELM327 is an “OBD to RS232 Interpreter”.
After opening a serial port, the communication with the vehicle is the same as with a modem.

Fortunately, there is already a Python lib that deals with this and interprets the answer from the OBD2 port (https://github.com/peterh/pyobd)

However, I have based my work on this fork: https://github.com/Pbartek/pyobd-pi/blob/master/obd_io.py
The corresponding project page (https://www.instructables.com/id/OBD-Pi/) describes how to make the script executable.

The only significant dependency is python-serial. I’m using a USB connector, so I do not need bluetooth bluez-utils blueman.
I also don’t need the packages python-wxgtk2.8 python-wxtools wx2.8-i18n libwxgtk2.8-dev because I don’t use graphical output.

Here the fork of Pbartek differs significantly from the original of peterh: in the original, it references the package wx in obd.io.py.
The fork of Pbartek removes these dependencies and contains the script obd_recorder.py. An excellent starting point to understand how communication works.

The fork of Pbartek removes these dependencies and contains the script obd_recorder.py. An excellent starting point to understand how communication works.

The following steps are necessary to install the package Pbartek on the Raspberry Pi:

Optional:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get autoremove
sudo reboot

Installs python-serial and the packages for bluetooth and the graphical output (if needed):

sudo apt-get install python-serial
sudo apt-get install bluetooth bluez-utils blueman
sudo apt-get install python-wxgtk2.8 python-wxtools wx2.8-i18n libwxgtk2.8-dev

Installs the GIT client and then fetches the package.
(Not necessary if the package is loaded manually and transferred to the Raspberry Pi):

sudo apt-get install git-core
sudo reboot

git clone https://github.com/Pbartek/pyobd-pi.git

Communication

For the communication between Python and the OBD2 port of the car the serial port of the Raspberry Pi is opened first. The Init method of the class OBDPort (obd_io.py) defines how the port is opened and how communication to the ELM327 is then established.

The meaning of the commands can be read in the ELM327 documentation.

pyOBD uses the following command sequence for initialization:

  1. atz (reset)
  2. ate0 (echo off)
  3. 0100 (supported monitor ids)

The first attempts have always failed.
The answer after sending 0100 was:

0100SEARCHING……UNABLE TO CONNECT

The fault may occur if the ignition on the vehicle is not set to at least level 2. In my case, however, the error message always came.
It only worked after I activated the automatic protocol recognition following the ATZ command.

I’ve added something to the whole sequence:

  1. atz (reset)
  2. ate1 (echo on)
  3. atsp0 (0 = automatic protocol detection)
  4. attp0 (0 = try protocol)
  5. atdp (display protocol)
  6. atdpn (protocol used by car)
  7. 0100 (supported monitor ids)

After that, I have received the following response:

0100SEARCHING…41 00 BE 1F B0 10 68

After establishing the connection, the commands are sent according to the OBD2 command list (https://en.wikipedia.org/wiki/OBD-II_PIDs) and the result can be interpreted.

If you have similar plans, I recommend that you familiarize yourself with the OBD2 protocol and compare the sent/received values with the OBD-II PID description. This is the only way to get a feeling for what is actually happening here.

Leave a Reply

Your email address will not be published. Required fields are marked *