import time import network from umqttsimple import MQTTClient
t = 0 keepalive = 60
defdo_connect(): wlan = network.WLAN(network.STA_IF) wlan.active(True) ifnot wlan.isconnected(): print('connecting to network...') wlan.connect('ID', 'password') i = 1 whilenot wlan.isconnected(): print("正在链接...{}".format(i)) i += 1 time.sleep(1) print('network config:', wlan.ifconfig())
defsub_cb(topic, msg): print(topic, msg)
do_connect() c = MQTTClient("umqtt_client", "address", keepalive=keepalive) c.set_callback(sub_cb) c.connect() c.subscribe(b"test") whileTrue: gloabl t t = t + 1 print(i) c.check_msg() time.sleep(1) if t == keepalive: c.ping() t = 0 print('发送心跳包')