darrylb123 / usbrelay

Control usb relay - based on hidapi
GNU General Public License v2.0
315 stars 98 forks source link

question (I need help) - Respbarry pi open usb relay with pi user error - Fehlerdatei "hid.pyx" #91

Closed staebchen0 closed 2 years ago

staebchen0 commented 2 years ago

Hi, I'm a bit desperate now and looking for a solution. While searching I came across the following description of the problem. [(https://github.com/darrylb123/usbrelay/issues/15#issuecomment-558004832)]

Maybe someone can help me to solve the problem? I also posted my problem on Stackflow, so far without an answer.

here is my error description: I cannot address the USB relay with the pi user. (it only works with the root user) System Info: raspberry pi 4 pyhton 3.9

Test pi user:

pi@raspi:~ $ python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hid
>>> dev = hid.device()
>>> dev.open(5824,1503)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "hid.pyx", line 127, in hid.device.open
OSError: open failed
>>> 

Test root user: When I run the code with root privileges it works

root@raspi:/home/pi# python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hid
>>> dev = hid.device()
>>> dev.open(5824,1503)
>>> 

I created the following role with root (there is no other way) 50-usbrelay.rules Folder: /etc/udev/rules.d

SUBSYSTEM=="usb", ATTR{idVendor}=="16c0",ATTR{idProduct}=="05df", MODE="0666"
KERNEL=="hidraw*", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05df", MODE="0660", GROUP="usbrelay", TAG+="systemd" ENV{SYSTEMD_WANTS}= "usbrelayd.service"
KERNEL=="hidraw*", ATTRS{idVendor}=="5824", ATTRS{idProduct}=="1503", MODE="0660", GROUP="usbrelay", SYMLINK+="usbrelay%b"

updated with: sudo udevadm control --reload

I also changed the write permissions: root@raspi:/# cd etc/udev

drwxr-xr-x 2 root root 4096 20. Mär 20:55 hwdb.d drwxr-xr-x 2 root root 4096 14. Sep 19:32 rules.d -rw-r--r-- 1 root root 305 2. Feb 2021 udev.conf

root@raspi:/etc/udev/rules.d# ls -l
insgesamt 8
-rwxr-xr-x 1 root root  357 14. Sep 19:30 50-usbrelay.rules
-rwxr-xr-x 1 root root 2254 14. Sep 19:32 99-com.rules

the pi user is also in the "usbrelay" group, so it should be able to access it

pi@raspi:~ $  getent group usbrelay
usbrelay:x:1001:pi

I've also tried restarting the Pi and unplugging the USB relay cable and plugging it back in What do I have to do so that the pi user also has the right to execute?

Thanks in advance!

staebchen0 commented 2 years ago

after trying again, it works now. I always had before with an existing group (plugdev) My last attempt was to create a new group "usbrelay" and assign the pi user

If someone has the same problem, it would be worth a try ;-)