例程讲解14-mqtt_pub mqtt发布

# MQTT示例
# This example shows how to use the MQTT library to publish to a topic.
# 这个示例展示了如何使用MQTT库发布到主题。
#
# 1) 将mqtt.py库复制到OpenMV存储。
# 2) 在OpenMV摄像机上运行这个脚本。
# 3) 将mosquito客户端安装在PC上,并运行以下命令:
#    mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v
#
# 注意:如果无法访问mosquito,请尝试另一个代理(例如:broker.hivemq.com)。
import time, network
from mqtt import MQTTClient

SSID='' # Network SSID
KEY=''  # Network key

# 初始化wlan模块并连接到网络
print("Trying to connect... (may take a while)...")

wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)

# 我们现在应该有一个通过DHCP的有效IP了
print(wlan.ifconfig())

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

while (True):
    client.publish("openmv/test", "Hello World!")
    time.sleep(1000)

results matching ""

    No results matching ""