Skip to content

Instantly share code, notes, and snippets.

@bennuttall
Last active July 8, 2021 18:22
Show Gist options
  • 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.

@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