BM2 battery monitoring using ble tracker component

Hello,

You could try to set the connection interval To every 10 minutes and wait a couple of hours with the BM2 close to the ESP32.

Sorted,
Found another forum post that was talking about the base topic, had to add home/back to the start of it, as soon as I put that back in front of BLE-BM2 it came up straight away.

Final dumb newbie question. If i want to have the BM2 update in real time i set the interval between scans/active scans slider to 0? and if I’m understanding the documentation correctly I whitelist the BM"'s mac and then turn the connection time up so that the ESP32 isnt constantly trying to connect to everything?

End goal is to have the sensor data update in real time in the morning before i leave for work, less frequently over night or at weekends,

To increase the voltage frequency update you can decrease the Time between connection to 1 minute, you could go also to 10s by an mqtt command (10000s):

Reduce also the scan interval to 10s in this case:

Ahh
I had scan and connections backwards.
Makes sense now I think.

I’ll test it out when I get home.

Thanks again for the help.

1 Like

is this currently working with ESPHome? if so, what is the best code to use to do the job?
thanks

It got it up and running quite recently using the code mentioned here, referencing this.

I am not sure where to begin with this project. Do I need to flash ESPhome onto the BM2 battery monitor?

Am I just taking the ESPhome yaml from this comment and flashing it to an ESP32?

I’ve tried flashing to the ESP32 but HA isn’t picking anything up with the battery monitor.

Any guidance would be appreciated.

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;

1 Like

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