BM2 battery monitoring using ble tracker component

let us know your progress…Tks

Hi … i got two of them for my motorhome, looking to add them in HomeAssistant

Could this be a good starting point?

custom-components/ble_monitor: BLE monitor passively monitors BLE sensors (Xiaomi, Qingping, ATC, BlueMaestro, Brifit, Govee, Kegtron, Moat, Inkbird, iNode, Yeelight, RuuviTag, SensorPush, Teltonika, Thermoplus and Thermopro) (github.com)

It has been integrated into Theengs Decoder, do you want to use with an ESP32 or a computer/raspberry?

Any one could connect to a BM2 with this?

GitHub - KrystianD/bm2-battery-monitor: Python MQTT publisher and ESPHome template for Bluetooth based Battery Monitors - reverse engineering

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