custom-components / ble_monitor

BLE monitor for passive BLE sensors
https://community.home-assistant.io/t/passive-ble-monitor-integration/
MIT License
1.91k stars 247 forks source link

Bluetooth: hci0: advertising data len corrected #301

Closed insof closed 3 years ago

insof commented 3 years ago

have debian with installed Home Assistant 2021.3.3 installed HACS installed ble_monitor 1.3.0 updated to last versions

bluetooth ORICO BTA-508 (RTL8761B)

one of devices, that doesnt need token - works (Viomi V-SK152) other - CGDK2 - Qingping Temp & RH Monitor Lite - i can detect it, got MAC, insert bindToken as in instructions, and got this image

image

here is YAML file image

dmesg shows image

sorry for stupid question - but im stuck on it on few weeks(

Ernst79 commented 3 years ago

Could you post the debug log from Home Assistant, starting right after a HA restart, for the first 2 minutes?

Also remove the rounding option from your configuration and set active scan to False.

insof commented 3 years ago

image

2021-03-10 21:41:09 WARNING (MainThread) [homeassistant.loader] You are using a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant 2021-03-10 21:41:09 WARNING (MainThread) [homeassistant.loader] You are using a custom integration ble_monitor which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant 2021-03-10 21:41:12 DEBUG (MainThread) [custom_components.ble_monitor] Initializing BLE Monitor integration (YAML): {'hci_interface': [0], 'discovery': True, 'active_scan': False, 'report_unknown': False, 'batt_entities': True, 'decimals': 1, 'period': 60, 'log_spikes': False, 'use_median': False, 'restore_state': False, 'devices': [{'mac': '58:2D:34:11:E0:82', 'name': 'Temp', 'encryption_key': 'bf014d866e100f110b91f9ef0089da1e', 'temperature_unit': '°C', 'decimals': 'default', 'use_median': 'default', 'restore_state': 'default', 'reset_timer': 35}], 'rounding': 1, 'is_flow': False, 'ids_from_name': True} 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.config_flow] async_step_import: {'hci_interface': [0], 'discovery': True, 'active_scan': False, 'report_unknown': False, 'batt_entities': True, 'decimals': 1, 'period': 60, 'log_spikes': False, 'use_median': False, 'restore_state': False, 'devices': [{'mac': '58:2D:34:11:E0:82', 'name': 'Temp', 'encryption_key': 'bf014d866e100f110b91f9ef0089da1e', 'temperature_unit': '°C', 'decimals': 'default', 'use_median': 'default', 'restore_state': 'default', 'reset_timer': 35}], 'rounding': 1, 'is_flow': False, 'ids_from_name': True} 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.config_flow] async_step_user: {'hci_interface': [0], 'discovery': True, 'active_scan': False, 'report_unknown': False, 'batt_entities': True, 'decimals': 1, 'period': 60, 'log_spikes': False, 'use_median': False, 'restore_state': False, 'devices': '--Devices--', 'rounding': 1, 'is_flow': False, 'ids_from_name': True} 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] Initializing BLE Monitor entry (config entry): <homeassistant.config_entries.ConfigEntry object at 0x7f4d309fe360> 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] async_setup_entry: domain {'hci_interface': [0], 'discovery': True, 'active_scan': False, 'report_unknown': False, 'batt_entities': True, 'decimals': 1, 'period': 60, 'log_spikes': False, 'use_median': False, 'restore_state': False, 'devices': [{'mac': '58:2D:34:11:E0:82', 'name': 'Temp', 'encryption_key': 'bf014d866e100f110b91f9ef0089da1e', 'temperature_unit': '°C', 'decimals': 'default', 'use_median': 'default', 'restore_state': 'default', 'reset_timer': 35}], 'rounding': 1, 'is_flow': False, 'ids_from_name': True} 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] async_setup_entry: {'hci_interface': ['0'], 'discovery': True, 'active_scan': False, 'report_unknown': False, 'batt_entities': True, 'decimals': 1, 'period': 60, 'log_spikes': False, 'use_median': False, 'restore_state': False, 'devices': [{'mac': '58:2D:34:11:E0:82', 'name': 'Temp', 'encryption_key': 'bf014d866e100f110b91f9ef0089da1e', 'temperature_unit': '°C', 'decimals': 'default', 'use_median': 'default', 'restore_state': 'default', 'reset_timer': 35}], 'rounding': 1, 'is_flow': False, 'ids_from_name': True} 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] HCI interface is [0] 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] Spawning HCIdump thread 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] HCIdump thread: Init 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] 1 encryptors mac:key pairs loaded. 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] whitelist: [] 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor] 0 whitelist item(s) loaded. 2021-03-10 21:41:14 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: Run 2021-03-10 21:41:14 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: connected to hci0 2021-03-10 21:41:14 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: start main event_loop 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Starting binary sensor entry startup 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] BLE binary sensors updater initialization 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] BLE binary sensors updater initialized 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Binary sensor entry setup finished 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Binary entities updater loop started! 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Starting measuring sensor entry startup 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.sensor] BLE sensors updater initialization 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.sensor] BLE sensors updater initialized 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Measuring sensor entry setup finished 2021-03-10 21:41:14 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Entities updater loop started! 2021-03-10 21:41:16 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Binary sensor device with mac address B8:7C:6F:A8:E6:A5 has the following settings. Name: B87C6FA8E6A5. Restore state: False. Reset Timer: 35 2021-03-10 21:41:16 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac address B8:7C:6F:A8:E6:A5 has the following settings. Name: B87C6FA8E6A5. Temperature unit: °C. Decimals: 1. Use Median: False. Restore state: False. Reset Timer: 35. 2021-03-10 21:41:16 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] async_added_to_hass called for ble switch B87C6FA8E6A5 2021-03-10 21:41:16 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble temperature B87C6FA8E6A5 2021-03-10 21:41:21 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac address 58:2D:34:11:E0:82 has the following settings. Name: Temp. Temperature unit: °C. Decimals: 1. Use Median: False. Restore state: False. Reset Timer: 35. 2021-03-10 21:41:21 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac address 58:2D:34:11:E0:82 has the following settings. Name: Temp. Temperature unit: °C. Decimals: 1. Use Median: False. Restore state: False. Reset Timer: 35. 2021-03-10 21:41:21 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac address 58:2D:34:11:E0:82 has the following settings. Name: Temp. Temperature unit: °C. Decimals: 1. Use Median: False. Restore state: False. Reset Timer: 35. 2021-03-10 21:41:21 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble temperature Temp 2021-03-10 21:41:21 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble humidity Temp 2021-03-10 21:41:21 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble battery Temp 2021-03-10 21:42:15 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] 2 MiBeacon BLE ADV messages processed for 1 binary sensor device(s) total. Priority queue = 0 2021-03-10 21:42:15 DEBUG (MainThread) [custom_components.ble_monitor.sensor] 2 MiBeacon BLE ADV messages processed for 2 measuring device(s). 2021-03-10 21:42:15 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: main event_loop stopped, finishing 2021-03-10 21:42:15 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: Scanning will be restarted 2021-03-10 21:42:15 DEBUG (Thread-3) [custom_components.ble_monitor] 4 HCI events processed for previous period. 2021-03-10 21:42:15 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: Run 2021-03-10 21:42:15 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: connected to hci0 2021-03-10 21:42:15 DEBUG (Thread-3) [custom_components.ble_monitor] HCIdump thread: start main event_loop

im trying to get info from 58:2D:34:11:E0:82 device maybe key is not valid? can i somehow check it? i was strictly following instructions to get the key from Mi Home app, but i have 2 different data sets from it

first one Did: blt.3.15heks1cc5g00 Token: f344ad0a17609d281b40c1ca Bindkey: bf014d866e100f110b91f9ef0089da1e Mac: 58:2D:34:11:E0:82

second one { "did": "blt.3.15heks1cc5g00", "uid": 1681869602, "token": "f344ad0a17609d281b40c1ca", "name": "Qingping Temp & RH Monitor Lite", "pid": 6, "mac": "58:2D:34:11:E0:82", "bssid": "", "longitude": "0.00000000", "latitude": "0.00000000", "show_mode": 1, "model": "cgllc.sensor_ht.dk2", "permitLevel": 16, "isOnline": true, "spec_type": "urn:miot-spec-v2:device:temperature-humidity-sensor:0000A00A:cgllc-dk2:1", "extra": { "isSetPincode": 0, "pincodeType": 0, "fw_version": "1.1.1_0102", "isSubGroup": false, "showGroupMember": false }, "orderTime": 1611348871, "freqFlag": true, "hide_mode": 0, "api_server": "cn" }

Ernst79 commented 3 years ago

Did you add it to MiHome? If so, you can use Xiaomi cloud token extractor to get the correct key. The latest version supports extracting Bluetooth encryption keys.

https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor

insof commented 3 years ago

yes, i add it to MiHome to get this tokens(data) will try today and write here. thanks!

ps looking into hassio this morning found that somewhere in the night I get some data from this device image

3 hours ago means that last data recieved 3 hours ago?

Ernst79 commented 3 years ago

I think it is just a matter of bad reception. For a test, place the sensor close to your HA machine (raspberry?). If you use an SSD, make sure it is as far away as possible from the bluetooth radio. SSD drives are know to interfere with/block Bluetooth.

insof commented 3 years ago

sensor is in 25-30 cm from bluetooth reciever - will take it closer its not raspberry, its regular old pc, with USB Bluetooth reciever no SSD in it, regular HDD 2.5"

Ernst79 commented 3 years ago

25 cm is close enough :-). Lets move on to the real debugging then. Could you make a HCIdump for, let's say, 10 minutes. I copied the instructions from the faq below.

The BLE advertisements, which are collected with the above procedure, can also be collected with hcidump with the following command (leave it running for a couple of minutes).

sudo hcidump --raw hci > dump.txt

In case you get Disable scan failed: Input/output error, reset hciconfig with one of the following

sudo hciconfig hci0 down
sudo hciconfig hci0 up

or

sudo hciconfig hci0 reset

And than run the first command again.

You can attach the hcidump here as an attachment

insof commented 3 years ago

moving sensor closer didnt help here is the file dump.txt

and another one dump.txt

Ernst79 commented 3 years ago

Thanks, I found an issue in the data format of the messages, I have to look into this a bit further, will do that tonight. Might be related to the advertings data len corrected messages you showed. I'll come back to you a.s.a.p.

insof commented 3 years ago

thanks! i was fighting with it for few weeks, feeling myself totally dumb))

Ernst79 commented 3 years ago

I checked both your dump.txt files. All messages that are send by the sensor do not contain any useful data. All messages from your sensor end with your MAC address (reversed per two) and than only 08. Normally, you would get about 30 numbers behind the MAC address, which contains the data we need.

------mac-----------??
82 e0 11 34 2d 58   08

I also checked the data len corrected messages, I see these on my system as well. But it still works fine on my end, so I don't think that has anything to do with it.

The big question is, why does the sensor only send messages without data? (or only very limited)

So I guess it is something on the sensor side. Could you try to remove it from MiHome, and readd it. Next, get the new encryption key (will be changed) and try it again.

insof commented 3 years ago

no problem, will do that in a few hours, as far as I will be near sensor. can it be a problem with Bluetooth driver? i see that i can detect other devices and collect correct data from Viomi Kettle, so i think drivers works well.

Ernst79 commented 3 years ago

I see messages coming in, so I don't expect driver issues. Even messages from your CGDK2 sensor, but they just don't contain (useful) data (only the MAC and RSSI value).

A HCIdump is just low level data collecting, so it's not related to BLE monitor or Home Assistant.

That is why I put my bets on the sensor not being coupled to MiHome for some reason

insof commented 3 years ago

I recieve data from it on the phone, maybe phone sends some request? will try to readd, and write then.

insof commented 3 years ago

sorry for delay new data after sensor reset and readd to mihome: Did: blt.3.1621b6djoek00 Token: d941c385eb5491f99946d29b Bindkey: 84981e3608a723d799dff4751b0492d9 Mac: 58:2D:34:11:E0:82

after HASSIO restart got this in dmesg image

here is dump

dump.txt

still nothing from sensor image

insof commented 3 years ago

after few hours sensor sends some info, but randomly, as i understand image image

behavior the same as before re adding

Ernst79 commented 3 years ago

Based on your information, I should say that the component is working, but it is just receiving a very limited number of messages. RSSI value seems fine. Kettle is working, so it should be something on the sensor side, I think. To be honest, I'm out of idea's. I'm not if it is possible, but you could try another bluetooth dongle and/or even a different HA, to see if it makes a difference. But I understand that might not be easy, if you don't have a different device at hand.

insof commented 3 years ago

i can try another dongle but im confused - phone gets the information, when i select sensor on mihome, so - sensor sends data. i can order another sensor, is there any sensor without encryption you can advise?

Ernst79 commented 3 years ago

I think the phone/app activates the "active mode" of the sensor. Bluetooth has two working modes, active and passive. In active mode, bluetooth sends a request to the sensor and asks for the data. That is probably how your phone/app works. this data is in a different format and can NOT be read by BLE Monitor. The sensor also sends data, without "asking" for it. Our components listens to this last data. It has a different format compared the the active messages.

The reason we don't use this active messaging, is because it affects the battery. For the MiHome app, this isn't a big issue, as you only check the data when you open the app, which is probably only once a day or once a week. Home Assistant would require asking for data every e.g. minute, which would drain the battery fast(er).

I use the LYWSDCGQ (no encryption) and 3x LYWSD03MMC (with encryption). The last has encryption, but you can flash ATC firmware on it, which removes the encryption and makes it send data very often (multiple times per minute). The first sensor is a bit bigger, the last is quite small. Both are fine. Flashing firmware is easy and can be done on a website.

insof commented 3 years ago

understood, thanks i will try something like changing the dongle and write here

ordered LYWSD03MMC - will try with it. hope it will work. another offtopic question - can I trigger some event or launch some script on changing temp for example or easy get this data outside hassio? I want to control my gas boiler with it

Ernst79 commented 3 years ago

Sure, you can make an automation that triggers e.g. when temperate gets below a certain value. That should not be a problem. The automation can start a script when triggered. That’s the beauty of Home Assistant

insof commented 3 years ago

thanks, today i will recieve new sensor and will try to add it to ble_monitor hope it would work

insof commented 3 years ago

recieve LYWSD03MMC

obtain token Did: blt.3.163a4mq9s5g00 Token: a4bd16a1110fccfc330a3bc3 Bindkey: c751edf5aefea4a9774bf949e2f2f2b3 Mac: A4:C1:38:2B:B2:79

add it to ble monitor image

image

image

still have no other dongle to test, but have some red in logs image

image

image

collecting data for dump, will be in short time

insof commented 3 years ago

dump dump.txt

ordered new dongle.

Ernst79 commented 3 years ago

You have the exact same issue, in the HCI dump, all messages from your new sensor again only show 08 as data. Very strange. As both sensors show the same problem, I think it's related to your dongle, I agree.

Hopefully a new dongle will help.

insof commented 3 years ago

the same as before new sensor sometimes recieves data image

image

i cant find any bluetooth dongle that support linux(officially) so ordered one that I found on linux.org in disscussions.

Ugreen BT4.0 Bluetooth: 4.0 + EDR CSR8510

Ernst79 commented 3 years ago

With the normal firmware, it should update once every 10 minutes. You might want to try ATC firmware https://github.com/pvvx/ATC_MiThermometer

This will allow more frequent updates (several per minute).

But first the dongle issue should be solved!

Ernst79 commented 3 years ago

In issue #302 , someone else found that his sensor is only updated once every 11/12 minutes for CGDK2. He posted a link to another github, where they also show that the other messages have only 08 as result.

insof commented 3 years ago

read it, but didnt understand anything) dont know what ot do) waiting for new dongle for now

Ernst79 commented 3 years ago

Sorry if I wasn't clear. I tried to explain that the CGDK2 sensor sends (useful) data only once every 11 or 12 minutes. The same applies for the LYWD03MMC sensor, which sends data once every 10 minutes (with default firmware). So, if you have reception issues with your current bluetooth dongle, it could explain the behavior you observe.

I still think you have reception issues, despite the excellent RSSI value. This could be something blocking the Bluetooth signal (SSD/wifi). or just a bad dongle. So, nothing you can do right now, lets wait for the new dongle and see if that helps.

One thing you could do, is installing the custom firmware for the LYWD03MMC sensor (see https://github.com/pvvx/ATC_MiThermometer). It's up to you if you feel comfortable with flashing your sensor (which is actually quite simple, as they provided a webtool to do the flashing). The main benefit of this custom firmware is that it sends multiple messages a minute, in stead of once every 10 minutes. This reduces the impact of missing messages a lot. But it is only available for the LYWD03MMC sensor, not for CGDK2.

Ernst79 commented 3 years ago

Any update? Is the new dongle working better?

insof commented 3 years ago

sorry, I got new dongle, but its not supported by linux, as I found in net its unsupported on core version >3.9 i can see it in lsusb but hci0 device not detected

I ordered another one, I hope it would work and I will update this issue. Sorry for delay.

insof commented 3 years ago

finally I win this battle, at the end I have 2 different working PC, 4 different bluetooth dongles, 3 bluetooth temperature sensors

I assemble another machine from some old stuff, but install ubuntu(not debian as on first), install HA through docker. One of 4 dongles works with ubuntu out of the box. then I followed your instructions on install - and everything works well.

I assume that there was something in installation(debian+HA). Sorry for your time. Hope it will help someone to save lots of time and money.

Ernst79 commented 3 years ago

No problem, glad it is solved for you.

pabloab commented 2 years ago

Seems related to this kernel bug.