BM2 battery monitoring using ble tracker component

Yes.

Esphome:

esphome:
  name: bluetooth_gateway
  platform: ESP32
  board: esp-wrover-kit
  includes:
    - include.h
    
wifi:
  ssid: ""
  password: ""


# Enable logging
logger:
  level: INFO
  
# Enable Home Assistant API
api:

ota:
    
# Enable Bluetooth scanning for this ESP32
esp32_ble_tracker:

sensor:

  - platform: ble_client
    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
    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;  
      
ble_client:
  - mac_address: XX:XX:XX:XX:XX:XX
    id: bm2_battery_meter

One more thing, I am not 100% sure, but I think I copied include.h into /config/esphome/

That’s all, you will then have a sensor.voltage component in HA.

Great!

5 Likes

Hello

i got this error…any help would be apreciated

INFO Reading configuration /config/esphome/esphome-web-e044b0.yaml…
Failed config

INFO Reading configuration /config/esphome/esphome-web-e044b0.yaml…
INFO Generating C++ source…
INFO Compiling app…
Processing bluetoothgateway (board: esp-wrover-kit; framework: arduino; platform: platformio/espressif32 @ 3.5.0)

HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
LDF: Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
Dependency Graph
|-- WiFi @ 1.0
|-- ESPmDNS @ 1.0
|-- Update @ 1.0
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/src/main.cpp.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/esp32-hal-touch.c.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/esp32-hal-uart.c.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/libb64/cdecode.c.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/libb64/cencode.c.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/main.cpp.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/stdlib_noniso.c.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/wiring_pulse.c.o
Compiling /data/bluetoothgateway/.pioenvs/bluetoothgateway/FrameworkArduino/wiring_shift.c.o
In file included from src/main.cpp:24:0:
src/include.h:1:1: error: ‘mbedtls’ does not name a type
mbedtls/aes.h
^
*** [/data/bluetoothgateway/.pioenvs/bluetoothgateway/src/main.cpp.o] Error 1
========================= [FAILED] Took 31.43 seconds =========================

You could change firmware of BM2 to include all data (voltage / Temp / Accelerometer) on the BLE broadcast advertisement. BM2 uses CC2541 BLE Module from texas instrument and most CC2541 Sample codes are opensource such as reading voltage data from ADC , or reading accelerometer data from I2C. BM2 has both ADC and Accelerometer via I2c. Repository also include sample ESP32 arduino code to read voltage and accelerometer data from CC2541 BLE Broadcasts

1 Like

It would be great if someone would be able to port the sample ESP32 Arduino code into ESPHOME compatible code.

1 Like

Did you have any luck with this?

Just had my battery go flat twice in the last week. Would love to be able to b track the voltage without needing to be stood next to the car.

Working here with OMG:

This worked well! Was able to integrate into my existing ESPHome BLE Bridge!

Just a friendly reminder that original app of BM2 is calling home, more on this:

1 Like

Ya… a voltage divider. Esp boards have an ADC that does exactly this. No integration or hardly anything extra required.

Hi 1technophile

Just switched to omg from the esp home code.
Like the idea of being able to vary the scan interval etc.

I can see from the console that omg is connecting to the device & the bm2 shows under mqtt in home assistant but batt and voltage are just showing unknown.

Have I done something wrong or missed something in the setup?

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?