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
549 stars 117 forks source link

Trying to add Xiaomi BLE Temperature and Humidity sensor #80

Closed ThirteenTX closed 5 years ago

ThirteenTX commented 5 years ago

So I am pretty new at this stuff but I was really interested in the project. I tried really hard to figure this out on my own without bothering you first. I bought one of these so I could monitor my back room temperature: https://www.home-assistant.io/components/mitemp_bt/.

The problem is the Pi I run Home Assistant on doesn't have the bluetooth range to read it from the back room. I tested it at short range and it works great. I found a few people say they are using this project to get around that. I bought a Pi zero and started to work on implementing this project.

Here are the issues I have. When I run the ./gateway.py command I get this as the reply:

18:36:35 Starting 18:36:48 Adding 1 mithermometer devices Traceback (most recent call last): File "./gateway.py", line 43, in manager.register_workers().start(mqtt) File "/home/pi/bt-mqtt-gateway/workers_manager.py", line 101, in start mqtt.callbacks_subscription(self._mqtt_callbacks) File "/home/pi/bt-mqtt-gateway/mqtt.py", line 73, in callbacks_subscription self.mqttc.connect(self.hostname, port=self.port) File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 839, in connect return self.reconnect() File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 962, in reconnect sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0)) File "/usr/lib/python3.5/socket.py", line 712, in create_connection raise err File "/usr/lib/python3.5/socket.py", line 703, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

I am not really sure what to do to get around that. Things I am not really clear on as well:

host: 192.168.1.1 <-- Is this local host or where the messages will be accepted port: 1883 <-- do I use this port for home assistant? username: user <-- Is this username to connect to home assistant or local host password: password <-- same as username concerns

I commented out user name and password for now as I wasn't sure.

I am also not sure if I am supposed to use mithermometer: or blescanmulti: to capture this data I've tried both with no success. I see the correct MAC from the Pi when running sudo hcitool lescan. Any ideas? Here is my current config:

host: x.x.x.x #home assistant IP port: 1883

username: user

password: password

topic_prefix: bkrmmqqt # 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:

mysensors:

      #      command_timeout: 35       # Optional override of globally set command_timeout.
      #      args:
      #        port: /dev/ttyUSB0
      #        baudrate: 9600
      #        topic_prefix: mysensors/out
      #    thermostat:
      #      args:
      #        devices:
      #          bedroom: 00:11:22:33:44:55
      #        topic_prefix: thermostat
      #      topic_subscription: thermostat/+/+/set
      #      update_interval: 60
      #    miscale:
      #      args:
      #        mac: 00:11:22:33:44:55
      #        topic_prefix: miscale
      #      update_interval: 1800
      #    linakdesk:
      #      args:
      #        mac: 00:11:22:33:44:55
      #        topic_prefix: linak_desk
      #      update_interval: 1800
      #    miflora:
      #      args:
      #        devices:
      #          herbs: 00:11:22:33:44:55
      #        topic_prefix: miflora
      #      update_interval: 300
mithermometer:
  args:
    devices:
      back_room: XX:XX:XX:XX:XX:XX   <-- ommited MAC of Thermometer
    topic_prefix: mithermometer
  update_interval: 300
  #    blescanmulti:
  #      args:
  #        devices:
  #          mlethermometer : XX:XX:XX:XX:XX:XX   <-- ommited MAC of Thermometer
      #          smartwath: 00:11:22:33:44:55
      #        topic_prefix: blescan
      #        available_payload: home
      #        unavailable_payload: not_home
      #        available_timeout: 0
      #        unavailable_timeout: 60
      #        scan_timeout: 10
      #        scan_passive: true
      #      update_interval: 60
  #    toothbrush:
  #      args:
  #        devices:
  #          ix: 00:11:22:33:44:55
  #          ia: 11:22:33:44:55:66
  #        topic_prefix: toothbrush
  #      update_interval: 10
  #    toothbrush_homeassistant:
  #      args:
  #        autodiscovery_prefix: homeassistant
  #        topic_prefix: toothbrush
  #        devices:
  #          ix:
  #            name: IX
  #            mac: 00:11:22:33:44:55
  #          ia:
  #            name: IA
  #            mac: 11:22:33:44:55:66
  #      update_interval: 10
  #    switchbot:
  #      args:
  #        devices:
  #          heater: 00:11:22:33:44:55
  #        topic_prefix: switchbot/bathroom
  #        state_topic_prefix: switchbot/bathroom
  #      topic_subscription: switchbot/+/+/set
  #      update_interval: 60

Here is what the code looks like in home assistant:

MQTT

mqtt: broker: !secret mqtt_broker_ip birth_message: topic: 'homeassistant/status' payload: 'online'

Any Ideas? I have reimaged this SD card a few times thinking I just messed up installing bluez or bluepy but everything seems fine as far as I can tell. I have also tried making the host the local address but no dice there either.

ThirteenTX commented 5 years ago

Adding debug info if that helps (MAC omitted): pi@mqtt-server:~/bt-mqtt-gateway $ sudo ./gateway.py -d 2019-06-30 19:39:44,349 INFO bt-mqtt-gw gateway.py:39: - Starting 2019-06-30 19:39:44,356 DEBUG bt-mqtt-gw.mqtt mqtt.py:23:init - Setting LWT to: bkrmmqqt/lwt_topic 2019-06-30 19:39:45,513 INFO bt-mqtt-gw.workers.mithermometer mithermometer.py:18:_setup - Adding 1 mithermometer devices 2019-06-30 19:39:45,516 DEBUG bt-mqtt-gw.workers.mithermometer mithermometer.py:20:_setup - Adding mithermometer device 'back_room' (XX:XX:XX:XX:XX:XX) 2019-06-30 19:39:45,520 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:60:register_workers - Added mithermometer config with a 2 seconds timeout 2019-06-30 19:39:45,524 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:65:register_workers - Added mithermometer worker with 300 seconds interval and a 35 seconds timeout Traceback (most recent call last): File "./gateway.py", line 43, in manager.register_workers().start(mqtt) File "/home/pi/bt-mqtt-gateway/workers_manager.py", line 101, in start mqtt.callbacks_subscription(self._mqtt_callbacks) File "/home/pi/bt-mqtt-gateway/mqtt.py", line 73, in callbacks_subscription self.mqttc.connect(self.hostname, port=self.port) File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 839, in connect return self.reconnect() File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 962, in reconnect sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0)) File "/usr/lib/python3.5/socket.py", line 712, in create_connection raise err File "/usr/lib/python3.5/socket.py", line 703, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

zewelor commented 5 years ago

Hi

from error, it looks like you cannot reach mqtt server. Check ip and / or port. Be sure there is routing to given mqtt server. From ha config ip is "!secret mqtt_broker_ip", so check if its the same as provided in bt-mqtt-gateway config.

ThirteenTX commented 5 years ago

So the IP in the config.yaml on the Pi zero is the IP of the bt-mqtt-gateway Pi zero. In home assistant the "!secret mqtt_broker_ip" is also the IP address if the bt-mqtt-gateway Pi zero. I added the port in home assistant just to test but the error is the same. I can ping the home assistant server from the Pi zero. Is that the right way to do it?

ThirteenTX commented 5 years ago

I have tried every combination now and none of them can get past the [Errno 111] Connection refused error.

zewelor commented 5 years ago

Do you have mqtt server ( like mosquito ) on your network ?

ThirteenTX commented 5 years ago

Oh I thought that is what this is. Is that something I can load up in as an add-on in hass.io?

bbbenji commented 5 years ago

Absolutely. There are a few add-ons which provide a mosquito broker. for example: https://www.home-assistant.io/addons/mosquitto/ & https://github.com/hassio-addons/addon-mqtt/blob/master/README.md

ThirteenTX commented 5 years ago

I'll give that a shot and see what I come up with and report back. Thanks to both of you for your time!

ThirteenTX commented 5 years ago

Okay I added this to my installation https://github.com/hassio-addons/addon-mqtt/blob/master/README.md

In the bt-mqtt-gateway config.yaml I set the host as the home assistant IP.

In home assistant I set up the configuration .yaml as: mqtt: broker: a0d7b954-mqtt username: !secret mqtt_username password: !secret mqtt_password client_id: home-assistant Per the instructions and set the relevant passwords.

In the add-on I set the config as follows: { "ssl": true, "certfile": "fullchain.pem", "keyfile": "privkey.pem", "broker": true, "allow_anonymous": false, "mqttusers": [ { "username": "redacted user", "password": "redacted pass", "readonly": true, "topics": [ "#" ] } ] }

I set the user and password in the bt-mqtt-gateway the same as it is in all of these configs.

The add-on logs show this repeatedly:

1562108372: New connection from "redacted ip" on port 1883. 1562108372: Client bt-mqtt-gateway already connected, closing old connection. 1562108372: New client connected from "redacted ip" as bt-mqtt-gateway (p2, c0, k60, u'redacted user').

Running the command "sudo ./gateway.py -d" on the bt-mqtt-gateway repeatedly gives: 2019-07-02 23:47:06,045 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:116:update_all - Updating all workers 2019-07-02 23:47:06,050 INFO bt-mqtt-gw.workers.mithermometer mithermometer.py:39:status_update - Updating 1 mithermometer devices 2019-07-02 23:47:06,054 DEBUG bt-mqtt-gw.workers.mithermometer mithermometer.py:42:status_update - Updating mithermometer device 'back_room' (redacted MAC) 2019-07-02 23:47:09,263 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:36:execute - Execution result of command MithermometerWorker.status_update: [{'payload': 22.6, 'topic': 'mithermometer/back_room/temperature'}, {'payload': 53.6, 'topic': 'mithermometer/back_room/humidity'}, {'payload': 100, 'topic': 'mithermometer/back_room/battery'}] 2019-07-02 23:52:05,997 INFO bt-mqtt-gw.workers.mithermometer mithermometer.py:39:status_update - Updating 1 mithermometer devices 2019-07-02 23:52:06,001 DEBUG bt-mqtt-gw.workers.mithermometer mithermometer.py:42:status_update - Updating mithermometer device 'back_room' (redacted MAC) 2019-07-02 23:52:09,116 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:36:execute - Execution result of command MithermometerWorker.status_update: [{'payload': 22.3, 'topic': 'mithermometer/back_room/temperature'}, {'payload': 54.7, 'topic': 'mithermometer/back_room/humidity'}, {'payload': 100, 'topic': 'mithermometer/back_room/battery'}] 2019-07-02 23:57:06,003 INFO bt-mqtt-gw.workers.mithermometer mithermometer.py:39:status_update - Updating 1 mithermometer devices 2019-07-02 23:57:06,008 DEBUG bt-mqtt-gw.workers.mithermometer mithermometer.py:42:status_update - Updating mithermometer device 'back_room' (redacted MAC) 2019-07-02 23:57:11,124 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:36:execute - Execution result of command MithermometerWorker.status_update: [{'payload': 22.8, 'topic': 'mithermometer/back_room/temperature'}, {'payload': 54.7, 'topic': 'mithermometer/back_room/humidity'}, {'payload': 100, 'topic': 'mithermometer/back_room/battery'}] 2019-07-03 00:02:06,008 INFO bt-mqtt-gw.workers.mithermometer mithermometer.py:39:status_update - Updating 1 mithermometer devices 2019-07-03 00:02:06,013 DEBUG bt-mqtt-gw.workers.mithermometer mithermometer.py:42:status_update - Updating mithermometer device 'back_room' (redacted MAC) 2019-07-03 00:02:11,135 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:36:execute - Execution result of command MithermometerWorker.status_update: [{'payload': 22.9, 'topic': 'mithermometer/back_room/temperature'}, {'payload': 54.1, 'topic': 'mithermometer/back_room/humidity'}, {'payload': 100, 'topic': 'mithermometer/back_room/battery'}]

So it seems like it is doing something. The issue I have now is how do I get that information into an entity in home assistant so I can track it?

zewelor commented 5 years ago

You can try newest version, which should support home assistant auto discovery ( https://www.home-assistant.io/docs/mqtt/discovery/ ) or manually configure everything https://github.com/zewelor/bt-mqtt-gateway/wiki/Home-Assistant

ThirteenTX commented 5 years ago

One thing I have never been clear on. Is this supposed to run on its own? Like in the bt-mqyy-gateway config.yaml, is "host" on line supposed to point at itself for IP or another server?

ThirteenTX commented 5 years ago

Another thing. Does this not work on stretch lite? Is that my issue. None of this information ever seems to be useable in home assistant.

zewelor commented 5 years ago

Host should point to mqtt server ip. It needs to send mqtt messages to it, and HA then reads it from mqtt server. It should work on stretch. From your log, it seems its working fine. Just configure mqtt sensors in HA and should be ready.

ThirteenTX commented 5 years ago

I really want to thank you again for bearing with me on this one! I am really new at this a I rewrote my SD card for this 6 times over and rebuilt my home assistant from scratch thinking that may be the issue. I probably have 30-40 hours in this but I learned a lot! I didn't see a "buy me a coffee" link but I would be glad to donate to the cause.

You were right I needed to make sensors in my sensor.yaml file. Here are the examples from mine that are now working as intended.

MQTT

The working config from the https://github.com/hassio-addons/addon-mqtt/blob/master/README.md add-on. I had to make changes like SSL to allow it to ingest messages.

{ "ssl": false, "certfile": "fullchain.pem", "keyfile": "privkey.pem", "broker": true, "allow_anonymous": false, "mqttusers": [ { "username": "redacted", "password": "redacted", "readonly": false, "topics": [ "#" ] } ], "log_level": "trace" }

The config.yaml in bt-mqtt-gateway that worked for me. host: x.x.x.x #home assistant IP port: 1883

username: redacted

password: redacted

topic_prefix: bkrmmqqt # 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: back_room: XX:XX:XX:XX:XX:XX <-- ommited MAC of Thermometer topic_prefix: mithermometer update_interval: 300

I hope these configs help some other lost person like me trying to do the same. Anyways you can close this one out as I am good :)

zewelor commented 5 years ago

Great it worked. There is some learning curve but after that its easy to add more and more stuff :D