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

problems with docker #198

Closed gingerlime closed 3 years ago

gingerlime commented 3 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 having problems running with docker on rpi 3

There were a few initial issues I managed to overcome

  1. The config.yaml wasn't found. This workaround seems to fix it.
  2. I was getting Can't down device hci0: Operation not permitted (1) errors. setcap seems to fix this.
  3. Now it seems to run, but I cannot connect via mosquitto_sub ... I couldn't find a solution for this
# mosquitto_sub -h localhost -p 1883 -d -t "#" -u user -P password
Client mosq/A09Uauus4ufhW9UrK9 sending CONNECT
Client mosq/A09Uauus4ufhW9UrK9 received CONNACK (5)
Connection error: Connection Refused: not authorised.
Client mosq/A09Uauus4ufhW9UrK9 sending DISCONNECT

To Reproduce Steps to reproduce the behavior:

  1. Start the gateway via docker
  2. Run mosquitto_sub -h localhost -p 1883 -d -t "#" -u user -P password
  3. see error

I tried removing the username/password from my config.yaml, but it still fails. Based on the output, it connects (there's a CONNACK), but it's not authorized

Expected behavior It should connect ?

Config

mqtt:
  host: 192.168.0.99
  port: 1883
  username: user
  password: password
  #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: pi         # 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:
    ruuvitag:
      args:
        devices:
          my_device: aa:bb:cc:dd:ee:ff
        topic_prefix: ruuvitag
      update_interval: 60

Debug gateway logs

docker run --cap-add=NET_ADMIN --cap-add=NET_RAW -e DEBUG=true -ti --rm --name bt-mqtt-gateway --network=host -v $PWD/config.yaml:/config.yaml -v $PWD/config.yaml:/application/config.yaml zewelor/bt-mqtt-gateway
Start in debug mode
2020-12-21 17:25:46,401 INFO bt-mqtt-gw gateway.py:76:<module> - Starting
2020-12-21 17:25:46,403 DEBUG bt-mqtt-gw.mqtt mqtt.py:30:__init__ - Setting LWT to: pi/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.
2020-12-21 17:25:48,720 INFO bt-mqtt-gw.workers.ruuvitag ruuvitag.py:59:_setup - Adding 1 ruuvitag devices
2020-12-21 17:25:48,720 DEBUG bt-mqtt-gw.workers.ruuvitag ruuvitag.py:61:_setup - Adding ruuvitag device 'my_device' (aa:bb:cc:dd:ee:ff)
2020-12-21 17:25:48,723 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:96:register_workers - Added ruuvitag config with a 2 seconds timeout
2020-12-21 17:25:48,723 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:103:register_workers - Added ruuvitag worker with 60 seconds interval and a 35 seconds timeout
2020-12-21 17:25:49,165 DEBUG bt-mqtt-gw.mqtt mqtt.py:117:callbacks_subscription - Subscribing to: pi/ruuvitag/update_interval
2020-12-21 17:25:49,166 DEBUG bt-mqtt-gw.mqtt mqtt.py:117:callbacks_subscription - Subscribing to: pi/homeassistant/status
2020-12-21 17:25:49,170 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:68:execute - Execution result of command RuuvitagWorker.config: [{'topic': <redacted>}]
2020-12-21 17:25:49,187 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:173:update_all - Updating all workers
2020-12-21 17:25:49,247 INFO bt-mqtt-gw.workers.ruuvitag ruuvitag.py:117:status_update - Updating 1 ruuvitag devices
2020-12-21 17:25:49,247 DEBUG bt-mqtt-gw.workers.ruuvitag ruuvitag.py:119:status_update - Updating ruuvitag device 'my_device' (aa:bb:cc:dd:ee:ff)
...

Server (please complete the following information):

zewelor commented 3 years ago

Looks like mqtt server doesn't authorize connection ? Maybe check mqtt server logs ?

gingerlime commented 3 years ago

Thanks @zewelor

Ah, so the mqtt server runs outside / independently of the docker image?

I didn’t realise I might have one running on the pi and assumed it’s bundled and running inside the docker image...

gingerlime commented 3 years ago

shouldn’t the debug logs show that the gateway cannot publish to the mqtt server in this case?

zewelor commented 3 years ago

Add info in README, about mqtt server requirement, I hope it will be clear now. Mqtt connection should exit after some time, when it cannot connect. I've tested with wrong mqtt server ip, and it exit after few seconds. Other error you wrote also should be fixed.

gingerlime commented 3 years ago

That's great. Thanks for your help @zewelor!

I now have a different problem/question but I'll ask separately... :)