Libmraa, mraa-gpio needs sudo

I’m trying to port an existing application from RPI to the RockPI s.
This is a node.js application. It’s running fine on the rockPi except for the following:

I installed libmraa. So when i do:

sudo mraa-gpio get 16

I get : Pin 16 = 0
Or : Pin 16 = 1 when button is pressed.

So this is already something. However, I’ve been struggling to make this work without sudo.
The NPM package mraa is unusable since it’s not updated and requires a very old version of node.

So i should be able to change / poll GPIO’s in CLI without sudo, to use GPIO on the rockPi.

I use the default ubuntu image.

Could somebody help me with this?

I found a working solution at last!

First, create a group and add the user that needs access to the GPIOs to it (this is for the current user;
replace $USER with an explicit username if you want).

sudo groupadd gpio

sudo usermod -a -G gpio $USER

Now reboot

Second, add the following lines to /etc/rc.local (you will have to edit it as root, using sudo vi or whatever).
chown -R root:gpio /sys/class/gpio

chmod -R ug+rw /sys/class/gpio

Third, create a new udev rule in a file, say /etc/udev/rules.d/80-gpio-noroot.rules (you will have to edit it as root again):

# /etc/udev/rules.d/80-gpio-noroot.rules

 
# Corrects sys GPIO permissions on the Joule so non-root users in the gpio group can manipulate bits

 
# Change group to gpio

 
SUBSYSTEM=="gpio", PROGRAM="/bin/sh -c '/bin/chown -R root:gpio /sys/devices/platform/INT34D1:*/gpio'"

 
# Change user permissions to ensure user and group have read/write permissions

 
SUBSYSTEM=="gpio", PROGRAM="/bin/sh -c '/bin/chmod -R ug+rw /sys/devices/platform/INT34D1:*/gpio'"

Fourth, reboot or restart udev using
sudo udevadm trigger --subsystem-match=gpio

And finally test:
mraa-gpio get 16

The first call will not do anything but set up the GPIO, so the value will be -1, but following calls should work. And return a value between 0-1.

I have the same problem. I tried the solution but it does not work for my system: ROCK 4 SE + Debian Bullseye because /sys/devices/platform is different (no INT34D1), I have /sys/devices/platform/pinctrl/gpio with gpio71 and gpiochip032/64/96/128folders. So tried using pinctrl instead of INT34D1, but this did not work…

This tutorial works for gpio, but not as yet for pwm…

The solution above is OK for C program examples but it does not work for C++ examples. I need to access C++ code from my C++ application, but the linker fails if I link C code that does not have ‘main’.

All very frustrating…
OK now I have mraa working without sudo with C++ code…
1)
change the /etc/udev/rules.d/50-gpio.rules file in the tutorial above to:

SUBSYSTEM==“gpio*”, PROGRAM="/bin/sh -c ’
chown -R root:gpiouser /sys/class/gpio && chmod -R 770 /sys/class/gpio;
chown -R root:gpiouser /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/platform/pinctrl/gpio;
chown -R root:gpiouser /sys$devpath && chmod -R 770 /sys$devpath
'"
2)
Now you have to perform a system mraa-gpio get TWICE for all the pins you are using. If you don’t do that then the program will not run.
Presumably because the initialization code in the examples does not completely initialize completely.

The solution above is OK for C program examples but it does not work for C++ examples. I need to access C++ code from my C++ application, but the linker fails if I link C code that does not have ‘main’.

What makes you think that the solution is not applicable to C++? Which solution exactly are you referring to?

And why does linking C code to your C++ application fail? Which command line do you use to link and which error message do you get?

See my last amended post. So mraa GPIO now works as a user for C++ programs.

I am trying to find a way to make the PWM work as a user, C++ code works but only as root.

I guess there is not a lot of incentive to work on this because now mraa is deprecated as it uses sysfs: see https://sergioprado.blog/new-linux-kernel-gpio-user-space-interface/, a new kernel (>4.7) is required for Debian…

Any news on that?

I have the same problems with the latest debian bullseye image (with kernel 5).

Is there a complete solution to be able to access the gpio’s from the nodejs onoff lib?