> 文档中心 > W5100S-EVB-Pico [Micropython] – MQTT

W5100S-EVB-Pico [Micropython] – MQTT

注:最初发表-By Louis_m 

组成:

硬件设施:

W5100S-EVB-Pico              X 1

Raspberry Pi 4              X 1

软件应用程序:

Micopython 

详细信息:

什么是MQTT?

  发布(Pub)/订阅(Sub)模型和发布者/订阅者模型是订阅客户端和发布客户端之间的通信模型。

 为在M2M和物联网中使用而创建的轻量级消息协议。

  •  在TCP操作。
  •   主题问题。
  • 订阅主题。
  • 作为中继,适用于1:N的通信。
  •  适用于“设备对设备”的消息处理,如低功耗传感器、移动设备、嵌入式计算机、Arduinos和微控制器。

概述:

WIZnet以太网帽(硬件连接在顶部)是一个利用W5100S并支持3.3V和5V树莓派Pico引脚兼容板。

请参考这个链接获取更多关于W5100S的信息。

  • 树莓派Pico pin兼容
  • 以太网(W5100S 硬连线TCP/IP芯片)
  • 产品页面: Overview | WIZnet Document System
  • 同时支持4个独立的硬件插槽
  • 支持无socket的新命令ARP-Request, PING-Request
  • 仅在自协商模式下支持Auto-MDIX

RP2040数据表:

– Raspberry Pi Documentation - Raspberry Pi Pico

RP2040

准备:

连接 Raspberry Pi

接通电源并连接以太网线缆

运行RPi-4 mosquitto到服务器

在你的树莓派准备好树莓派OS后,你可以继续本教程,让我们安装Mosquitto Broker.

    1.树莓派通过SSH打开终端窗口。

打开Rpi终端

 2.执行以下命令升级和更新系统

pi@raspberry:~ $ sudo apt update && sudo apt upgrade

3.按“Y”Enter。更新和升级需要一些时间(在我的例子中,大约需要10分钟)

4.要安装mosquitto Broker,请输入以下命令:

pi@raspberry:~ $ sudo apt install -y mosquitto mosquitto-clients

5.要使mosquitto在树莓派启动时自动启动,你需要运行以下命令(这意味着当树莓派启动时,mosquitto broker将启动):

pi@raspberry:~ $ sudo systemctl enable mosquitto.service

6.现在,运行以下命令来测试安装:

pi@raspberry:~ $ mosquitto -v

7.检查mosquitto 服务器是否使用下面的命令运行。您还可以使用命令停止和启动服务器。

状态

pi@raspberry:~ $service mosquitto status

停止

pi@raspberry:~ $service mosquitto stop

开始

pi@raspberry:~ $service mosquitto start

如果检查出现如下状态,说明服务器正常运行。

Mosquitto 状态

连接 W5100S-Pico-EVB:

 W5100S-PIco-EVB

连接树莓派Pico到台式机或笔记本使用5针微型USB电缆。

开发环境设置:

Micropython Thonny IDE

在树莓派Pico上安装Thonny IDE,请参考上面链接。

Thonny, Python IDE for beginners

  1. 在所有的RP2040板上,该驱动器将被称为RPI-RP2。从下面的链接下载UF2(firmware.uf2)文件,并将该文件放到Pico中。

文件链接-firmware.uf2

(Release v1.0.0 firmware.uf2 · Wiznet/RP2040-HAT-MicroPython · GitHub)

2.如果你点击状态栏中的MicroPython(Raspberry Pi Pico)并选择“Configure interpreter...”,你也可以访问固件安装菜单。

3.查看壳牌面板底部的Thonny编辑器。你应该会得到以下结果:

进口MQTT库:

首先,将要使用的函数库从库中导入到电脑中,请遵循以下步骤使用Thonny IDE将文件上传。

Micropython以太网库:

  • 创建一个新文件,使用你想要的名字并保存在电脑中,例如“(你的库名).py”。
  • 进入“Open>这台计算机”

该文件应以“(你的库名).py”的名称保存在RPi Pico上。

  • 进入文件>保存为>树莓派Pico>

发布

#### MQTT Pico (1) ##### – publish

  1. 设置SPI接口和初始化以太网配置

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.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))

while not nic.isconnected():

    time.sleep(1)

print(nic.regs())  

2.在MQTT配置中,代理IP地址是Raspberry Pi服务器的IP。

#mqtt config

mqtt_server = '192.168.1.7'client_id = 'wiz1'topic_pub = b'hello'topic_msg = b'Hello Pico'

last_message = 0

message_interval = 5

counter = 0

#MQTT connect

def mqtt_connect():

    client = MQTTClient(client_id, mqtt_server, keepalive=60)

    client.connect()

    print('Connected to %s MQTT Broker'%(mqtt_server))

    return client

#reconnect & reset

def reconnect():

    print('Failed to connected to MQTT Broker. Reconnecting...')

    time.sleep(5)

    machine.reset()

3.将使用MQTT发布

#MQTT Publish

def main():

    w5x00_init()

    try:

        client = mqtt_connect()

    except OSError as e:

        reconnect()

    

    while True:

        client.publish(topic_pub, topic_msg)

        time.sleep(3)

        

client.disconnect()

以“Hello”为主题向服务器发送“Hello WIZnet”消息

MQTT发布

订阅:

#### MQTT Pico (2) ##### – 订阅

  1. 设置SPI接口和初始化以太网配置

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.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))

while not nic.isconnected():

    time.sleep(1)

    print(nic.regs())

2.在MQTT配置中,代理IP地址是Raspberry Pi服务器的IP。

#mqtt config

mqtt_server = '192.168.1.7'

client_id = "wiz"

topic_sub = b'hello'

last_message = 0

message_interval = 5

counter = 0

def sub_cb(topic, msg):

    print((topic.decode('utf-8'), msg.decode('utf-8')))

#MQTT connect

def mqtt_connect():

    client = MQTTClient(client_id, mqtt_server, keepalive=60)

    client.set_callback(sub_cb)

    client.connect()

    print('Connected to %s MQTT Broker'%(mqtt_server))

    return client

#reconnect & reset

def reconnect():

    print('Failed to connected to Broker. Reconnecting...')

    time.sleep(5)

    machine.reset()

3.使用MQTT订阅

#subscribe

def main():

    w5x00_init()

    try: 

        client = mqtt_connect()

    except OSError as e:

        reconnect()

    while True:

        client.subscribe(topic_sub)

        time.sleep(1)

    client.disconnect()

如果您订阅了“Hello”主题,则可以从发布服务器获得一条消息。

MQTT订阅

文档:

代码:

MQTT_pub

MQTT_sub