drjayvee / gpio-cli

Command line interface for RaspberryPi's GPIO

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GPIO-cli

About

Many RaspberryPi users ask how to use GPIO without being root.

The short answer is that that's not possible.

The slightly longer answer is that GPIO really needs to be run as root. But that doesn't mean your whole python program needs to be run as root. This python module provides a command line interface (CLI) to GPI, so you can call it from shell, like so:

Using GPIO from shell

# Set pin 13 to HIGH, get its value, then set to LOW
sudo python gpiocli.py set 13 HIGH
sudo python gpiocli.py get 13
sudo python gpiocli.py set 13 LOW -q -v
sudo python gpiocli.py cleanup

Using it from python

import subprocess
subprocess.check_call(["sudo", "python", "gpiocli.py", "set", "13", "HIGH"])
subprocess.check_call(["sudo", "python", "gpiocli.py", "get", "13"])
# alternatively, depending on your shell environment
subprocess.check_call(["sudo python gpiocli.py get 13"], shell=True)

Why use sudo?

It's tempting to use setuid to make the module executable and run as root. However, that doesn't work!

Make script executable

Since the module starts with #!/usr/bin/python, you can chmod +x this script, and then run

sudo ./gpiocli.py 13 HIGH

About

Command line interface for RaspberryPi's GPIO

License:Other


Languages

Language:Python 100.0%