BM2 battery monitoring using ble tracker component

When you copied that config, did you change the settings that apply to you or did you use their settings?

Did you also copy the file you need for a custom component? They tell you that in your link you posted.

Yes, I did but it wasn’t clear on installing on the monitor or a separate ESP32 device. I am getting failed to connect now.

INFO Starting log output from 10.10.20.83 using esphome API
INFO Successfully connected to bluetooth_gateway @ 10.10.20.83 in 0.053s
INFO Successful handshake with bluetooth_gateway @ 10.10.20.83 in 0.092s
[08:13:58][I][app:102]: ESPHome version 2023.12.8 compiled on Jan 25 2024, 07:59:03
[08:14:17][W][ble_sensor:117]: [Voltage] Cannot poll, not connected
[08:14:50][W][api.connection:107]: : Reading failed: BAD_INDICATOR errno=11
[08:15:15][I][esp32_ble_client:069]: [0] [MAC ADDRESS] 0x00 Attempting BLE connection
[08:15:17][W][ble_sensor:117]: [Voltage] Cannot poll, not connected

Did you also move the include.h file to your esphome/config directory?

@Fallingaway24

Yes, I did. I’ve tried 3 different ESP32 boards (D1mini, dig quad board, nodeMCU).

All of them report, Cannot poll.

I tried the OMG on the nodemcu and it finds the battery monitor but I haven’t yet figured out what it does with it. I’m guessing I need to add an mqtt sensor.

Ya, idk without more informaton

Here’s my ESP build. What more information would you be looking for?

esphome:
  name: bm2-esp
  friendly_name: bm2-esp

  includes:
    - include.h

esp32:
  board: esp-wrover-kit
  framework:
    type: arduino

# Enable logging
logger:
  level: INFO

# Enable Home Assistant API
api:
  encryption:
    key: !secret bm2_key

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password


mqtt:
  broker: 10.10.10.10
  discovery: false
  topic_prefix: esphome/bm2
  username: !secret mqtt_user
  password: !secret mqtt_password

web_server:
  port: 80

esp32_ble_tracker:

ble_client:
  - mac_address: !secret bm2_mac
    id: bm2_battery_meter

sensor:
  - platform: ble_client   
    type: characteristic
    ble_client_id: bm2_battery_meter
    name: Voltage
    service_uuid: 'fff0'
    characteristic_uuid: 'fff4'
    unit_of_measurement: 'V'
    accuracy_decimals: 2
    state_class: measurement
    device_class: voltage
    force_update: true
    expire_after: 5min
    notify: true
    lambda: |-
      mbedtls_aes_context aes;
      mbedtls_aes_init(&aes);
      unsigned char output[16];
      unsigned char key[16] = { 108, 101, 97, 103, 101, 110, 100, 255, 254, 49, 56, 56, 50, 52, 54, 54, };
      unsigned char iv[16] = {};
      mbedtls_aes_setkey_dec(&aes, key, 128);
      mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, 16, iv, (uint8_t*)&x[0], output);
      mbedtls_aes_free(&aes);

      return ((output[2] | (output[1] << 8)) >> 4) / 100.0f;

Anyone able to make this work lately? (Feb 2024)

I loaded the code onto an ESP32 and it compiles, but I am stuck at
[W][ble_sensor:123]: [Voltage] Cannot poll, not connected
every 5 minutes out of the logs. Seems like every 30 minutes it also spits out:
[D][esp32_ble_tracker:266]: Starting scan…

I was able to get OpenMqttGateway to connect with a similar ESP32 from the same location, so its not a ‘proximity to BM2’ problem. Unfortunately, OMG was not able to decide the Voltage data stream. :frowning:

Anyone have any suggestions on either direction?

I switched to open mqtt a few months ago.
I can’t remember what the catalyst was.

But I have noticed recently it will report the percentage but have the voltage as unknown or unavailable.

Not sure what version is on my esp32 as I’ve long since forgotten the admin password

recently?

I thought my new BM2 was the weirdo with some potentially-new encryption on it. But if you’re getting the same thing maybe it’s not just a me problem.

Just checked mine.
I have a reading for percentage updated 3 hours ago and although I have a reading for voltage or was last updated yesterday.

Even though I’ve been out to work and back.

Less of an issue for me at the moment as I’ve replaced the faulty battery in the car & no longer need the alert if the car needed to be jumpstarted in the morning.

I might try reflashing the gateway though

How long ago did u set this up? You definitely used to get voltages?

I wonder if there’s been an adverse update at some point and nobody noticed this broke.

Mine used to work fine but stopped working reliably months ago. I also found that the ESP was crashing after I added the web server

web_server:
  port: 80

Removing this has made it stable, but readings are still non existent unless I am very close to the car.

There are big bold warning boxes at the beginning of each one of the BT documents that specifically warn about crashes if you add to many platforms. Audio/voice and web server are specifically mentioned as “too much” to use with BT. Just read those docs, it save you a lot of time and avoid finding out about more preventable mistakes.


1 Like

It works! I guess I was stupidly assuming the ESP32 had similar bluetooth range as the latest iPhone. I reinstalled the yaml and plugged the ESP32 into a USB battery for portability and left it on the car’s dashboard. Now when I check the logs in Esphome I am seeing voltages with 2 decimal places of accuracy! HOORAAYY!

To be clear, I got it through ESPHome, not through OpenMQTTGateway.

First Post ! yay
I’ve setup a BM2 and the esp32 config above and it’s working well, thank you !
My only question is how to adjust the config to allow me to monitor two BM2 devices?
Do i just replicate the BLE_Client and Sensor config below the current code with the second mac address and a new name ?

Thanks,

Yes, exactly that. See below.

Screenshot 2024-04-01 at 06.06.04.jpeg

1 Like

Glad you go it working.

Is this the ESPHome code you used?

Did you have to change anything?

Yes, its working. That’s prettymuch the code I used. I think I just updated the bluetooth address and name.

I have mine connected and partially working but no reporting of Voltage.

13:34:01	[D]	[esp32_ble_client:110]	
[0] [50:54:7B:5D:A4:40] Found device
13:35:34	[D]	[esp32_ble_client:110]	
[0] [50:54:7B:5D:A4:40] Found device
13:35:34	[D]	[esp32_ble_tracker:665]	
Found device 50:54:7B:5D:A4:40 RSSI=-70
13:35:34	[D]	[esp32_ble_tracker:686]	
  Address Type: PUBLIC
13:35:34	[D]	[esp32_ble_tracker:688]	
  Name: 'Battery Monitor'
13:51:26	[I]	[app:062]	
setup() finished successfully!
13:51:26	[D]	[text_sensor:064]	
'Dryderdale vehicle battery monitor IP Address': Sending state '10.0.0.62'
13:52:58	[D]	[esp32_ble_client:110]	
[0] [50:54:7B:5D:A4:40] Found device
13:52:58	[D]	[esp32_ble_tracker:665]	
Found device 50:54:7B:5D:A4:40 RSSI=-70
13:52:58	[D]	[esp32_ble_tracker:686]	
  Address Type: PUBLIC
13:54:17	[I]	[mqtt:274]	
MQTT Connected!
14:00:29	[W]	[ble_sensor:123]	
[F12 Battery Voltage] Cannot poll, not connected

It reports the address type and device name correctly but I’m seeing cannot poll for voltage.

I have this working OK using OMG although voltage and percentage don’t report very often.

Any ideas?

Get closer to the BM2.
The bluetooth signal is not very strong. I have to keep the ESP32 pretty close to the BM2 to get them connected.

Before you drive yourself crazy in the software, get those two devices right next to each other and see if it works. If so, start experimenting to see how far away the connection stays solid.