Airthings / waveplus-reader

MIT License
133 stars 61 forks source link

Raspberry pi 3B+ Python3, sudo privilege required #28

Open beltsanderxyz opened 3 years ago

beltsanderxyz commented 3 years ago

I've got waveplus-reader working on my RPi 3B+ using Python3. I had to reformat the print statements, run in a virtual environment, and format my call "sudo python3 read_waveplus.py serial_number 60".

I am trying to figure out why sudo privileges are required and how to avoid needing that in the future. The error I get without sudo privilege is below:

Traceback (most recent call last): File "read_waveplus.py", line 221, in <module> waveplus.connect() File "read_waveplus.py", line 119, in connect devices = scanner.scan(0.1) # 0.1 seconds scan period File "/home/pi/MyEnv/waveplus/lib/python3.7/site-packages/bluepy/btle.py", line 734, in scan self.start(passive=passive) File "/home/pi/MyEnv/waveplus/lib/python3.7/site-packages/bluepy/btle.py", line 672, in start self._mgmtCmd("le on") File "/home/pi/MyEnv/waveplus/lib/python3.7/site-packages/bluepy/btle.py", line 283, in _mgmtCmd "Failed to execute mgmt cmd '%s'" % (cmd)) bluepy.btle.BTLEException: Failed to execute mgmt cmd 'le on'

stiandre commented 2 years ago

I had the same issue, and didn't want to run my python app with root privileges. I solved it by giving bluepy-helper the appropriate permissions to execute mgmt cmd: 'le on' by executing the following command:

sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper

It looks like you are using a virtual environment (MyEnv), and then you can just set it for bluepy-helper within that particular environment. I am using pyenv and poetry, so I made a tiny shell script to fix it:

#!/bin/sh

PROJECT_VENV=$(poetry env info -p)
sudo setcap 'cap_net_raw,cap_net_admin+eip' $PROJECT_VENV/lib/python3.10/site-packages/bluepy/bluepy-helper

@beltsanderxyz