跳到主要内容

远程GPIO用法

以下示例演示了 GPIO Zero 库远程 GPIO 功能的部分功能。在开始学习这些示例之前,请先阅读有关准备 Pi 和主机以使用 配置远程GPIO 的知识。

请注意,所有示例都是假设 Python 3 编写的。这些示例在 Python 2 下可能也能运行,但不能保证!

LED+按钮

让一个 Raspberry Pi 上的 Button 控制另一个 Raspberry Pi 上的 LED

from gpiozero import Button, LED
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause

factory = PiGPIOFactory(host='192.168.1.3')

button = Button(2)
led = LED(17, pin_factory=factory)

led.source = button

pause()

LED+2个按钮

按下两个按钮时,LED 会亮起:

from gpiozero import Button, LED
from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero.tools import all_values
from signal import pause

factory3 = PiGPIOFactory(host='192.168.1.3')
factory4 = PiGPIOFactory(host='192.168.1.4')

led = LED(17)
button_1 = Button(17, pin_factory=factory3)
button_2 = Button(17, pin_factory=factory4)

led.source = all_values(button_1, button_2)

pause()

多房间运动警报

在家里的每个房间都安装一个带有运动传感器(MotionSensor)的 Raspberry Pi,当每个房间有动静时,LED 指示灯就会显示出来:

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()

多房间门铃

在希望听到门铃声的每个房间安装一个 Raspberry Pi,并连接一个蜂鸣器(Buzzer),然后使用一个按钮(Button)作为门铃:

from gpiozero import Button, Buzzer
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]

pin = 17
button = Button(pin) # 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()

它还可以用作内部门铃(从厨房告诉人们晚餐时间到了)。

远程按钮机器人

与按钮控制机器人(Robot)的简单方案类似,本示例使用四个按钮来控制机器人的方向。不过,为机器人使用遥控能力意味着控制按钮可以与机器人分开:

from gpiozero import Button, Robot, Motor
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause

factory = PiGPIOFactory(host='192.168.1.17')
robot = Robot(left=Motor(4, 14), right=Motor(17, 18),
pin_factory=factory) # remote pins

# local buttons
left = Button(26)
right = Button(16)
fw = Button(21)
bw = Button(20)

fw.when_pressed = robot.forward
fw.when_released = robot.stop

left.when_pressed = robot.left
left.when_released = robot.stop

right.when_pressed = robot.right
right.when_released = robot.stop

bw.when_pressed = robot.backward
bw.when_released = robot.stop

pause()

光传感器+Sense HAT

Sense HAT(GPIO Zero 不支持)包括温度、湿度和压力传感器,但没有光传感器。远程 GPIO 允许使用外部光传感器(LightSensor)。Sense HAT LED 显示器可根据光照度显示不同的颜色:

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)

请注意,在这种情况下,必须在本地运行 Sense HAT 代码,远程运行 GPIO。


中文翻译版以英文版相同知识授权方式共享:BSD-3-Clause。交流 Q群:498908352