5. Remote GPIO Recipes¶
The following recipes demonstrate some of the capabilities of the remote GPIO feature of the GPIO Zero library. Before you start following these examples, please read up on preparing your Pi and your host PC to work with Configuring Remote GPIO.
Please note that all recipes are written assuming Python 3. Recipes may work under Python 2, but no guarantees!
5.3. Multi-room motion alert¶
Install a Raspberry Pi with a
MotionSensor in each room of your house,
and have an class:LED indicator showing when there’s motion in each room:
from gpiozero import LEDBoard, MotionSensor from gpiozero.pins.pigpio import PiGPIOFactory from gpiozero.tools import zip_values from signal import pause ips = ['192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6'] remotes = [PiGPIOFactory(host=ip) for ip in ips] leds = LEDBoard(2, 3, 4, 5) # leds on this pi sensors = [MotionSensor(17, pin_factory=r) for r in remotes] # remote sensors leds.source = zip_values(*sensors) pause()
5.4. Multi-room doorbell¶
from gpiozero import LEDBoard, MotionSensor from gpiozero.pins.pigpio import PiGPIOFactory from signal import pause ips = ['192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6'] remotes = [PiGPIOFactory(host=ip) for ip in ips] button = Button(17) # button on this pi buzzers = [Buzzer(pin, pin_factory=r) for r in remotes] # buzzers on remote pins for buzzer in buzzers: buzzer.source = button pause()
This could also be used as an internal doorbell (tell people it’s time for dinner from the kitchen).
5.6. Light sensor + Sense HAT¶
The Sense HAT (not supported by GPIO Zero) includes temperature, humidity
and pressure sensors, but no light sensor. Remote GPIO allows an external
LightSensor to be used as well. The Sense HAT LED display can be used
to show different colours according to the light levels:
from gpiozero import LightSensor from gpiozero.pins.pigpio import PiGPIOFactory from sense_hat import SenseHat remote_factory = PiGPIOFactory(host='192.168.1.4') light = LightSensor(4, pin_factory=remote_factory) # remote motion sensor sense = SenseHat() # local sense hat blue = (0, 0, 255) yellow = (255, 255, 0) while True: if light.value > 0.5: sense.clear(yellow) else: sense.clear(blue)
Note that in this case, the Sense HAT code must be run locally, and the GPIO remotely.