赞
踩
在这个智能硬件和物联网时代,MicroPython和树莓派PICO正以其独特的优势引领着嵌入式开发的新潮流。MicroPython作为一种精简优化的Python 3语言,为微控制器和嵌入式设备提供了高效开发和简易调试的
当我们结合WIZnet W5100S/W5500网络模块,MicroPython和树莓派PICO的开发潜力被进一步放大。这两款模块都内置了TCP/IP协议栈,使得在嵌入式设备上实现网络连接变得更加容易。无论是进行数据传输、远程控制,还是构建物联网应用,它们都提供了强大的支持。
本章我们将以WIZnet W5100S为例,以MicroPython的开发方式,连接至OneNET旧版MQTT上,并定时上报DHT11传感器的温湿度信息。
第一步:创建产品
第二步:创建设备
第三步:创建数据流
第四步:计算参数
参数名 | 参数值 |
---|---|
mqttHostUrl | mqtt.heclouds.com(固定不变) |
port | 6002(固定不变) |
clientId | 1168615815(设备ID) |
username | 621478(产品ID) |
passwd | 0123456789(鉴权信息) |
发布主题 | $dp(固定不变) |
WIZnet 主流硬件协议栈以太网芯片参数对比
Model | Embedded Core | Host I/F | TX/RX Buffer | HW Socket | Network Performance |
---|---|---|---|---|---|
W5100S | TCP/IPv4, MAC & PHY | 8bit BUS, SPI | 16KB | 4 | Max 25Mbps |
W6100 | TCP/IPv4/IPv6, MAC & PHY | 8bit BUS, Fast SPI | 32KB | 8 | Max 25Mbps |
W5500 | TCP/IPv4, MAC & PHY | Fast SPI | 32KB | 8 | Max 15Mbps |
相较于软件协议栈,WIZnet的硬件协议栈以太网芯片有以下优点:
软件:
硬件:
我们直接打开mqtt_onenet_old.py文件。
第一步:可以看到在w5x00_init()函数中,进行了SPI的初始化。以及将spi相关引脚和复位引脚注册到库中,后续则是激活网络,并使用DHCP配置网络地址信息,当DHCP失败时,则配置静态网络地址信息。当未配置成功时,会打印出网络地址相关寄存器的信息,可以帮助我们更好的排查问题。
第二步:连接OneNET的MQTT服务器,连接失败则进入复位程序。
第三步:开启定时器定时上报温湿度信息。
需注意:要将MQTT参数定义改为您的OneNET的MQTT参数
#import library from umqttsimple import MQTTClient from usocket import socket from machine import Pin,SPI,Timer import dht import network import time #mqtt config mqtt_params = {} mqtt_params['url'] = "mqtt.heclouds.com" mqtt_params['port'] = 6002 mqtt_params['clientid'] = '1168615815' mqtt_params['username'] = '621478' mqtt_params['passwd'] = '0123456789' mqtt_params['pubtopic'] = '$dp' mqtt_params['pubqos'] = 0 message_interval = 5 timer_1s_count = 0 tim = Timer() #DHT11 definitions pin = Pin(2,Pin.OUT) sensor = dht.DHT11(pin) #mqtt client client = None """ W5x00 chip initialization. param: None returns: None """ def w5x00_init(): #spi init spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18)) nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin nic.active(True)#network active try: #DHCP print("\r\nConfiguring DHCP") nic.ifconfig('dhcp') except: #None DHCP print("\r\nDHCP fails, use static configuration") nic.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))#Set static network address information #Print network address information print("IP :",nic.ifconfig()[0]) print("Subnet Mask:",nic.ifconfig()[1]) print("Gateway :",nic.ifconfig()[2]) print("DNS :",nic.ifconfig()[3],"\r\n") #If there is no network connection, the register address information is printed while not nic.isconnected(): time.sleep(1) print(nic.regs()) """ 1-second timer callback function. param1: class timer returns: None """ def tick(timer): global timer_1s_count global client timer_1s_count += 1 if timer_1s_count >= message_interval: timer_1s_count = 0 sensor.measure() msg = '{"CurrentTemperature":%s,"CurrentHumidity":%s}'%(str(sensor.temperature()),str(sensor.humidity())) datalen = len(msg) temp = list(msg) temp.insert(0,chr(datalen%0x100)) temp.insert(0,chr(datalen//0x100)) temp.insert(0,"\x03") msg = "".join(temp) try: client.publish(mqtt_params['pubtopic'],msg,qos = mqtt_params['pubqos']) print('send:{"CurrentTemperature":%s,"CurrentHumidity":%s}'%(str(sensor.temperature()),str(sensor.humidity()))) except: print("publish error!please wait reset") machine.reset() """ Connect to the MQTT server. param: None returns: None """ def mqtt_connect(): client = MQTTClient(mqtt_params['clientid'], mqtt_params['url'], mqtt_params['port'],mqtt_params['username'],mqtt_params['passwd'],keepalive=60) client.connect() print('Connected to %s MQTT Broker'%(mqtt_params['url'])) return client """ Connection error handler. param: None returns: None """ def reconnect(): print('Failed to connected to Broker. Reconnecting...') time.sleep(5) machine.reset() def main(): global client print("WIZnet chip MQTT of OneNET(old version MQTT) example") w5x00_init() try: client = mqtt_connect() except OSError as e: reconnect() tim.init(freq=1, callback=tick) while True: time.sleep(1) if __name__ == "__main__": main()
要测试以太网示例,必须将开发环境配置为使用Raspberry Pi Pico。
注意:因为MicroPython的print函数是启用了stdout缓冲的,所以有时候并不会第一时间打印出内容。
运行该脚本必须要有umqttsimple.py库支持,如何添加umqttsimple.py库请查看MQTT协议示例
第一步:将程序复制到Thonny中,然后选择环境为Raspberry Pi Pico,再运行
第二步:可以看到此时每间隔5秒上报了一次温湿度信息,并且平台上也会实时更新。
想了解更多,评论留言哦!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。