Skip to content

Instantly share code, notes, and snippets.

@bennuttall
Last active July 8, 2021 18:22
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennuttall/572789b0aa5fc2e7c05c7ada1bdc813e to your computer and use it in GitHub Desktop.
Save bennuttall/572789b0aa5fc2e7c05c7ada1bdc813e to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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
Copy link
Author

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
Copy link

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
Copy link

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
Copy link

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
Copy link

@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

@rmckinney654
Copy link

Has anyone got an example of reading an input remotely over PIGPIO?

@bennuttall
Copy link
Author

@bennuttall
Copy link
Author

Note the examples in this 4 year old gist are outdated. See the gpiozero docs for new usage.

@PiFlUn
Copy link

PiFlUn commented Mar 9, 2021

Does this also work if the Pi is not in the same network? E.g. I'm in my home network and wish to control the Pi which is in my university's network?

@bennuttall
Copy link
Author

It should work over the internet provided sufficient port forwarding is set up, although I'd expect it to be very slow. However, I'm guessing you don't have network admin rights to your university network.

@PiFlUn
Copy link

PiFlUn commented Mar 9, 2021

Could I maybe find a workaround via ssh? I can ssh my university-account.

@bennuttall
Copy link
Author

Yes you could SSH in and control it with python directly - that would be a better approach.

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