esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
291 stars 36 forks source link

ble_client sensor with two characteristics values are crossed #6434

Open shaneinak opened 4 hours ago

shaneinak commented 4 hours ago

The problem

I have a BLE sensor with two characteristics. One is for the amount of oil in my tank in gallons, the other is the percent full . It was all working as expected and displaying in my home assistant dashboard as expected. This setup is still in a lab environment and I wanted to ensure it could survive a reboot. After the reboot the readings appear but the percentage reading shows up as the gallons and the gallons show up as the percentage. It is a 300 gallon tank that has 262.8 gallons that comes out to 87.6% full. How did a reboot of the ble_sensor and the BLE proxy device flip these readings?

Screenshot 2024-11-15 094833

Which version of ESPHome has the issue?

ESPHome 2024.10.3

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2024.11.1

What platform are you using?

ESP32

Board

ESP-WROOM-32 ESP32 ESP-32S

Component causing the issue

No response

Example YAML snippet

substitutions:
  name: esp32-bluetooth-proxy-1fff70
  friendly_name: BLE Proxy
packages:
  esphome.bluetooth-proxy: github://esphome/bluetooth-proxies/esp32-generic/esp32-generic.yaml@main
esphome:
  name: ${name}
  name_add_mac_suffix: false
  friendly_name: ${friendly_name}
api:
  encryption:
    key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
esp32_ble_tracker:

ble_client:
  - mac_address: 40:4C:CA:48:45:66
    id: Tank_Level_Sensor
sensor:
  - platform: ble_client
    type: characteristic
    ble_client_id: Tank_Level_Sensor
    name: "Amount of Oil"
    service_uuid: '5624c4a0-a813-48d1-84f8-39aadcf45b76'
    characteristic_uuid: 'bc7f5493-977c-40a9-8546-9b987ff71586'
    unit_of_measurement: "gal"
    icon: "mdi:gauge"
    accuracy_decimals: 1
    lambda: |-
      return *((float*)(&x[0]));
  - platform: ble_client
    type: characteristic
    ble_client_id: Tank_Level_Sensor
    name: "Tank Percent Full"
    service_uuid: '5624c4a0-a813-48d1-84f8-39aadcf45b76'
    characteristic_uuid: 'efb623c7-1297-4ca5-bf35-cfcaa1949ce7'
    unit_of_measurement: "%"
    icon: "mdi:gauge"
    accuracy_decimals: 1
    lambda: |-
      return *((float*)(&x[0]));
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

Anything in the logs that might be useful for us?

INFO ESPHome 2024.10.3
INFO Reading configuration /config/esphome/esp32-bluetooth-proxy-1fff70.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing esp32-bluetooth-proxy-1fff70 (board: esp32dev; framework: espidf; platform: platformio/espressif32@5.4.0)
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - framework-espidf @ 3.40408.0 (4.4.8) 
 - tool-cmake @ 3.16.9 
 - tool-ninja @ 1.10.2 
 - toolchain-esp32ulp @ 2.35.0-20220830 
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Reading CMake configuration...
Dependency Graph
|-- noise-c @ 0.1.6
RAM:   [==        ]  16.4% (used 53688 bytes from 327680 bytes)
Flash: [=======   ]  69.0% (used 1265761 bytes from 1835008 bytes)
========================= [SUCCESS] Took 81.31 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 192.168.50.239
INFO Uploading /data/build/esp32-bluetooth-proxy-1fff70/.pioenvs/esp32-bluetooth-proxy-1fff70/firmware.bin (1272336 bytes)
Uploading: [============================================================] 100% Done...

INFO Upload took 15.37 seconds, waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from 192.168.50.239 using esphome API
INFO Successfully connected to esp32-bluetooth-proxy-1fff70 @ 192.168.50.239 in 11.341s
INFO Successful handshake with esp32-bluetooth-proxy-1fff70 @ 192.168.50.239 in 0.184s
[09:15:48][I][app:100]: ESPHome version 2024.10.3 compiled on Nov 14 2024, 20:59:11
[09:15:48][C][wifi:600]: WiFi:
[09:15:48][C][wifi:428]:   Local MAC: A0:B7:65:1F:FF:70
[09:15:48][C][wifi:433]:   SSID: [redacted]
[09:15:48][C][wifi:436]:   IP Address: 192.168.50.239
[09:15:48][C][wifi:440]:   BSSID: [redacted]
[09:15:48][C][wifi:441]:   Hostname: 'esp32-bluetooth-proxy-1fff70'
[09:15:48][C][wifi:443]:   Signal strength: -42 dB ▂▄▆█
[09:15:48][C][wifi:447]:   Channel: 9
[09:15:48][C][wifi:448]:   Subnet: 255.255.255.0
[09:15:48][C][wifi:449]:   Gateway: 192.168.50.1
[09:15:48][C][wifi:450]:   DNS1: 192.168.50.1
[09:15:48][C][wifi:451]:   DNS2: 0.0.0.0
[09:15:48][C][logger:185]: Logger:
[09:15:48][C][logger:186]:   Level: DEBUG
[09:15:48][C][logger:188]:   Log Baud Rate: 115200
[09:15:48][C][logger:189]:   Hardware UART: UART0
[09:15:48][C][bluetooth_proxy:091]: Bluetooth Proxy:
[09:15:48][C][bluetooth_proxy:092]:   Active: YES
[09:15:48][C][bluetooth_proxy:093]:   Connections: 3
[09:15:48][C][bluetooth_proxy:094]:   Raw advertisements: YES
[09:15:48][C][safe_mode.button:024]: Safe Mode Button 'Safe Mode Boot'
[09:15:48][C][safe_mode.button:024]:   Icon: 'mdi:restart-alert'
[09:15:48][C][factory_reset.button:011]: Factory Reset Button 'Factory reset'
[09:15:48][C][factory_reset.button:011]:   Icon: 'mdi:restart-alert'
[09:15:48][C][ble_sensor:017]: BLE Sensor 'Amount of Oil'
[09:15:48][C][ble_sensor:017]:   State Class: ''
[09:15:48][C][ble_sensor:017]:   Unit of Measurement: 'gal'
[09:15:48][C][ble_sensor:017]:   Accuracy Decimals: 1
[09:15:48][C][ble_sensor:017]:   Icon: 'mdi:gauge'
[09:15:48][C][ble_sensor:018]:   MAC address        : 40:4C:CA:48:45:66
[09:15:48][C][ble_sensor:019]:   Service UUID       : 5624C4A0-A813-48D1-84F8-39AADCF45B76
[09:15:48][C][ble_sensor:020]:   Characteristic UUID: BC7F5493-977C-40A9-8546-9B987FF71586
[09:15:48][C][ble_sensor:021]:   Descriptor UUID    : 00000000-0000-0000-0000-000000000000
[09:15:48][C][ble_sensor:022]:   Notifications      : NO
[09:15:48][C][ble_sensor:023]:   Update Interval: 60.0s
[09:15:48][C][ble_sensor:017]: BLE Sensor 'Tank Percent Full'
[09:15:48][C][ble_sensor:017]:   State Class: ''
[09:15:48][C][ble_sensor:017]:   Unit of Measurement: '%'
[09:15:48][C][ble_sensor:017]:   Accuracy Decimals: 1
[09:15:48][C][ble_sensor:017]:   Icon: 'mdi:gauge'
[09:15:48][C][ble_sensor:018]:   MAC address        : 40:4C:CA:48:45:66
[09:15:49][C][ble_sensor:019]:   Service UUID       : 5624C4A0-A813-48D1-84F8-39AADCF45B76
[09:15:49][C][ble_sensor:020]:   Characteristic UUID: EFB623C7-1297-4CA5-BF35-CFCAA1949CE7
[09:15:49][C][ble_sensor:021]:   Descriptor UUID    : 00000000-0000-0000-0000-000000000000
[09:15:49][C][ble_sensor:022]:   Notifications      : NO
[09:15:49][C][ble_sensor:023]:   Update Interval: 60.0s
[09:15:49][C][esp32_ble:391]: ESP32 BLE:
[09:15:49][C][esp32_ble:393]:   MAC address: A0:B7:65:1F:FF:72
[09:15:49][C][esp32_ble:394]:   IO Capability: none
[09:15:49][C][esp32_ble_tracker:655]: BLE Tracker:
[09:15:49][C][esp32_ble_tracker:656]:   Scan Duration: 300 s
[09:15:49][C][esp32_ble_tracker:657]:   Scan Interval: 320.0 ms
[09:15:49][C][esp32_ble_tracker:658]:   Scan Window: 30.0 ms
[09:15:49][C][esp32_ble_tracker:659]:   Scan Type: ACTIVE
[09:15:49][C][esp32_ble_tracker:660]:   Continuous Scanning: True
[09:15:49][C][ble_client:027]: BLE Client:
[09:15:49][C][ble_client:028]:   Address: 40:4C:CA:48:45:66
[09:15:49][C][ble_client:029]:   Auto-Connect: TRUE
[09:15:49][C][mdns:116]: mDNS:
[09:15:49][C][mdns:117]:   Hostname: esp32-bluetooth-proxy-1fff70
[09:15:49][C][esphome.ota:073]: Over-The-Air updates:
[09:15:49][C][esphome.ota:074]:   Address: esp32-bluetooth-proxy-1fff70.local:3232
[09:15:49][C][esphome.ota:075]:   Version: 2
[09:15:49][C][safe_mode:018]: Safe Mode:
[09:15:49][C][safe_mode:020]:   Boot considered successful after 60 seconds
[09:15:49][C][safe_mode:021]:   Invoke after 10 boot attempts
[09:15:49][C][safe_mode:023]:   Remain in safe mode for 300 seconds
[09:15:49][C][api:140]: API Server:
[09:15:49][C][api:141]:   Address: esp32-bluetooth-proxy-1fff70.local:6053
[09:15:49][C][api:143]:   Using noise encryption: YES
[09:16:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:16:15][D][sensor:094]: 'Amount of Oil': Sending state 87.58563 gal with 1 decimals of accuracy
[09:16:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:16:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.75690 % with 1 decimals of accuracy
[09:16:36][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
[09:16:36][D][esp32.preferences:114]: Saving 1 preferences to flash...
[09:16:36][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[09:17:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:17:15][D][sensor:094]: 'Amount of Oil': Sending state 87.48933 gal with 1 decimals of accuracy
[09:17:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:17:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.46802 % with 1 decimals of accuracy
[09:18:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:18:15][D][sensor:094]: 'Amount of Oil': Sending state 87.48933 gal with 1 decimals of accuracy
[09:18:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:18:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.46802 % with 1 decimals of accuracy
[09:19:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:19:15][D][sensor:094]: 'Amount of Oil': Sending state 87.48933 gal with 1 decimals of accuracy
[09:19:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:19:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.75690 % with 1 decimals of accuracy
[09:20:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:20:15][D][sensor:094]: 'Amount of Oil': Sending state 87.48933 gal with 1 decimals of accuracy
[09:20:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:20:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.75690 % with 1 decimals of accuracy
[09:20:43][D][esp32_ble_tracker:270]: Starting scan...
[09:21:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:21:15][D][sensor:094]: 'Amount of Oil': Sending state 87.58563 gal with 1 decimals of accuracy
[09:21:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:21:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.75690 % with 1 decimals of accuracy
[09:22:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:22:15][D][sensor:094]: 'Amount of Oil': Sending state 87.58563 gal with 1 decimals of accuracy
[09:22:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:22:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.75690 % with 1 decimals of accuracy
[09:23:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:23:15][D][sensor:094]: 'Amount of Oil': Sending state 87.48933 gal with 1 decimals of accuracy
[09:23:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:23:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.46802 % with 1 decimals of accuracy
[09:24:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:24:15][D][sensor:094]: 'Amount of Oil': Sending state 87.48933 gal with 1 decimals of accuracy
[09:24:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:24:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.75690 % with 1 decimals of accuracy
[09:25:15][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:25:15][D][sensor:094]: 'Amount of Oil': Sending state 87.48933 gal with 1 decimals of accuracy
[09:25:30][D][esp32_ble_client:110]: [3] [40:4C:CA:48:45:66] ESP_GATTC_READ_CHAR_EVT
[09:25:30][D][sensor:094]: 'Tank Percent Full': Sending state 262.75690 % with 1 decimals of accuracy

Additional information

Here is a snip of the BLE specific Arduino code on the sensor device:

bool deviceConnected = false; bool oldDeviceConnected = false;

define SERVICE_UUID "5624c4a0-a813-48d1-84f8-39aadcf45b76"

define CHARACTERISTIC_GALLON_UUID "bc7f5493-977c-40a9-8546-9b987ff71586"

define CHARACTERISTIC_PERCENT_UUID "efb623c7-1297-4ca5-bf35-cfcaa1949ce7"

// makes the chracteristic globlal static BLECharacteristic pCharacteristicGallon; static BLECharacteristic pCharacteristicPercent;

class MyServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer *pServer) { deviceConnected = true; };

void onDisconnect(BLEServer *pServer) { deviceConnected = false; } };

void setup() {

BLEDevice::init("Tank Level Sensor"); BLEServer pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); BLEService pService = pServer->createService(SERVICE_UUID); pCharacteristicGallon = pService->createCharacteristic( CHARACTERISTIC_GALLON_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); pCharacteristicPercent = pService->createCharacteristic( CHARACTERISTIC_PERCENT_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); pService->start(); BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); //pAdvertising->addManufacturerSpecificData(MANUFACTURER_ID); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06);
pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); }

void loop() { if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready BLEDevice::startAdvertising(); // restart advertising oldDeviceConnected = deviceConnected; } // connecting if (deviceConnected && !oldDeviceConnected) { // do stuff here on connecting oldDeviceConnected = deviceConnected; }

pCharacteristicGallon->setValue(gallons_of_oil);
pCharacteristicPercent->setValue(percent);

delay(1000);

}

shaneinak commented 3 hours ago

Well... I rebooted again and the readings are correct again. I guess this is just one of those quirky issues that gets fixed with the magic ON/OFF button. if anyone knows what may be causing this odd behavior I'd appreciate any tips. At one point I had three characteristics, the third being the battery level of the sensor. I was getting many combinations of crisscrossed values. I ended up removing the battery since the battery life was to short, I decided to just hardwire the sensor power for reliable power. The values getting crossed up is a strange one and I am a total armature and most likely don't have the most efficient coding. Screenshot 2024-11-15 114058