SOLVED GPIO without root
-
I am currently writing a module which uses the
GPIO Pins
for that the script has to be started asroot
. I tried running MM withDISPLAY=:0 sudo npm start
but all I get this way is aNo protocol specified
error on startup. Any idea how to fix this or how to use wiring-pi without root? Wasn’t there an Raspbian update recently that allowed GPIO access without root? I don’t remember it exactly… -
First Google result brings this up:
https://dissectionbydavid.wordpress.com/2013/10/21/raspberry-pi-using-gpio-wiringpi-without-root-sudo-access/ -
That’s nice and all but too complicated for people to setup…
Adding asudo
somewhere is one thing but that would make installing the module so complicated
Seems like it might be possible by usingusermod -a -G gpio pi
will check and report. -
Found a workaround! Use the shell within node to export the pins. Works without root if the user is added to the
gpio group
, to do that executesudo useradd -g pi gpio
.
Setup the pins
const exec = require('child_process').exec; exec("echo '22' > /sys/class/gpio/export", null); exec("echo 'in' > /sys/class/gpio/gpio22/direction", null); exec("echo '17' > /sys/class/gpio/export", null); exec("echo 'out' > /sys/class/gpio/gpio17/direction", null);
Control the pins
gpio.setup('sys'); gpio.wiringPiISR(22, gpio.INT_EDGE_BOTH, function(delta) { if (gpio.digitalRead(22) == 1) { console.log("High"); gpio.digitalWrite(17, 1) } else if (gpio.digitalRead(22) == 0) { console.log("Low"); gpio.digitalWrite(17, 0) } });