ルーチン説明 14-mqtt_pub mqtt リリース
# MQTT示例
# This example shows how to use the MQTT library to publish to a topic.
#
# 1) 将 mqtt.py 库复制到OpenMV内存。
# 2) 在 OpenMV Cam 上运行这个脚本。
# 3) 在PC上安装mosquitto客户端并运行以下命令:
# mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v
#
# 注意: 如果 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()
while True:
client.publish("openmv/test", "Hello World!")
time.sleep_ms(1000)