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

Possibly the installation instructions are not correct (SUDO is not used consistently) #244

Closed lionhe1966 closed 2 years ago

lionhe1966 commented 2 years ago

I have tried installing bt-mqtt-gateway on a fresh installation of bullseye on my Raspberry 0W as of 09/01/2022 I followed exactly the installation instructions. The interactive command sudo ./gateway.sh worked but the service did not start. This is the same as #195 (Error with the service startup).

I then erased the installation and started again, trying to understand what the various installation commands actually do.

sudo apt-get install git python3 python3-pip python3-wheel bluetooth bluez libglib2.0-dev sudo pip3 install virtualenv git clone https://github.com/zewelor/bt-mqtt-gateway.git cd bt-mqtt-gateway virtualenv -p python3 .venv source .venv/bin/activate

Up to here everything is correct. The culprit, in my opinion is the following sudo pip3 install -r requirements.txt this command does not seem to install the packages into the virtual environment. In my second (succesful) attempt I gave this command without sudo.

Then instructions tell to install other dependencies without sudo, therefore packages are installed in the virtual enviroment (I have run this command after configuring the file configure.yaml)

pip3 install `./gateway.py -r configured`

In my first attempt in which I followed rigorously instructions, I tested the installation with the sudo command interactively

source .venv/bin/activate sudo ./gateway.py

but in my second attempt, sudo was not necessary.

source .venv/bin/activate ./gateway.py

So far the interactive test was successfull in both cases, but only when the installation is performed without sudo, the service starts correctly (following instructions it complains that yaml or paho-mqtt are not available).

My explanation is that with sudo pip3 install -r requirements.txt the installation of some packages is not performed in the virtual environment. The solutions suggested in #195 basically aim at installing all required packages with sudo, outside the virtual environment.

jumping2000 commented 2 years ago

Hello, I tried your configuration without "sudo" but I have some errors

2022-01-15 11:37:43,310 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:168:update_all - Updating all workers
2022-01-15 11:37:43,371 ERROR bt-mqtt-gw logger.py:50:log_exception - Fatal error while executing worker command: BTLEManagementError
Traceback (most recent call last):
  File "/home/dietpi/bt-mqtt-gateway/./gateway.py", line 90, in <module>
    mqtt.publish(_WORKERS_QUEUE.get(timeout=10).execute())
  File "/home/dietpi/bt-mqtt-gateway/workers_manager.py", line 48, in execute
    messages = self._callback(*self._args)
  File "/home/dietpi/bt-mqtt-gateway/workers/miscale.py", line 28, in status_update
    results = self._get_data()
  File "/home/dietpi/bt-mqtt-gateway/workers/miscale.py", line 105, in _get_data
    scanner.scan(self.SCAN_TIMEOUT, passive=True)
  File "/home/dietpi/bt-mqtt-gateway/.venv/lib/python3.9/site-packages/bluepy/btle.py", line 852, in scan
    self.start(passive=passive)
  File "/home/dietpi/bt-mqtt-gateway/.venv/lib/python3.9/site-packages/bluepy/btle.py", line 790, in start
    self._mgmtCmd("le on")
  File "/home/dietpi/bt-mqtt-gateway/.venv/lib/python3.9/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
Traceback (most recent call last):
  File "/home/dietpi/bt-mqtt-gateway/./gateway.py", line 108, in <module>
    raise e
  File "/home/dietpi/bt-mqtt-gateway/./gateway.py", line 90, in <module>
    mqtt.publish(_WORKERS_QUEUE.get(timeout=10).execute())
  File "/home/dietpi/bt-mqtt-gateway/workers_manager.py", line 48, in execute
    messages = self._callback(*self._args)
  File "/home/dietpi/bt-mqtt-gateway/workers/miscale.py", line 28, in status_update
    results = self._get_data()
  File "/home/dietpi/bt-mqtt-gateway/workers/miscale.py", line 105, in _get_data
    scanner.scan(self.SCAN_TIMEOUT, passive=True)
  File "/home/dietpi/bt-mqtt-gateway/.venv/lib/python3.9/site-packages/bluepy/btle.py", line 852, in scan
    self.start(passive=passive)
  File "/home/dietpi/bt-mqtt-gateway/.venv/lib/python3.9/site-packages/bluepy/btle.py", line 790, in start
    self._mgmtCmd("le on")
  File "/home/dietpi/bt-mqtt-gateway/.venv/lib/python3.9/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
giddyhup commented 2 years ago

Hi @jumping2000, I ran into the same error and -- after some extensive DuckDuckGoing -- I now think I fixed it by allowing anyone to run bluepy-helper:

sudo setcap 'cap_net_raw,cap_net_admin+eip' .venv/lib/python3.9/site-packages/bluepy/bluepy-helper

zewelor commented 2 years ago

I've fixed README some time ago. Maybe its good now ? If not please reopen