GPIO without root


  • Admin

    I am currently writing a module which uses the GPIO Pins for that the script has to be started as root. I tried running MM with DISPLAY=:0 sudo npm startbut all I get this way is a No 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…


  • Moderator


  • Admin

    That’s nice and all but too complicated for people to setup…
    Adding a sudo somewhere is one thing but that would make installing the module so complicated :confused:
    Seems like it might be possible by using usermod -a -G gpio pi will check and report.


  • Admin

    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 execute sudo 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)
    	}
    });
    

Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.