Routineerklärung 14-mqtt_sub mqtt-Abonnement

# MQTT示例
# This example shows how to use the MQTT library to subscribe to a topic.
#
# 1) 将 mqtt.py 库复制到OpenMV内存。
# 2) 在 OpenMV Cam 上运行这个脚本。
# 3) 在PC上安装mosquitto客户端并运行以下命令:
#    mosquitto_pub -t "openmv/test" -m "Hello World!" -h test.mosquitto.org -p 1883
#
# 注意: 如果 mosquitto 代理无法访问,请尝试其他代理(例如:broker.hivemq.com 或broker.emqx.io)

import time
import network
from mqtt import MQTTClient

SSID = ""  # Network SSID 填入要连接的wifi名称
KEY = ""  # Network key 填入wifi密码

# 初始化wlan模块并连接到网络
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY)

while not wlan.isconnected():
    print('Trying to connect to "{:s}"...'.format(SSID))
    time.sleep_ms(1000)

# 我们现在应该通过DHCP获得有效的IP
print("WiFi Connected ", wlan.ifconfig())

client = MQTTClient("openmv", "test.mosquitto.org", port=1883)
client.connect()


def callback(topic, msg):
    print(topic, msg)


# 必须先设置回调
client.set_callback(callback)
client.subscribe("openmv/test")

while True:
    client.check_msg()  # 轮询消息
    time.sleep_ms(1000)

results matching ""

    No results matching ""