zedin
Posts: 38
Joined: Tue Oct 18, 2011 1:20 pm

Accessing gpio in python without sudo

Wed Sep 18, 2013 3:30 pm

I am trying to figure out if there is a way to access and use the gpio in python without having to run the program with sudo. Was always taught that sudo use should be very limited and wanted to know if there was another way to get at the gpio. The only thing remotely that I can see is using the shell command from wiring pi but not sure how well that will work from within python or how cumbersome.

Or should I just ignore what I was taught and run everything with sudo?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Accessing gpio in python without sudo

Wed Sep 18, 2013 3:39 pm

For now you'll just have to use sudo, but I'm sure I read somewhere recently that Ben Croston said he hopes to add the ability for his RPi.GPIO to be usable by non-root users at some point.

User avatar
croston
Posts: 705
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Accessing gpio in python without sudo

Wed Sep 18, 2013 3:43 pm

As @AndrewS says, I intend to get RPi.GPIO to work without root privileges. To watch progress on this, keep an eye on this issue report:

http://code.google.com/p/raspberry-gpio ... tail?id=26

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Accessing gpio in python without sudo

Wed Sep 18, 2013 3:47 pm

You can launch pigpio as a daemon (which does require root privileges). Thereafter you can access the gpios through a socket or pipe interface without needing to use sudo.

http://abyz.co.uk/rpi/pigpio/

zedin
Posts: 38
Joined: Tue Oct 18, 2011 1:20 pm

Re: Accessing gpio in python without sudo

Wed Sep 18, 2013 4:02 pm

Thanks for the info. Will try the daemon for now and keep hitting refresh on the RPi.gpio page =)

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Accessing gpio in python without sudo

Wed Sep 18, 2013 6:03 pm

This is my third Python program. So I'm sure it breaks all sorts of style rules.

It demonstrates how to use the socket interface to pigpio from Python.

An advantage of using a socket interface is being able to control/manipulate the gpios over a network. You can run all the CPU intensive stuff on your PC, useful if you want to graph rapid gpio level changes.

Code: Select all

#!/usr/bin/python

import socket
import struct

# Macro        Cmd Function           P1      P2           Result
# =====        === ========           ==      ==           ======
# PI_CMD_MODES  0  Set Mode           gpio    mode         status
# PI_CMD_MODEG  1  Get Mode           gpio    -            mode
# PI_CMD_PUD    2  Set pull up/down   gpio    value        status
# PI_CMD_READ   3  Read               gpio    -            level
# PI_CMD_WRITE  4  Write              ugpio   level        status
# PI_CMD_PWM    5  PWM                ugpio   dutycycle    status
# PI_CMD_PRS    6  Set PWM range      ugpio   range        real_range
# PI_CMD_PFS    7  Set PWM frequency  ugpio   frequency    set_frequency
# PI_CMD_SERVO  8  Servo              ugpio   pulsewidth   status
# PI_CMD_WDOG   9  Set watchdog       ugpio   timeout      status
# PI_CMD_BR1   10  Read bank 1        -       -            levels
# PI_CMD_BR2   11  Read bank 2        -       -            levels
# PI_CMD_BC1   12  Clear bank 1       bits    -            0
# PI_CMD_BC2   13  Clear bank 2       bits    -            0
# PI_CMD_BS1   14  Set bank 1         bits    -            0
# PI_CMD_BS2   15  Set bank 2         bits    -            0
# PI_CMD_TICK  16  Tick               -       -            tick
# PI_CMD_HWVER 17  H/W Ver.           -       -            revision
# PI_CMD_NO    18  Notify open        -       -            handle
# PI_CMD_NB    19  Notify begin       handle  bits         status
# PI_CMD_NP    20  Notify pause       handle  -            status
# PI_CMD_NC    21  Notify close       handle  -            status
# PI_CMD_PRG   22  Get PWM range      ugpio   -            range
# PI_CMD_PFG   23  Get PWM frequency  ugpio   -            frequency
# PI_CMD_PRRG  24  Get PWM real range ugpio   -            real_range

def pigpioCmd(s, cmd, p1, p2):
   s.send(struct.pack('IIII', cmd, p1, p2, 0))
   x, y, z, res = struct.unpack('IIII', s.recv(16))
   return res
   

HOST = 'soft'    # The remote host name, use '' if on local machine
PORT = 8888      # default pigpio port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((HOST, PORT))

res = pigpioCmd(s, 17, 0, 0) # get pi hardware version

print "Pi has hardware revision", res

t1 = pigpioCmd(s, 16, 0, 0) # get current tick
t2 = pigpioCmd(s, 16, 0, 0) # get current tick

print "ticks and microseconds diff", t2, t1, t2-t1

# start servo pulses on gpio4 (pin P1-7) of 1500 micros
r = pigpioCmd(s, 8, 4, 1500)
print "servo status", r

# start PWM on gpio17 (pin P1-11) with 50% dutycycle
r = pigpioCmd(s, 5, 17, 128)
print "PWM status", r

# read level of gpio0
r = pigpioCmd(s, 3, 0, 0)
print "gpio0 level", r

s.close()
Here I'm running the Python on a laptop which is wirelessly connected to the Pi.

Code: Select all

$ ./pigpio.py  
Pi has hardware revision 2
ticks and microseconds diff 3746311906 3746309187 2719
servo status 0
PWM status 0
gpio0 level 1

Return to “Python”