I managed to build an ESP32 in ESPHome last night that worked with this code and returned the voltage reading in mqqt. I put the include.h and secrets.yaml file in the config/esphome directory.
I’m guessing from the lambda part of the sensor code that the voltage source is encrypted, so does that help with making an update of OpenMQTTGateway, possibly?
esphome:
name: esp32s3
friendly_name: ESP32S3
includes:
- include.h
esp32:
board: esp32-s3-devkitc-1
framework:
type: arduino
logger:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
time:
- platform: sntp
id: sntp_time
web_server:
port: 80
mqtt:
broker: 192.168.0.XXX
discovery: false
topic_prefix: esphome/BM2
username: mqtt-user
password: XXXXXXXXX
esp32_ble_tracker:
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
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: 94:A9:A8:42:72:43
id: bm2_battery_meter