RP2350-ETH是一款迷你RP2350开发板,让你拥有集成 TCP/IP 协议栈,做到网络通信,
在极小板型下引出14 个多功能 GPIO 引脚,PCB边缘采用半孔工艺,可以轻松快捷集成到项目中。
| ||
| ||
|
CH9120 | RP2350 | 功能 |
RXD | GP21 | 串行数据输入 |
TXD | GP20 | 串行数据输出 |
TCPCS | GP17 | TCP客户端模式下,连接状态指示,低电平表示连接成功 |
CFG0 | GP18 | 网络配置使能脚,低电平时,进入串口配置模式 |
RSTI | GP19 | 复位,低电平有效 |
cd ~ sudo wget https://www.waveshare.net/w/upload/1/15/RP2350_ETH_CODE.zip unzip RP2350_ETH_CODE.zip cd ~/RP2350_ETH_CODE
cd C/RP2350-ETH-Demo/build/ export PICO_SDK_PATH=../../pico-sdk #注意:务必写对你自己的SDK所在路径 cmake .. #执行make生成可执行文件,第一次编译时间比较久 make -j9 #编译完成,会生成uf2文件。
cp main.uf2 /media/pi/RPI-RP2/
如图所示:
如果你当前的Thonny版本没有pico支持包,输入以下指令来更新Thonny IDE
sudo apt upgrade thonny
3.点击File->Open...->~/RP2350_ETH_CODE/Python/RP2350-ETH-Demo.py,运行脚本即可
Item | Value |
PICO_SDK_PATH | [存放PICO-SDK路径]\pico-sdk |
NMake Makefiles #Ctrl+S保存设置,清空build文件夹,重启Visual Studio Code并重新编译
1.网络配置
UCHAR CH9120_Mode = TCP_CLIENT; //Optional:TCP_SERVER、TCP_CLIENT、UDP_SERVER、UDP_CLIENT UCHAR CH9120_LOCAL_IP[4] = {192, 168, 10, 205}; // LOCAL IP UCHAR CH9120_GATEWAY[4] = {192, 168, 11, 1}; // GATEWAY UCHAR CH9120_SUBNET_MASK[4] = {255, 255, 252, 0}; // SUBNET MASK UCHAR CH9120_TARGET_IP[4] = {192, 168, 10, 137}; // TARGET_IP UWORD CH9120_PORT1 = 1000; // LOCAL PORT1 UWORD CH9120_TARGET_PORT = 2000; // TARGET PORT UDOUBLE CH9120_BAUD_RATE = 115200; // BAUD RATE
#define TCP_SERVER 0 #define TCP_CLIENT 1 #define UDP_SERVER 2 #define UDP_CLIENT 3
2.硬件接口
void CH9120_Start();
void CH9120_End();
void CH9120_SetMode(UCHAR Mode);
void CH9120_SetLocalIP(UCHAR CH9120_LOCAL_IP[]);
void CH9120_SetSubnetMask(UCHAR CH9120_SUBNET_MASK[]);
void CH9120_SetGateway(UCHAR CH9120_GATEWAY[]);
void CH9120_SetTargetIP(UCHAR CH9120_TARGET_IP[]);
void CH9120_SetLocalPort(UWORD CH9120_PORT);
void CH9120_SetTargetPort(UWORD CH9120_TARGET_PORT);
void CH9120_SetBaudRate(UDOUBLE CH9120_BAUD_RATE);
3.代码解析
主要代码如下:
int Pico_ETH_CH9120_test(void) { CH9120_init(); //Initialize Configuration CH9120 RX_TX(); //receive and dispatch }
4.运行效果
1.网络配置
配置工作模式、IP、网关、子网掩码、端口号和串口波特率的代码都在 Python/RP2350-ETH-Demo/RP2350-ETH-Demo.py文件中,按照自己实际需求进行修改即可:
MODE = 1 #0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client GATEWAY = (192, 168, 1, 1) # GATEWAY TARGET_IP = (192, 168, 1, 10) # TARGET_IP LOCAL_IP = (192, 168, 1, 200) # LOCAL_IP SUBNET_MASK = (255,255,255,0) # SUBNET_MASK LOCAL_PORT1 = 1000 # LOCAL_PORT1 TARGET_PORT = 2000 # TARGET_PORT BAUD_RATE = 115200 # BAUD_RATE
2.硬件接口
CH9120 的配置函数都在 Python/RP2350-ETH-Demo/ch9120.py 文件中,程序通过导入 CH9120 类,再通过 ch9120 对象调用
ch9120 = CH9120(uart1)
ch9120.enter_config()
ch9120.exit_config()
ch9120.set_mode(MODE)
ch9120.set_localIP(LOCAL_IP)
ch9120.set_subnetMask(SUBNET_MASK)
ch9120.set_gateway(GATEWAY)
ch9120.set_localPort(LOCAL_PORT1)
ch9120.set_targetIP(TARGET_IP)
ch9120.set_targetPort(TARGET_PORT)
ch9120.set_baudRate(BAUD_RATE)
3.代码解析
主要代码如下:
if __name__ == "__main__": ch9120_configure() while True: time.sleep(0.1) while uart1.any() > 0: rxData1 = uart1.read(uart1.any()) uart1.write(rxData1) print(rxData1.decode('utf8'))
4.运行效果
1.网络配置
基于 TCP/IP 协议实现 MQTT 通讯,MQTT 与 CH9120 的配置代码都在 Python/RP2350-ETH-Demo/RP2350-ETH-MQTT.py 文件中:
# MQTT CLIENT_ID = "Waveshare_RP2350_ETH" SUBSCRIBE_TOPIC = "test_topic1" PUBLISH_TOPIC = "test_topic2" # CH9120 MODE = 1 #0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client GATEWAY = (192, 168, 1, 1) # GATEWAY TARGET_IP = (47, 92, 129, 18) # TARGET_IP LOCAL_IP = (192, 168, 1, 200) # LOCAL_IP SUBNET_MASK = (255,255,255,0) # SUBNET_MASK LOCAL_PORT1 = 1000 # LOCAL_PORT1 TARGET_PORT = 1883 # TARGET_PORT BAUD_RATE = 115200 # BAUD_RATE
MQTT相关变量说明,可根据自身需求进行修改:
CLIENT_ID:客户端ID SUBSCRIBE_TOPIC:订阅主题名称 PUBLISH_TOPIC:发布主题名称 TARGET_IP:MQTT服务端IP TARGET_PORT:MQTT服务端通信端口
2.接口函数
mqtt_client = MQTTClient(uart1)
mqtt_client.connect()
mqtt_client.subscribe(SUBSCRIBE_TOPIC)
mqtt_client.publish(PUBLISH_TOPIC, message)
mqtt_client.send_heartbeat()
mqtt_client.check_heartbeat_response()
mqtt_client.extract_data(rxData)
3.代码解析
主要代码如下:
if __name__ == "__main__": ch9120_configure() mqtt_client = MQTTClient(uart1) mqtt_client.ClientID = CLIENT_ID # Set ClientID mqtt_client.connect() # Connect to MQTT server mqtt_client.subscribe(SUBSCRIBE_TOPIC) # Subscribe to topic:test_topic1 while True: rxData = uart1.read() if rxData is not None: topic, message = mqtt_client.extract_data(rxData) # Parse the received data if topic == SUBSCRIBE_TOPIC: print("Topic:", topic) print("Message:", message) mqtt_client.publish(PUBLISH_TOPIC, message) # Send received data to topic:test_topic2 time.sleep_ms(20)
这段代码首先对CH9120进行配置,然后向MQTT服务端的通讯端口发送字节数据进行连接和订阅主题:
ch9120_configure() mqtt_client = MQTTClient(uart1) mqtt_client.ClientID = CLIENT_ID # Set ClientID mqtt_client.connect() # Connect to MQTT server mqtt_client.subscribe(SUBSCRIBE_TOPIC) # Subscribe to topic:test_topic1
这段代码表示进入数据收发模式,将接收到的消息从另一个主题发送回去:
while True: rxData = uart1.read() if rxData is not None: topic, message = mqtt_client.extract_data(rxData) # Parse the received data if topic == SUBSCRIBE_TOPIC: print("Topic:", topic) print("Message:", message) mqtt_client.publish(PUBLISH_TOPIC, message) # Send received data to topic:test_topic2 time.sleep_ms(20)
在连接建立后,客户端需要确保自己任意两次 MQTT 协议包的发送间隔不超过 Keep Alive 的值,如果客户端当前处于空闲状态,没有可发送的包,则可以发送 PINGREQ 协议包(心跳包)。如果MQTT服务端没有在 Keep Alive 的 1.5 倍时间内,收到来自客户端的任何包,则会认为和客户端之间的连接出现了问题,此时服务端便会断开和客户端的连接。所以客户端在长时间不发送数据包且要保持连接,需要每隔一段时间发送一次心跳包,通常建议在 Keep Alive 时间的一半内发送一次。下面这段代码表示每30秒向服务端发送一次心跳包,若检查心跳失败时,将进行重新连接,直到检查心跳成功时退出:
current_time = time.time() if current_time - last_heartbeat_time >= 30: last_heartbeat_time = current_time mqtt_client.send_heartbeat() if not mqtt_client.check_heartbeat_response(): while True: mqtt_client.send_heartbeat() if mqtt_client.check_heartbeat_response(): break
4.运行效果
| |
| |
| |
| |
| |
| |
| |
|
【MicroPython】machine.Pin类函数详解
【MicroPython】machine.PWM类函数详解
【MicroPython】machine.ADC类函数详解
【MicroPython】machine.UART类函数详解
【MicroPython】machine.I2C类函数详解
【MicroPython】machine.SPI类函数详解
【MicroPython】rp2.StateMachine类函数详解
周一-周五(9:30-6:30)周六(9:30-5:30)
手机:13434470212
邮箱:services04@spotpear.cn
QQ:202004841