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

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