▌01树莓派远程GPIO
树莓派支持在通过网络对其GPIO进行控制。如果树莓派已经安装了pigpio之后,便可以通过以下步骤来实现远程GPIO控制。
参考的文档:
Configuration Remote GPIO
1.配置树莓派
通过树莓派的首选项中的“Raspberry Pi Configuration”,或者通过窗口命令:sudo raspi-config打开配置对话框。通过其中的interface中的 RemoteGPIO来使能远程GPIO功能。
▲ 配置树莓派的Interface参数
2.启动GPIO守候程序
(1)设置自动启动功能
使用systemctl命令设置启动自动运行GPIO远程有台守候程序:
sudo systemctl enable pigpiod
(2)启动GPIOD
sudo systemctl start pigpiod
sudo gpiod
详细参见树莓派的Remote GPIO中的内容。
3.Windows下载GPIO软件包
安装命令:
pip install gpiozero pigpio
4.例程
#!/usr/local/bin/python# -*- coding: gbk -*-#============================================================# TESTIO.PY-- by Dr. ZhuoQing -03-07## Note:#============================================================from head import *import gpiozerofrom gpiozeroimport LEDfrom gpiozero.pins.pigpio import PiGPIOFactoryfactory = PiGPIOFactory(host='192.168.0.134')led = LED(17, pin_factory=factory)while True:led.on()time.sleep(1)led.off()time.sleep(1)printf('\a')#------------------------------------------------------------# END OF FILE : TESTIO.PY#============================================================
from gpiozero import LEDfrom gpiozero.pins.pigpio import PiGPIOFactoryfrom time import sleepfactory3 = PiGPIOFactory(host='192.168.1.3')factory4 = PiGPIOFactory(host='192.168.1.4')led_1 = LED(17, pin_factory=factory3)led_2 = LED(17, pin_factory=factory4)while True:led_1.on()led_2.off()sleep(1)led_1.off()led_2.on()sleep(1)
▲ 远程GPIO运行的效果
▌结论
树莓派支持远程GPIO的动作。本文给出了远程GPIO的配置过程。更多的远程GPIO示例可以参见:Remote GPIO Recipes.
在python中增加远程控制命令,可以通过“rprg”来天界相应的指令。
#!/usr/local/bin/python# -*- coding: gbk -*-#============================================================# RPRG.PY -- by Dr. ZhuoQing -03-09## Note: Raspberry Pi remote GPIO# #============================================================from head import *tspinsii("from gpiozeroimport LED,Button")tspinsii("from gpiozero.pins.pigpio import PiGPIOFactory\r\n")tspinsii("factory = PiGPIOFactory(host='192.168.0.134')\r\n")tspinsii("#beep = LED(10, pin_factory=factory)\r\n")#------------------------------------------------------------# END OF FILE : RPRG.PY#============================================================
■ 相关文献链接:
Configuration Remote GPIORemote GPIO Recipes