3172| 1
|
[教程] 【智控万物】CodeLab Adapter控制树莓派GPIO |
【前言】操作树莓派 GPIO 的工具里,gpiozero尤为出色,因其具有很好的可理解性。 【准备】 参考:https://adapter.codelab.club/extension_guide/rpi_gpio/ 【查看引脚】 gpio用的mode是wPi,而gpiozero用的是BCM,所以pin17对应wpi的pin0 【查看代码】 node_raspberrypi.py [mw_shl_code=python,false]''' pip install gpiozero pigpio --user # docs: https://gpiozero.readthedocs.io/en/stable/remote_gpio.html ''' from io import StringIO import contextlib import sys import time import os # env from loguru import logger from codelab_adapter_client import AdapterNode from codelab_adapter_client.utils import get_or_create_node_logger_dir #ERO_PIN_FACTORY=pigpio PIGPIO_ADDR= "raspberrypi.local"# 192.168.1.3 #os.environ["GPIOZERO_PIN_FACTORY"] = "pigpio" #os.environ["PIGPIO_ADDR"] = "raspberrypi.local" from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep node_logger_dir = get_or_create_node_logger_dir() debug_log = str(node_logger_dir / "debug.log") logger.add(debug_log, rotation="1 MB", level="DEBUG") class RPINode(AdapterNode): def __init__(self): super().__init__(logger=logger) self.NODE_ID = self.generate_node_id(__file__) #self.factory = PiGPIOFactory(host='127.0.0.1') # 192.168.1.3 self.factory = PiGPIOFactory(host='raspberrypi.local') # 192.168.1.3 # 反馈 连接成功, 与失败 self.led = LED(17, pin_factory=self.factory) def run_python_code(self, code): try: output = eval(code, {"__builtins__": None}, { "led": self.led, "factory": self.factory }) except Exception as e: output = e return output def extension_message_handle(self, topic, payload): self.logger.info(f'code: {payload["content"]}') message_id = payload.get("message_id") python_code = payload["content"] output = self.run_python_code(python_code) payload["content"] = str(output) message = {"payload": payload} self.publish(message) def run(self): "避免插件结束退出" while self._running: time.sleep(0.5) if __name__ == "__main__": try: node = RPINode() node.receive_loop_as_thread() node.run() except KeyboardInterrupt: # node.logger.debug("KeyboardInterrupt") # work mac if node._running: node.terminate() # Clean up before exiting. [/mw_shl_code]Adapter Node,可参考:https://adapter.codelab.club/dev_guide/Adapter-Node/ 【CodeLab Scratch代码】 【验证】 也是用gpio验证 个人学习备注(一):Node RED 树莓派物联网 1、Node-RED安装与使用 https://blog.csdn.net/fnFenNuDManMan/article/details/104834919?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1 1.1Node-Red在windows上的安装和运行 https://www.jianshu.com/p/185242483016 如出现如下错误: 这样的问题 经过搜索得出 1.管理员身份打开powerShell 2.输入set-ExecutionPolicy RemoteSigned 浏览器地址栏中输入:http://127.0.0.1:1880/ 2、node-red教程2 第一条数据流 https://blog.csdn.net/geek_monkey/article/details/80736954?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1 3、node-red教程3.1 inject介绍 https://blog.csdn.net/geek_monkey/article/details/80737818 4、node-red教程 3.4 switch控件介绍 https://blog.csdn.net/geek_monkey/article/details/80738104 个人学习备注(二):python3控制树莓派的GPIO端口 https://blog.csdn.net/qq_36958104/article/details/83447224?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed