Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Set up a Pi and host PC for remote GPIO access using gpiozero

Remote GPIO

GPIO Zero allows you to create objects representing GPIO devices. As well as running it on a Raspberry Pi, you can also install GPIO Zero on a PC and create objects referencing GPIO pins on a Pi over the network.

To do this, you'll need to do a few things to get set up:

  1. Enable Remote GPIO on the Pi in the Raspberry Pi Configuration Tool.

  2. Run the pigpio daemon on the Pi:

    • sudo pigpiod
  3. Get the Pi's IP address:

    • hostname -I
  4. Install gpiozero and pigpio on your host machine (not necessary on Raspbian or x86 PIXEL):

    • Install pip: sudo apt install python3-pip
    • Install gpiozero and pigpio: sudo pip3 install gpiozero pigpio
  5. Run your Python environment with the PIGPIO_ADDR environment variable set, e.g one of the following:

    • PIGPIO_ADDR=192.168.1.4 ipython3
    • PIGPIO_ADDR=192.168.1.4 python3
    • PIGPIO_ADDR=192.168.1.4 idle3 &

    If running on a Raspberry Pi, you also need to set the pin factory to PiGPIOPin:

    • GPIOZERO_PIN_FACTORY=PiGPIOPin PIGPIO_ADDR=192.168.1.4 ipython3
    • GPIOZERO_PIN_FACTORY=PiGPIOPin PIGPIO_ADDR=192.168.1.4 python3
    • GPIOZERO_PIN_FACTORY=PiGPIOPin PIGPIO_ADDR=192.168.1.4 idle3 &
  6. Now use GPIO Zero like normal, and the devices will be controlled by GPIO pins on the remote Pi:

    >>> from gpiozero import LED
    >>> led = LED(2)
    >>> led.blink()  # LED on remote Pi's pin 2 now blinking

    Alternatively, use pin objects as described in the pins documentation.

@bennuttall

This comment has been minimized.

Show comment Hide comment
@bennuttall

bennuttall Dec 20, 2016

Note that this also works on PC and Mac - I'm just not up on how you install pip, git clone and such on those platforms. If anyone can help out and reproduce these instructions for other platforms I'd appreciate it!

Owner

bennuttall commented Dec 20, 2016

Note that this also works on PC and Mac - I'm just not up on how you install pip, git clone and such on those platforms. If anyone can help out and reproduce these instructions for other platforms I'd appreciate it!

@alexanderdixon

This comment has been minimized.

Show comment Hide comment
@alexanderdixon

alexanderdixon Dec 21, 2016

Nice. Would this work accessing more than 1 rpi remotely, i.e. A handful of rpis gpio being used from a central point for event detection or something?

Nice. Would this work accessing more than 1 rpi remotely, i.e. A handful of rpis gpio being used from a central point for event detection or something?

@bennuttall

This comment has been minimized.

Show comment Hide comment
@bennuttall

bennuttall Dec 21, 2016

Yes - that works too. You can either run different scripts for each Pi, and use the environment variable as above, or to use them in the same script, use one as your "default" IP, and either create pin instances on the IP:

from gpiozero import LED
from gpiozero.pins.pigpiod import PiGPIOPin

pin_a = PiGPIOPin(2, host='192.168.1.2')  # reference to pin 2 on a different Pi
pin_b = PiGPIOPin(2, host='192.168.1.3')  # reference to pin 2 on another Pi

red = LED(2)  # led on this Pi
green = LED(pin_a)  # led on different Pi
blue = LED(pin_b)  # led on another Pi

or I think you should be able to set the IP when changing the default pin factory in between creating devices, but I'm not certain on how this can be done (see #279).

See http://bennuttall.com/whats-new-gpio-zero-v1-3/ and http://gpiozero.readthedocs.io/en/latest/api_pins.html

An example:

from gpiozero import LED, Button
from gpiozero.pins.pigpiod import PiGPIOPin
from signal import pause

# set up references to pins on two different Pis
pin_a = PiGPIOPin(2, "192.168.1.2")
pin_b = PiGPIOPin(2, "192.168.1.3")

# create buttons connected to remote Pis
button_a = Button(pin_a)
button_b = Button(pin_b)

# create LEDs on this Pi
led_a = LED(2)
led_b = LED(3)

# the LEDs on this Pi should be on when the (remote) buttons are pressed
led_a.source = button_a.values
led_b.source = button_b.values

pause()

Not the most thrilling example, but you get the idea!

Note: of course you can nest the pin reference so it's in one line without a separate pin object:

led = LED(PiGPIOPin(2, host="192.168.1.2"))
Owner

bennuttall commented Dec 21, 2016

Yes - that works too. You can either run different scripts for each Pi, and use the environment variable as above, or to use them in the same script, use one as your "default" IP, and either create pin instances on the IP:

from gpiozero import LED
from gpiozero.pins.pigpiod import PiGPIOPin

pin_a = PiGPIOPin(2, host='192.168.1.2')  # reference to pin 2 on a different Pi
pin_b = PiGPIOPin(2, host='192.168.1.3')  # reference to pin 2 on another Pi

red = LED(2)  # led on this Pi
green = LED(pin_a)  # led on different Pi
blue = LED(pin_b)  # led on another Pi

or I think you should be able to set the IP when changing the default pin factory in between creating devices, but I'm not certain on how this can be done (see #279).

See http://bennuttall.com/whats-new-gpio-zero-v1-3/ and http://gpiozero.readthedocs.io/en/latest/api_pins.html

An example:

from gpiozero import LED, Button
from gpiozero.pins.pigpiod import PiGPIOPin
from signal import pause

# set up references to pins on two different Pis
pin_a = PiGPIOPin(2, "192.168.1.2")
pin_b = PiGPIOPin(2, "192.168.1.3")

# create buttons connected to remote Pis
button_a = Button(pin_a)
button_b = Button(pin_b)

# create LEDs on this Pi
led_a = LED(2)
led_b = LED(3)

# the LEDs on this Pi should be on when the (remote) buttons are pressed
led_a.source = button_a.values
led_b.source = button_b.values

pause()

Not the most thrilling example, but you get the idea!

Note: of course you can nest the pin reference so it's in one line without a separate pin object:

led = LED(PiGPIOPin(2, host="192.168.1.2"))
@annagat

This comment has been minimized.

Show comment Hide comment
@annagat

annagat Mar 21, 2018

Hi,

I am trying to run this pigpio in Windows 7. I am confused on #5. Where do I supposed to run them or how do I use those commands. I tried the command in cmd but it says

'PIGPIO_ADDR' is not recognized as an internal or external command,operable program or batch file.

Here is the code I am trying to run.

`from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import LED
from time import sleep

device_ip = '192.168.1.14'
factory = PiGPIOFactory(device_ip)
remote_gpio = LED(17, pin_factory=factory) # remote button

while True:
remote_gpio.on()
sleep(1)
remote_gpio.off()
sleep(1)`

Any help will be much appreciated.

annagat commented Mar 21, 2018

Hi,

I am trying to run this pigpio in Windows 7. I am confused on #5. Where do I supposed to run them or how do I use those commands. I tried the command in cmd but it says

'PIGPIO_ADDR' is not recognized as an internal or external command,operable program or batch file.

Here is the code I am trying to run.

`from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import LED
from time import sleep

device_ip = '192.168.1.14'
factory = PiGPIOFactory(device_ip)
remote_gpio = LED(17, pin_factory=factory) # remote button

while True:
remote_gpio.on()
sleep(1)
remote_gpio.off()
sleep(1)`

Any help will be much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment