pvvx / ATC_MiThermometer

Custom firmware for the Xiaomi Thermometers and Telink Flasher
https://github.com/pvvx/pvvx.github.io/tree/master/ATC_MiThermometer
Other
2.75k stars 196 forks source link

Using 4.5 firmware and values from two are getting unavailable #430

Closed thehijacker closed 6 months ago

thehijacker commented 7 months ago

I have an odd problem. Nine LYWSD03MMC sensors with latest 4.5 firmware on them. 4 are on top floor and 5 in ground floor of the house. I also have two ESP32 flashed with Bluetooh Proxy. One for each floor (in middle of hallways). Default configuration for them:

esp32_ble_tracker:
  scan_parameters:
    interval: 1100ms
    window: 1100ms
    active: true

bluetooth_proxy:
  active: true

All nine sensors have default configuration. I only changed the protocol to BTHome v2. Home assistant picked them all up automatically under BTHome integration up to 60 seconds after I changed the protocl and saved configuration. And it started collecting data.

But two of the (on top floor) are giving me issues. There are gaps between collection and Home Assistant will show unavailable status for them when he doesn't get regular updates.

Here are all with current values:

image

And graph for one without issues:

image

And graphs for the two problematic ones:

image

image

I did change the batteries on both problematic sensors and it is the same with new batteries.

How to debug this? Bluetooth Proxy is not picking them up? Only one proxy would be enough or I need more then two? I need different ESPHome Bluetooth Proxy configuration or maybe I need to change setting on the temperature sensors?

Thank you for any suggestions.

pvvx commented 7 months ago

I have a lot of BLE sensors and when testing ESPHome Bluetooth Proxy did not cope. But the simplest CSR8510A10 USB-BT adapter works stably with 30..50 BLE devices. https://github.com/pvvx/ATC_MiThermometer/discussions/328

In the default setting, the thermometer transmits all data in BTHome format every 2.5 seconds and over 3 channels.

Please report instabilities to ESPHome.

thehijacker commented 7 months ago

Thanks for the prompt reply. But this adapter requires PC? Will one of this adapters be able to reach all sensors in house (both floors)? The HA servers is not in ideal location (ex. center of the house).

pvvx commented 7 months ago

It is possible to switch the thermometer to work on "LE Long Range". The communication range in the “LE Long Range” mode exceeds the communication range of household Wi-Fi. To compensate for battery consumption, increase the advertising interval in TelinkMiFlasher.html by 5..6 seconds.

Then you will need an RTL8761BU type adapter. How to enable BLE 5.0+ processing mode in Linux/Bluez is described in https://github.com/pvvx/ATC_MiThermometer/issues/297. This configuration significantly exceeds the communication range of the ESP32 with any Wi-Fi router, or Zigbee. Competition is only with GSM and fiber optic communications, if compared in a straight line without radio obstacles.

New Bluetooth standards have not been supported on Linux since 2016. Someone pays extra or is interested in preventing development and expanding user capabilities. Only Android provides support for the latest BLE standards in its API.

thehijacker commented 7 months ago

I've rebooted both ESP proxy boards and set interval to 5 seconds. It seems to fix it:

image

pvvx commented 7 months ago

Such a game of “synchronization” will not help globally.

thehijacker commented 7 months ago

Today I have received Orange Pi Zero 3 board to use as cheap backup for some service main server runs. It also has Bluetooth 5.0. Can I use it as BLE receiver?

http://www.orangepi.org/html/hardWare/computerAndMicrocontrollers/details/Orange-Pi-Zero-3.html

pvvx commented 7 months ago

Today I have received Orange Pi Zero 3 board to use as cheap backup for some service main server runs. It also has Bluetooth 5.0. Can I use it as BLE receiver?

No technical specifications. Only advertising about BT 5.0. There is no support for Bluetooth 5.0 in the system and program. Built-in BT chips in different RPIs usually have poor reception, low performance and instability. Everything is done purely for show, to sell.

mtnrun commented 7 months ago

Apologies for these basic questions but I'm new to this and I'm having the same issue (dropped sensor readings) for which I haven't been able to find any definitive resources on the best esphome configuration if using bluetooth_proxy or a different setup altogether to get LYWSD03MMC sensor data reliably in home assistant.

For esphome, do you need to set esp32_ble_tracker and /or bluetooth_proxy to active? What's the best config that works with LYWSD03MMC? Additionally, what are the ideal configs for LYWSD03MMC around setting different intervals to maybe reduce conflicts with multiple units (I have 5 sensors currently)?

It seems like pvvx is suggesting that esp32 boards that act as bluetooth_proxies are always going to be brittle. Am I understanding this correctly that buying a bluetooth dongle and connecting it directly to the server running home assistant will yield better results? The part I find confusing with that suggestion is driver support. I got a beelink mini s12 and followed the suggestion to install proxmox, and then run home assistant in a VM. Are you familiar if bluetooth support for a dongle would work with such a setup? Appreciate any additional guidance.

pvvx commented 7 months ago

What's the best config that works with LYWSD03MMC?

The USB-BT adapter on the CSR8510A10 works with any thermometer interval settings.

There are currently over 60 BLE devices on the air nearby. Of these, registered and connected to HA on one adapter:

image

image

image

But "LE Long Range" doesn't work - the CSR8510A10 is a chip with preliminary support for Bluetooth 4.2.

pvvx commented 7 months ago

Devices such as smart plugs, TVs, computers, smartphones and other Bluetooth enabled devices broadcast BLE/MESH advertisements at intervals of up to 50-100ms. image All this interferes with reception and the adapter must accept everything.

What kind of ESP32 with a children's program for games and initial education can we talk about?

"btmon" in real time: https://youtube.com/shorts/mmhbBSmxqO0?feature=share

Wifi MQTT - :) :) :)

When testing in an urban environment with a BLE sniffer, it received more than a hundred BLE/MESH advertisements per second. The HCI protocol is not able to pass such a stream via UART at a speed of 115200 baud.

pvvx commented 7 months ago

There is a T-ZigBee module with ESP32-C3 and TLSR8258, which contains 2 chips. But the antennas are not spaced apart and there is no software support.

mtnrun commented 7 months ago

Thanks, I think I'll buy one of the CSR8510A10 adapters from the HA page you linked and see if I can get it to work. All my sensors are within 6 meter range.

pvvx commented 7 months ago

A set of 2 adapters, providing all the options possible today.

image

RTL8761BU (Best receiver sensitivity)

  1. Software (fw) loads when Linux starts (BT5.2)
  2. Low speed
  3. Support all PHY - Working in LE Long Range (500..1500 m) image https://aliexpress.com/item/4001097686145.html image

Warning: Linux (and Bluez) does not support Bluetooth 5.0. Patch

CSR8510A10 (Speed and large number of accepted Bluetooth 4.2 advertising packets.)

image

PS: Do not confuse these adapters with numerous counterfeits.

thehijacker commented 7 months ago

If I buy one of this and connect it to a PC that is not running Home Assistant directly (RPi, etc), what would be best software to run on it, that would collect the data and pass it to Home Assistant?

I would prefer that Home Assistant automatically detects the sensors if possible. But I do not mind to add the manually as I have only 9.

pvvx commented 7 months ago

I have not done such research. For me, to cover my private territory of hundreds of meters, one single-board computer with two specified cheap adapters is enough. There are more problems with WiFi routers than with LE Long Range directly in HA. Currently, the network segment in the townhouse is disconnected due to the freezing of the Wi-Fi router. To fix the problem, you need to travel hundreds of kilometers to switch the power supply to the router... And with BLE devices such problems have not been observed for 3 years. It is enough to change the batteries on time.

thehijacker commented 6 months ago

Indeed it was a race condition. After one day those two sensor readings were again unreliable. But today I have received the USB BT adapter (Ugreen):

Bus 001 Device 021: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
[3018415.534985] usb 1-3: new full-speed USB device number 21 using xhci_hcd
[3018415.792501] usb 1-3: New USB device found, idVendor=0a12, idProduct=0001, bcdDevice=88.91
[3018415.792511] usb 1-3: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[3018415.792515] usb 1-3: Product: CSR8510 A10
[3018415.799454] Bluetooth: hci1: CSR: Setting up dongle with HCI ver=6 rev=22bb; LMP ver=6 subver=22bb; manufacturer=10

I have connected it now to Home Assistant server and it was detected by Bluetooth integration. So I disabled both ESP32 proxies but noew I no longer receive any readings over BTHome integration.

I am sure I need to configure this differently now. Can you point me in proper direction? Can I still use BTHome as protocol but collect the data over Bluetooth integration with this CSR8510 A10 adapter?

EDIT 1 hour later: I needed to install Passive BLE Monitor component. After setting up I was able to catch 7 of 9 sensors. Painfull to edit all the entities names but at end I had 7 working an collecting. But I was missing two which are the farther away from the HA server. But I still had the ESP32 which I disabled so they did not catch the BTHome devices. I converted them to BLE Gateway and when added and enabled service calls from them, the two missing sensors showned.

image

Please correct me if my steps were wrong or I could do this setup differently with less steps. Thanks.

pvvx commented 6 months ago

Bluez still does not know how to process LE Bluetooth advertising according to the standard.

To work in BTHome in “passive mode” you need to set the following settings on the thermometer:

image

And also change the settings for Bluez.

Add --experimental at the end of the following line : ExecStart=/usr/local/libexec/bluetooth/bluetoothd --experimental

And change the intervals in other Bluez settings files. They are different depending on the version of Bluez.


Check the adapter against a counterfeit item. There are a lot of fakes of the CSR8510 A10 now.

pvvx commented 6 months ago

Passive BLE Monitor uses Black and Bluez. For this reason, it cannot work with BT5.0.

Indeed it was a race condition. After one day those two sensor readings were again unreliable.

He gave a comparison: ESP versus any BT adapter.

Issue statistics show that 80% of problems occur among users using ESP. The rest are divided into those using low-quality BT adapters and Linux. There are no messages from those who use Android (Google Linux version), because everything is fine with BLE there. Apple devices fare worse with BLE. They reinterpret Bluetooth standards in their own way and often do not correct errors in released devices. Windows is different in that it does not have programs for users with support for BT5.0 and higher. Although Microsoft's statement about support for the BT5.0 standard is present.


Bluez

Supported Profiles Lower level Host Stack Core specification 4.2. Not (yet) 3.0+HS. Includes GAP, L2CAP, RFCOMM and SDP.

Bluetooth 4.2 - Released on 2 December 2014

It's like you are using a USB3.0 card and Linux is running it on USB1.1 and no API can switch to USB2.0/3.0 :P

The situation is similar with other hardware and software devices in Linux. Including CPU. Linux development has completely stopped at the level of 2016. Suitable for use on legacy equipment only.