例程讲解14-WiFi-Shield->mqtt_example
# MQTT 例程。
# 此示例显示如何使用MQTT库。
#
# 1) 将mqtt.py库复制到OpenMV U盘里。
# 2) 在PC上安装mosquitto客户端并运行以下命令:
# mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v
#
import time, network
from mqtt import MQTTClient
SSID='mux' # Network SSID
KEY='j806fVnT7tObdCYE' # 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_ms(1000)