import _thread import time import utils from config import getConfig from httpServer import startHttpServer,closeHttpServer # 硬重启 import machine def taskCheck(): """ 定时检查连接 :return: """ time_sleep = 10 count = 0 while True: if getConfig().runTask: try: if utils.getStatus(): print('WiFi 正常', utils.getWifiConnectInfo()) if getConfig().mqttStatus : print('MQTT 正常', getConfig().mqtt.host, getConfig().mqtt.port,getConfig().mqtt.subTopic) if getConfig().mqtt.clientId is "": print("设备未绑定") utils.send_ping() count = 0 # time_sleep = 60 else: print('MQTT 未连接', getConfig().mqtt.host, getConfig().mqtt.port,getConfig().mqtt.subTopic) utils.connectMQTT(time_sleep) else: print('WiFi 未连接') if utils.autoConnectWifi(): utils.connectMQTT(time_sleep) # 没有客户的id表示没被绑定 if not utils.getStatus() or not getConfig().mqttStatus or getConfig().mqtt.clientId is "": count += 1 time_sleep = 10 # 连接失败开启热点 utils.openAp() startHttpServer() else: closeHttpServer() utils.closeAp() if count > 20: machine.reset() except Exception as e: print(f"连接失败:未知错误 - {e}") time.sleep(time_sleep) try: _thread.start_new_thread(taskCheck, ()) except: machine.reset()