Read the statement by Michael Teeuw here.
GPIO without root
-
I am currently writing a module which uses the
GPIO Pinsfor that the script has to be started asroot. I tried running MM withDISPLAY=:0 sudo npm startbut all I get this way is aNo protocol specifiederror 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 asudosomewhere is one thing but that would make installing the module so complicated :confused:
Seems like it might be possible by usingusermod -a -G gpio piwill 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) } });
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login