问题描述:
代码是按照例程描述写的,连接到MQTT服务器后,每隔一段时间就会出现重连
from umqtt import MQTTClient
import log
from usr.config import Config
import modem
import utime
import _thread
class MyMqtt():
def __init__(self) -> None:
self.log = log.getLogger("MQTT")
self.config = Config()
self.client = None
self.clientId = modem.getDevSN()
self.subscribeTopic = “/log/*/aac”
self.pubscribeTopic = "log/back/aac"
def _init_mqtt(self):
self.client = MQTTClient(client_id= self.clientId, server="39.101.1.2", port=37237, user=None, password=None, keepalive=30)
self.client.set_callback(self._sub_callback) # 设置消息回调
self.client.connect() # 建立连接
self.client.subscribe(self.subscribeTopic) # 订阅主题
self.log.info("Subsrcibe topic is {}".format(self.subscribeTopic))
def _sub_callback(self,topic,message):
self.log.info("receive topic={},message={}".format(topic,message))
def start(self):
self._init_mqtt()
_thread.start_new_thread(self._wait_msg, ())
def _wait_msg(self):
while True:
if self.client is not None:
self.client.wait_msg()
utime.sleep_ms(300)
|