main.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import _thread
  2. import time
  3. import utils
  4. from config import getConfig
  5. from httpServer import startHttpServer,closeHttpServer
  6. # 硬重启
  7. import machine
  8. def taskCheck():
  9. """
  10. 定时检查连接
  11. :return:
  12. """
  13. time_sleep = 10
  14. count = 0
  15. while True:
  16. if getConfig().runTask:
  17. try:
  18. if utils.getStatus():
  19. print('WiFi 正常', utils.getWifiConnectInfo())
  20. if getConfig().mqttStatus :
  21. print('MQTT 正常', getConfig().mqtt.host, getConfig().mqtt.port,getConfig().mqtt.subTopic)
  22. if getConfig().mqtt.clientId is "":
  23. print("设备未绑定")
  24. utils.send_ping()
  25. count = 0
  26. # time_sleep = 60
  27. else:
  28. print('MQTT 未连接', getConfig().mqtt.host, getConfig().mqtt.port,getConfig().mqtt.subTopic)
  29. utils.connectMQTT(time_sleep)
  30. else:
  31. print('WiFi 未连接')
  32. if utils.autoConnectWifi():
  33. utils.connectMQTT(time_sleep)
  34. # 没有客户的id表示没被绑定
  35. if not utils.getStatus() or not getConfig().mqttStatus or getConfig().mqtt.clientId is "":
  36. count += 1
  37. time_sleep = 10
  38. # 连接失败开启热点
  39. utils.openAp()
  40. startHttpServer()
  41. else:
  42. closeHttpServer()
  43. utils.closeAp()
  44. if count > 20:
  45. machine.reset()
  46. except Exception as e:
  47. print(f"连接失败:未知错误 - {e}")
  48. time.sleep(time_sleep)
  49. try:
  50. _thread.start_new_thread(taskCheck, ())
  51. except:
  52. machine.reset()