zewelor / bt-mqtt-gateway

A simple Python script which provides a Bluetooth to MQTT gateway, easily extensible via custom workers. See https://github.com/zewelor/bt-mqtt-gateway/wiki for more information.
MIT License
548 stars 116 forks source link

Won't connect (most of the time) #154

Open ThirteenTX opened 4 years ago

ThirteenTX commented 4 years ago

[] I've read the Troubleshooting Wiki, my problem is not described there and I am already using the specified minimum bluez version.

Describe the bug Hello, back again. I have been using my older build of this for a while now and its been amazing. Trying to run the gateway.py on a new pi zero timesout almost every attempt. I can see it in a scan and confirmed the MAC. Here is what I get in the python virtual environment on a raspberry Pi zero with buster.

sudo ./gateway.py 05:12:10 Starting WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. To avoid this problem you can invoke Python with '-m pip' instead of running pip directly. WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. To avoid this problem you can invoke Python with '-m pip' instead of running pip directly. 05:12:20 Adding 1 mithermometer devices 05:12:21 Updating 1 mithermometer devices 05:12:27 Time out during update of mithermometer device 'humidor' (58:2d:34:39:82:87)

Also in the guide which may have been my initial problem. After activating the .venv from the guide do you all run all commands from that shell or do you need to back out to run some of them? I am on bluez 5.5

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior It runs like the previous version I have that was installed outside of the .venv

Config

mqtt:
  host: home assistant mqtt server
  port: 1883
  username: myuser
  password: mypass
  #ca_cert: /etc/ssl/certs/ca-certificates.crt # Uncomment to enable MQTT TLS, update path to appropriate location.
  #ca_verify: False              # Verify TLS certificate chain and host, disable for testing with self-signed certificates, default to True
  topic_prefix: hostname         # All messages will have that prefix added, remove if you dont need this.
  client_id: bt-mqtt-gateway
  availability_topic: lwt_topic
manager:
  sensor_config:
    topic: homeassistant
    retain: true
  topic_subscription:
    update_all:
      topic: homeassistant/status
      payload: online
  command_timeout: 35           # Timeout for worker operations. Can be removed if the default of 35 seconds is sufficient.
  workers:
    mithermometer:
      args:
        devices:
          humidor: 58:2d:34:39:82:87
        topic_prefix: mithermometerhd
      update_interval: 300

With the rest commented out.

Debug gateway logs

pi@raspberrypi:~/bt-mqtt-gateway $ sudo ./gateway.py -d 2020-02-21 05:34:00,088 INFO bt-mqtt-gw gateway.py:76: - Starting 2020-02-21 05:34:00,095 DEBUG bt-mqtt-gw.mqtt mqtt.py:30:init - Setting LWT to: hostname/lwt_topic WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. To avoid this problem you can invoke Python with '-m pip' instead of running pip directly. WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. To avoid this problem you can invoke Python with '-m pip' instead of running pip directly. 2020-02-21 05:34:10,617 INFO bt-mqtt-gw.workers.mithermometer mithermometer.py:19:_setup - Adding 1 mithermometer devices 2020-02-21 05:34:10,620 DEBUG bt-mqtt-gw.workers.mithermometer mithermometer.py:21:_setup - Adding mithermometer device 'humidor' (58:2d:34:39:82:87) 2020-02-21 05:34:10,623 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:96:register_workers - Added mithermometer config with a 2 seconds timeout 2020-02-21 05:34:10,626 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:106:register_workers - Added mithermometer worker with 300 seconds interval and a 35 seconds timeout 2020-02-21 05:34:11,717 DEBUG bt-mqtt-gw.mqtt mqtt.py:116:callbacks_subscription - Subscribing to: hostname/mithermometerhd/update_interval 2020-02-21 05:34:11,723 DEBUG bt-mqtt-gw.mqtt mqtt.py:116:callbacks_subscription - Subscribing to: hostname/homeassistant/status 2020-02-21 05:34:11,735 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:67:execute - Execution result of command MithermometerWorker.config: [{'topic': 'sensor/58-2d-34-39-82-87/mithermometer_humidor_temperature/config', 'payload': '{"unique_id": "bt-mqtt-gateway/58-2d-34-39-82-87/mithermometer_humidor_temperature", "name": "mithermometer_humidor_temperature", "state_topic": "hostname/mithermometerhd/humidor/temperature", "device_class": "temperature", "device": {"identifiers": ["58:2d:34:39:82:87", "bt-mqtt-gateway/58-2d-34-39-82-87/mithermometer_humidor"], "manufacturer": "Xiaomi", "model": "LYWSD(CGQ/01ZM)", "name": "mithermometer_humidor"}, "unit_of_measurement": "\u00b0C"}'}, {'topic': 'sensor/58-2d-34-39-82-87/mithermometer_humidor_humidity/config', 'payload': '{"unique_id": "bt-mqtt-gateway/58-2d-34-39-82-87/mithermometer_humidor_humidity", "name": "mithermometer_humidor_humidity", "state_topic": "hostname/mithermometerhd/humidor/humidity", "device_class": "humidity", "device": {"identifiers": ["58:2d:34:39:82:87", "bt-mqtt-gateway/58-2d-34-39-82-87/mithermometer_humidor"], "manufacturer": "Xiaomi", "model": "LYWSD(CGQ/01ZM)", "name": "mithermometer_humidor"}, "unit_of_measurement": "%"}'}, {'topic': 'sensor/58-2d-34-39-82-87/mithermometer_humidor_battery/config', 'payload': '{"unique_id": "bt-mqtt-gateway/58-2d-34-39-82-87/mithermometer_humidor_battery", "name": "mithermometer_humidor_battery", "state_topic": "hostname/mithermometerhd/humidor/battery", "device_class": "battery", "device": {"identifiers": ["58:2d:34:39:82:87", "bt-mqtt-gateway/58-2d-34-39-82-87/mithermometer_humidor"], "manufacturer": "Xiaomi", "model": "LYWSD(CGQ/01ZM)", "name": "mithermometer_humidor"}, "unit_of_measurement": "%"}'}] 2020-02-21 05:34:11,772 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:172:update_all - Updating all workers 2020-02-21 05:34:11,778 INFO bt-mqtt-gw.workers.mithermometer mithermometer.py:69:status_update - Updating 1 mithermometer devices 2020-02-21 05:34:11,781 DEBUG bt-mqtt-gw.workers.mithermometer mithermometer.py:72:status_update - Updating mithermometer device 'humidor' (58:2d:34:39:82:87) 2020-02-21 05:34:17,788 ERROR bt-mqtt-gw.workers.mithermometer logger.py:50:log_exception - Time out during update of mithermometer device 'humidor' (58:2d:34:39:82:87) Traceback (most recent call last): File "/home/pi/bt-mqtt-gateway/workers/mithermometer.py", line 76, in status_update yield self.update_device_state(name, data["poller"]) File "/usr/local/lib/python3.7/dist-packages/interruptingcow/init.py", line 153, in inner return func(*args, *kwargs) File "/home/pi/bt-mqtt-gateway/workers/mithermometer.py", line 105, in update_device_state payload=poller.parameter_value(attr), File "/usr/local/lib/python3.7/dist-packages/mithermometer/mithermometer_poller.py", line 136, in parameter_value self.fill_cache() File "/usr/local/lib/python3.7/dist-packages/mithermometer/mithermometer_poller.py", line 60, in fill_cache self.firmware_version() File "/usr/local/lib/python3.7/dist-packages/mithermometer/mithermometer_poller.py", line 109, in firmware_version with self._bt_interface.connect(self._mac) as connection: File "/usr/local/lib/python3.7/dist-packages/btlewrap/base.py", line 47, in enter self._backend.connect(self._mac) File "/usr/local/lib/python3.7/dist-packages/btlewrap/bluepy.py", line 27, in _func_wrapper return func(args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/btlewrap/bluepy.py", line 56, in connect self._peripheral = Peripheral(mac, iface=iface, addrType=self.address_type) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 391, in init self._connect(deviceAddr, addrType, iface) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 436, in _connect rsp = self._getResp('stat') File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 407, in _getResp resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 347, in _waitResp rv = self._helper.stdout.readline() File "/usr/local/lib/python3.7/dist-packages/interruptingcow/init.py", line 74, in handler raise exception exceptions.DeviceTimeoutError 2020-02-21 05:34:17,849 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:67:execute - Execution result of command MithermometerWorker.status_update: []

Server (please complete the following information):

cybe commented 4 years ago

@zewelor I face the same issue in general. This is why I patch the per device timeout to 15 seconds in my setup. That can interfere with the overall timeout though, as that value might be exceeded.

The whole timeout aspect is quite finicky. You need to balance the overall timeout (optionally overridden within a worker), the update interval, plus the number of devices.

@ThirteenTX Please try to increase the per device timeout value.

ggggh commented 4 years ago

Similar situation here. In general, a lot of device timeouts, but especially to those devices which are a bit further away.

But, more importantly, running on the docker version of this on an raspberry pi 4, and finding I'm having to restart the docker container a couple of times a day as it suddenly stops working completely, with all measurements timing out. After a restart, it's back to the standard situation with some timing out.

ThirteenTX commented 4 years ago

I eventually moved to ESPhome.io for home assistant. It has worked flawlessly. I still have the older pre-python venv version of this running and it works without issue but the newer version won't connect consistently at all.