This article explores connecting a Lelink BLE OBD2 dongle to a Nissan Leaf for battery monitoring using an ESPHome Bluetooth gateway. We’ll delve into the current implementation using ESPHome’s built-in BLE stack, the challenges encountered, and potential solutions.
Current Implementation using ESPHome
The current setup utilizes a GL-S10 ESPHome Bluetooth gateway and a Lelink2 BLE OBD2 dongle to communicate with the Nissan Leaf. The following ESPHome configuration is being used:
ble_client:
- mac_address: B4:99:XX:XX:XX:XX
id: lelink
name: LELink BLE OBDII
on_connect:
then:
- lambda: |-
ESP_LOGI("ble_client_lambda", "Connected to lelink");
on_disconnect:
then:
- lambda: |-
ESP_LOGI("ble_client_lambda", "Disconnected from lelink");
text_sensor:
- platform: ble_client
ble_client_id: lelink
internal: True
notify: True
name: "lelink leaf response"
id: lelink_leaf_response_text
service_uuid: "0000ffe0-0000-1000-8000-00805f9b34fb"
characteristic_uuid: "ffe1"
on_notify:
then:
- lambda: |-
ESP_LOGI("ble_client_lambda", "notify from lelink_leaf");
interval:
- interval: 30s
then:
- logger.log:
level: INFO
format: "Sending ATZ"
- ble_client.ble_write:
id: lelink
service_uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
characteristic_uuid: ffe1
# List of bytes to write. value: [0x41, 0x54, 0x5A, 0x0D, 0x0A] #ATZrn
This configuration attempts to establish a connection with the Lelink OBD2 dongle and send the ATZ
command (followed by carriage return and line feed) every 30 seconds. The expected response, indicating successful initialization, is 0d,0d,4f,42,44,49,49,20,20,76,31,2E,35,0d,0d,3e
, which translates to “OBDII v1.5 >”.
Challenges and Troubleshooting
While initial tests using external BLE tools on an iPhone showed promising results with the ATZ
command eliciting the correct response, the ESPHome implementation exhibits inconsistent behavior. The logs reveal fragmented and incomplete responses:
[15:53:44][I][main:171]: Sending ATZ...
...
[15:54:14][I][ble_client_lambda:140]: notify from lelink OBDII v1.'
[15:54:15][I][ble_client_lambda:140]: notify from lelink >'
...
This suggests potential issues with:
- BLE Communication Stability: The connection between the ESPHome gateway and the Lelink dongle might be unstable, leading to dropped packets and incomplete data transmission.
- Command Timing: The 30-second interval might be too short, not allowing sufficient time for the dongle to process the command and send a complete response.
- Buffering: The ESPHome BLE client might not be handling the incoming data stream correctly, potentially overflowing buffers or misinterpreting fragmented packets.
Potential Solutions
Several approaches can be explored to address these challenges:
- Increase Command Interval: Extending the interval between
ATZ
commands might provide the Lelink dongle more time to respond fully. - Implement Error Handling: Incorporating error handling and retry mechanisms can improve resilience to transient communication issues.
- Custom C++ Component: Developing a custom C++ component for ESPHome allows for finer control over the BLE communication and data handling, potentially resolving buffering or timing issues. This provides a more robust solution for interacting with the Lelink Obd2 Nissan Leaf.
Conclusion
Connecting a Lelink OBD2 dongle to a Nissan Leaf using ESPHome for battery monitoring presents challenges in ensuring reliable BLE communication. While the initial setup using the built-in BLE stack shows promise, inconsistencies in data reception necessitate further investigation. Adjusting the command interval, implementing error handling, or developing a custom C++ component are potential avenues for achieving a stable and reliable connection for retrieving data from the lelink obd2 nissan leaf.