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:
-
Enable Remote GPIO on the Pi in the Raspberry Pi Configuration Tool.
-
Run the pigpio daemon on the Pi:
sudo pigpiod
-
Get the Pi's IP address:
hostname -I
-
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
- Install
-
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 &
-
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.
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.