如果你把python和mcus结合起来会得到什么?
reticul8的Python项目详细描述
网线8
Python的远程连接MCU端点
reticul8允许您使用python远程控制兼容的微控制器 例如Arduino或ESP32
在python方面,它使用python 3.5+和asyncio
在微控制器方面,它使用PJON 和PJON-cython通信 微控制器-任何可以运行pjon的uc都应该兼容。
它还使用protocol buffers来封装rpc消息。
例如,可以使用以下设置无线控制 ESP32使用ESPNOW
"HUB" (Running Python) <--Serial/UART--> NODE 1 (ESP32)
^--ESPNOW --> NODE 2 (ESP32)
^--ESPNOW --> NODE n (ESP32)
基本原理
标线8的设计满足以下要求:-
- 系统应该能够运行“复杂的应用程序逻辑”并“连接到Internet”
- 系统中的节点应该能够使用各种媒体(有线和无线)连接到集线器。
- 节点应该能够在通用MCU硬件上运行(最初以Arduino和ESP32为目标)
- 节点应该快速可靠,但不需要“智能”-应用程序逻辑可以存在于其他地方
- 节点和控制器之间的通信应快速可靠(即不通过互联网!)
请注意,一个关键要求是缺少Internet连接。你家怎么了 网络瘫痪时的自动化系统?
网线8是为一个家庭自动化系统而设计的,其中的节点不必直接连接 上网。这也有利于在控制器/集线器和节点之间进行通信 比pub/sub快(对于使用espnow和throughserial的两节点设置,<;70ms rtt)。
基于PJON作为节点之间的通信媒介,可以提供很多选择
网线8被设计成家庭自动化系统的一部分-特别是它允许节点(如ESP32或Arduino)来 作为由智能控制器控制的哑远程端点操作(例如运行在raspberrypi上的python)。
竞争项目包括:
- Mongoose OS-物联网的开源操作系统
- micropython-用于微控制器的python
- Zerynth-在微控制器上使用Python的物联网中间件
但当我看到我需要的功能,这些似乎都不适合。MicroPython和Zerynth似乎 太“资源重”,无法运行简单的哑端点。Mongoose操作系统非常适合,但它仍然假设 节点在Internet上。
类似Arduino的API:
节点(端点)通过使用protocolbuf定义的远程过程调用(rpc)来控制。
提供类似Arduino的API:-
importasyncioimportuvloopfromreticul8importrpc,pjon_strategiesfromreticul8.arduinoimport*classNode(rpc.Node):asyncdefnotify_startup(self):print("Received startup message from {}".format(self.device_id))withself:# schedule the inbuilt LED to blink 10 timeswithSchedule(count=10,after_ms=100,every_ms=500):awaitdigitalWrite(22,LOW)withSchedule(count=10,after_ms=600,every_ms=500):awaitdigitalWrite(22,HIGH)awaitasyncio.sleep(10)#manually blink the LED awaitpinMode(22,OUTPUT)foriinrange(5):awaitdigitalWrite(22,HIGH)awaitsleep(.1)awaitdigitalWrite(22,LOW)awaitsleep(.1)#read the value of the pinawaitpinMode(19,INPUT_PULLUP)value=awaitdigitalRead(19)print("HIGH"ifvalue==HIGHelse"LOW")#ping the remote nodeforiinrange(10):awaitping()#an ESP32 feature - built in PWMawaitPWM_config(22)whileTrue:awaitPWM_fade(pin=22,duty=0,fade_ms=500)awaitsleep(1)awaitPWM_fade(pin=22,duty=8192,fade_ms=500)awaitsleep(1)classPJON(pjon_strategies.SerialAsyncio):defnotify_connection_made(self):print("ESP32 connected")defnotify_connection_lost(self):asyncio.get_event_loop().stop()asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())loop=asyncio.get_event_loop()transport=PJON(device_id=10,url="/dev/ttyUSB0",baudrate=115200)Node(remote_device_id=11,transport=PJON)loop.run_forever()loop.close()
支持的RPC
GPIO
- pinmode()
- DigitalRead()
- digitalwrite()
- 输入->;使用更改时回调功能查看更改,取消缓冲
I2C
- i2c读取
- I2c_写入
ESP32特定功能:
- 脉宽调制(LEDC)
- OTA更新
网线8助手
- 安排命令重复运行
- 运行多个命令
计划功能
- 模拟输出
- 模拟输入
- 触摸传感器(ESP32)
- 脉冲计数器(ESP32)
构建ESP-IDF组件节点
Create a new ESP-IDF project, 还有add the Arduino component
添加网线8作为组件:
cd components
git clone https://github.com/xlfe/reticul8
您的main.cpp
只需要设置pjon总线,并将它们传递给reticul8类。调用设置和循环根据
Arduino的功能。
// Define Wifi config for ESPNOW #include"esp_wifi_types.h"staticwifi_country_twifi_country={cc:"AU",schan:1,nchan:14,max_tx_power:80,// Level 10policy:WIFI_COUNTRY_POLICY_MANUAL};#include"Arduino.h"# PJON defines#define PJON_INCLUDE_ANY#define PJON_INCLUDE_TSA#define PJON_INCLUDE_EN#define TSA_RESPONSE_TIME_OUT 100000#include<reticul8.h>PJON<Any>*bus=NULL;RETICUL8*r8=NULL;voidloop(){r8->loop();}voidsetup(){Serial.begin(115200);Serial.flush();//EPSNOWStrategyLink<ESPNOW>*link_esp=newStrategyLink<ESPNOW>;PJON<Any>*bus_esp=newPJON<Any>();bus_esp->set_asynchronous_acknowledge(false);bus_esp->set_synchronous_acknowledge(true);bus_esp->set_packet_id(true);bus_esp->set_crc_32(true);bus_esp->strategy.set_link(link_esp);//Uncomment the line below to make a single bus device (eg leaf)// otherwise the device is initialised as a bridge between esp-now and serial// r8 = new RETICUL8(bus_esp, 10); /*//SerialStrategyLink<ThroughSerialAsync>*link_tsa=newStrategyLink<ThroughSerialAsync>;link_tsa->strategy.set_serial(&Serial);bus=newPJON<Any>(11);bus->strategy.set_link(link_tsa);bus->set_asynchronous_acknowledge(false);bus->set_synchronous_acknowledge(false);bus->set_packet_id(false);bus->set_crc_32(false);PJON<Any>*secondary[1]={bus_esp};r8=newRETICUL8(bus,10,secondary,1);//*/r8->begin();}
最后,确保您的component.mk
(与main.cpp位于同一目录中)包含以下内容:-
COMPONENT_DEPENDS+=reticul8#Used for build timestampCPPFLAGS+=-D"__COMPILE_TIME__=`date'+%s'`"