mattjlewis / diozero

Java Device I/O library that is portable across Single Board Computers and microcontrollers. Tested with Raspberry Pi, Odroid C2, BeagleBone Black, Next Thing CHIP, Asus Tinker Board and Arduinos / Pico. Supports GPIO, I2C, SPI as well as Serial communication. Also known to work with Udoo Quad.
https://www.diozero.com
MIT License
261 stars 59 forks source link

Using pigpio without root #171

Closed RankoR closed 1 year ago

RankoR commented 1 year ago

Hi, I'm trying to use your library with pigpio for native PWM support on Pi 4 B:

Linux raspberrypi 6.1.34-v8+ #1657 SMP PREEMPT Fri Jun 16 12:36:29 BST 2023 aarch64 GNU/Linux.

My user is in the gpio group:

uid=1000(rankor) gid=1000(rankor) groups=1000(rankor),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),60(games),100(users),104(input),106(render),108(netdev),995(docker),997(gpio),998(i2c),999(spi)

/dev/gpiomem has correct permissions:

crw-rw---- 1 root gpio 245, 0 Jun 20 01:17 /dev/gpiomem.

When I'm trying to run my program without root permissions, it fails to initialize PiGPIO:

Caused by: java.lang.RuntimeException: Error initialising pigpio (must be run as root!): -1
        at uk.pigpioj.PigpioJNI.<init>(PigpioJNI.java:53)
        at uk.pigpioj.PigpioJ.autoDetectedImplementation(PigpioJ.java:22)
        at com.diozero.internal.provider.pigpioj.PigpioJDeviceFactory.<init>(PigpioJDeviceFactory.java:84)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:780)

But when I run it with sudo, it works fine.

I prefer not to run programs as root when possible. Is there a way to run it as a usual user?

mattjlewis commented 1 year ago

pigpio when loaded as a shared library requires root access unfortunately. You can run using the sockets interface if need be. Note native PWM works with the built in device factory on 2 pins - I assume you need more than 2.