Create a gist now

Instantly share code, notes, and snippets.

Embed
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?

alexanderdixon commented 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?

@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.

@k4mrul

This comment has been minimized.

Show comment
Hide comment
@k4mrul

k4mrul May 25, 2018

For windows, to set environment variable, execute:

set PIGPIO_ADDR=ip_address_of_your_pi
and
set GPIOZERO_PIN_FACTORY=pigpio

Full code:

import os
from time import sleep
os.environ["PIGPIO_ADDR"] = "ip_address_of_your_pi"
os.environ["GPIOZERO_PIN_FACTORY"] = "pigpio"
from gpiozero import LED

gpio_pin = 26   #we are using gpio 26
led = LED(gpio_pin)  
while True: 
    led.on() 
    sleep(1)
    led.off() 
    sleep(1)

Anyway, thank @bennuttall :)

k4mrul commented May 25, 2018

For windows, to set environment variable, execute:

set PIGPIO_ADDR=ip_address_of_your_pi
and
set GPIOZERO_PIN_FACTORY=pigpio

Full code:

import os
from time import sleep
os.environ["PIGPIO_ADDR"] = "ip_address_of_your_pi"
os.environ["GPIOZERO_PIN_FACTORY"] = "pigpio"
from gpiozero import LED

gpio_pin = 26   #we are using gpio 26
led = LED(gpio_pin)  
while True: 
    led.on() 
    sleep(1)
    led.off() 
    sleep(1)

Anyway, thank @bennuttall :)

@lemonkey

This comment has been minimized.

Show comment
Hide comment
@lemonkey

lemonkey Jul 20, 2018

Note: setting the environment variable GPIOZERO_PIN_FACTORY was required on macOS as well, even though the documentation states in
https://gpiozero.readthedocs.io/en/stable/remote_gpio.html#environment-variables that after setting the environment variable PIGPIO_ADDR:

If you are running this from a PC (not a Raspberry Pi) with gpiozero and the pigpio Python library installed, this will work with no further configuration. However, if you are running this from a Raspberry Pi, you will also need to ensure the default pin factory is set to PiGPIOFactory.

Otherwise you'll get something similar when trying to import the gpiozero library from a python script run on macOS, etc.

gpiozero/devices.py:452: PinFactoryFallback: Falling back from rpigpio: No module named 'RPi'
  'Falling back from %s: %s' % (name, str(e))))
gpiozero/devices.py:452: PinFactoryFallback: Falling back from rpio: No module named 'RPIO'
  'Falling back from %s: %s' % (name, str(e))))

lemonkey commented Jul 20, 2018

Note: setting the environment variable GPIOZERO_PIN_FACTORY was required on macOS as well, even though the documentation states in
https://gpiozero.readthedocs.io/en/stable/remote_gpio.html#environment-variables that after setting the environment variable PIGPIO_ADDR:

If you are running this from a PC (not a Raspberry Pi) with gpiozero and the pigpio Python library installed, this will work with no further configuration. However, if you are running this from a Raspberry Pi, you will also need to ensure the default pin factory is set to PiGPIOFactory.

Otherwise you'll get something similar when trying to import the gpiozero library from a python script run on macOS, etc.

gpiozero/devices.py:452: PinFactoryFallback: Falling back from rpigpio: No module named 'RPi'
  'Falling back from %s: %s' % (name, str(e))))
gpiozero/devices.py:452: PinFactoryFallback: Falling back from rpio: No module named 'RPIO'
  'Falling back from %s: %s' % (name, str(e))))
@lemonkey

This comment has been minimized.

Show comment
Hide comment
@lemonkey

lemonkey Jul 20, 2018

@bennuttall

In
https://gist.github.com/bennuttall/572789b0aa5fc2e7c05c7ada1bdc813e#gistcomment-1953893 I believe there is a typo:

Instead of from gpiozero.pins.pigpiod import PiGPIOPin it should be from gpiozero.pins.pigpio import PiGPIOPin.

Per the docs under
http://gpiozero.readthedocs.io/en/stable/api_pins.html#changing-the-pin-factory:

pigpio | gpiozero.pins.pigpio.PiGPIOFactory | gpiozero.pins.pigpio.PiGPIOPin

lemonkey commented Jul 20, 2018

@bennuttall

In
https://gist.github.com/bennuttall/572789b0aa5fc2e7c05c7ada1bdc813e#gistcomment-1953893 I believe there is a typo:

Instead of from gpiozero.pins.pigpiod import PiGPIOPin it should be from gpiozero.pins.pigpio import PiGPIOPin.

Per the docs under
http://gpiozero.readthedocs.io/en/stable/api_pins.html#changing-the-pin-factory:

pigpio | gpiozero.pins.pigpio.PiGPIOFactory | gpiozero.pins.pigpio.PiGPIOPin

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