KrystianD / bm2-battery-monitor

Python connector and ESPHome template for Bluetooth based Battery Monitors - reverse engineering.
MIT License
67 stars 10 forks source link

Error reading char at handle 46, status=2 #3

Closed adrianmihalko closed 2 years ago

adrianmihalko commented 2 years ago

Hi,

Thanks for this great project. I have successfully using it with my NodeMCU32, but it has insufficient Bluetooth range so I decided to install it a new Wemos ESP32 D1 Mini near my car.

For some reason it can't read the voltage now, but it's connecting successfully:

[18:05:30][W][ble_sensor:037]: [Voltage] Disconnected!
[18:05:30][W][ble_client:119]: connect to f8:30:02:xx:xx:xx failed, status=133
[18:05:40][I][ble_client:086]: Attempting BLE connection to f8:30:02:xx:xx:xx
[18:05:41][I][ble_sensor:031]: [Voltage] Connected successfully!
[18:05:43][I][ble_client:171]: Service UUID: 0x1800
[18:05:43][I][ble_client:172]:   start_handle: 0x1  end_handle: 0xb
[18:05:43][I][ble_client:384]:  characteristic 0x2A00, handle 0x3, properties 0x2
[18:05:43][I][ble_client:384]:  characteristic 0x2A01, handle 0x5, properties 0x2
[18:05:43][I][ble_client:384]:  characteristic 0x2A02, handle 0x7, properties 0xa
[18:05:43][I][ble_client:384]:  characteristic 0x2A03, handle 0x9, properties 0x8
[18:05:43][I][ble_client:384]:  characteristic 0x2A04, handle 0xb, properties 0x2
[18:05:43][I][ble_client:171]: Service UUID: 0x1801
[18:05:43][I][ble_client:172]:   start_handle: 0xc  end_handle: 0xf
[18:05:43][I][ble_client:384]:  characteristic 0x2A05, handle 0xe, properties 0x20
[18:05:43][I][ble_client:384]:  characteristic 0x2A26, handle 0x18, properties 0x2
[18:05:43][I][ble_client:384]:  characteristic 0x2A27, handle 0x1a, properties 0x2
[18:05:43][I][ble_client:384]:  characteristic 0x2A28, handle 0x1c, properties 0x2
[18:05:43][I][ble_client:384]:  characteristic 0x2A29, handle 0x1e, properties 0x2
[18:05:43][I][ble_client:384]:  characteristic 0x2A2A, handle 0x20, properties 0x2
[18:05:43][I][ble_client:384]:  characteristic 0x2A50, handle 0x22, properties 0x2
[18:05:43][I][ble_client:172]:   start_handle: 0x34  end_handle: 0xffff
[18:05:43][I][ble_client:384]:  characteristic F000FFC1-0451-4000-B000-000000000000, handle 0x36, properties 0x1c
[18:05:43][I][ble_client:384]:  characteristic F000FFC2-0451-4000-B000-000000000000, handle 0x3a, properties 0x1c
[18:05:46][W][api.connection:080]: Home Assistant 2022.8.7 (::FFFF:C0A8:16F): Connection closed
[18:05:48][W][ble_sensor:080]: Error reading char at handle 46, status=2
[18:06:46][W][api.connection:080]: Home Assistant 2022.8.7 (::FFFF:C0A8:16F): Connection closed
[18:06:48][W][ble_sensor:080]: Error reading char at handle 46, status=2

My esphome.yaml code:

esphome:
  name: bluetooth_gateway
  platform: ESP32
  board: esp-wrover-kit
  includes:
    - include.h

wifi:
  ssid: ""
  password: ""

  manual_ip:
    static_ip: 192.168.1.206
    gateway: 192.168.1.1
    subnet: 255.255.255.0

# 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: F8:30:02:2D:4D:9C
    id: bm2_battery_meter
KrystianD commented 2 years ago

Hey @adrianmihalko, unfortunately I am not able to help. It also doesn't look like something with the code from this repository as in general it works, and it is not specific to a particular platform. In general, BT and BLE are very unstable, so it can be literally anything :(

Checking the error code - ESP_GATT_READ_NOT_PERMIT = 0x02