Bluetooth BLE and Python - hwthomas/ccs-chademo GitHub Wiki

Overview

These pages gather information about using Bluetooth Low Energy (BLE) on the Raspberry Pi4b with Python as the programming language.

The primary need for using BLE is to communicate to an ELM327 OBD-II dongle (in my case, an LELink2 OBD-II from www.outdoor-apps.com) which gives access to the Nissan Leaf Car-CAN bus through the OBD port, and provides an alternative means of reading information from the Li-Ion Battery Controller (LBC) instead of a direct CAN-bus link through the CHAdeMO port.

The information about BLE (Bluetooth Version 4.0+), which is somewhat different to earlier versions, is fairly complex, and has taken some time to bring together in a straightforward form. The LeafSpy Android app uses the LELink2 ELM327 OBD-II dongle (amongst others) to read battery data from the Leaf, and the aim of the information gathered here is to build a similar program (in python3) to run on an RPi4b which could (possibly) provide the CHAdeMO CAN-bus interface to the Leaf.

BLE Services and Characteristics in OBD-II BLE dongle (specifically the LELink2)

The LELink2 uses a Texas CC2541 chip (which implements the standard HM-10 BLE module) to expose a serial peripheral with a proprietary connectivity service:-

UUID: 0000ffe0-0000-1000-8000-00805f9b34fb
This enables bidirectional comms between the module and any 'central device' (eg RPi4b) connected to it.

The service defines a single characteristic:-
UUID: 0000ffe1-0000-1000-8000-00805f9b34fb that stores 20 bytes of unformatted data.

When the 'central device' wants to send data to the module, it writes to the characteristic with the desired content.
When the module wants to send data, it sends a notification to the 'central device', which needs to be set up (in software) to respond to this notification and read the data.

Sending arbitrary CAN messages using the ELM327

Although ELM Electronics is no longer in business, OBD-II chips to their open-sourced specification are still being produced, and information and applications notes are still available on the web as ELM Application Notes. In order to obtain information from the Nissan Leaf Li-Ion Battery Controller (LBC) with active multi-message queries as described in Leaf2018-CAN.pdf, the most important details can be found in Application Note AN07 and the ELM327 Data Sheet

Install ble-serial package

The current version of RPiOS (Debian 12 'bookworm') requires virtual environments to be set up for external Python packages.
See notes under Python Installation checks in the RPi4b Initial Configuration page for details of how to do this.

An easy way to use Bluetooth Low Energy (BLE) in Python is to import the ble-serial package.

pip3 install ble-serial