mqttTask.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. --- 模块功能:MQTT客户端处理框架
  2. -- @author openLuat
  3. -- @module mqtt.mqttTask
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.28
  7. module(...,package.seeall)
  8. require"misc"
  9. require"mqtt"
  10. require"mqttOutMsg"
  11. require"mqttInMsg"
  12. require"http"
  13. require 'pins'
  14. local ready = false
  15. --- MQTT连接是否处于激活状态
  16. -- @return 激活状态返回true,非激活状态返回false
  17. -- @usage mqttTask.isReady()
  18. function isReady()
  19. return ready
  20. end
  21. local date = {
  22. mode = 1, -- 1表示客户端;2表示服务器;默认为1
  23. intPin = pio.P0_22, -- 以太网芯片中断通知引脚
  24. rstPin = pio.P0_21, -- 复位以太网芯片引脚
  25. powerFunc=function ( state )
  26. if state then
  27. local setGpioFnc_TX = pins.setup(pio.P0_7, 0)
  28. pmd.ldoset(15, pmd.LDO_VMMC)
  29. else
  30. pmd.ldoset(0, pmd.LDO_VMMC)
  31. local setGpioFnc_TX = pins.setup(pio.P0_7, 1)
  32. end
  33. end,
  34. spi = {spi.SPI_1, 0, 0, 8, 800000} -- SPI通道参数,id,cpha,cpol,dataBits,clock,默认spi.SPI_1,0,0,8,800000
  35. }
  36. --启动MQTT客户端任务
  37. sys.taskInit(
  38. function()
  39. local retryConnectCnt = 0
  40. link.openNetwork(link.CH395, date)
  41. while true do
  42. if not socket.isReady() then
  43. retryConnectCnt = 0
  44. --等待网络环境准备就绪,超时时间是5分钟
  45. sys.waitUntil("IP_READY_IND",300000)
  46. end
  47. if socket.isReady() then
  48. local imei = misc.getImei()
  49. --创建一个MQTT客户端
  50. local mqttClient = mqtt.client(imei,600,"user","password")
  51. --阻塞执行MQTT CONNECT动作,直至成功
  52. --如果使用ssl连接,打开mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"}),根据自己的需求配置
  53. --mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"})
  54. if mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp") then
  55. log.info('mqtt连接')
  56. retryConnectCnt = 0
  57. ready = true
  58. --订阅主题
  59. if mqttClient:subscribe({["/event0"]=0, ["/中文event1"]=1}) then
  60. mqttOutMsg.init()
  61. --循环处理接收和发送的数据
  62. while true do
  63. if not mqttInMsg.proc(mqttClient) then log.error("mqttTask.mqttInMsg.proc error") break end
  64. if not mqttOutMsg.proc(mqttClient) then log.error("mqttTask.mqttOutMsg proc error") break end
  65. end
  66. mqttOutMsg.unInit()
  67. end
  68. ready = false
  69. else
  70. retryConnectCnt = retryConnectCnt+1
  71. end
  72. --断开MQTT连接
  73. log.info('mqtt关闭')
  74. mqttClient:disconnect()
  75. if retryConnectCnt>=5 then link.shut() retryConnectCnt=0 end
  76. sys.wait(5000)
  77. else
  78. link.closeNetWork()
  79. sys.wait(20000)
  80. link.openNetwork(link.CH395, date)
  81. end
  82. end
  83. end
  84. )
  85. local function cbFncFile(result,prompt,head,filePath)
  86. log.info("testHttp.cbFncFile",result,prompt,filePath)
  87. if result and head then
  88. for k,v in pairs(head) do
  89. log.info("testHttp.cbFncFile",k..": "..v)
  90. end
  91. end
  92. if result and filePath then
  93. local size = io.fileSize(filePath)
  94. log.info("testHttp.cbFncFile","fileSize="..size)
  95. --输出文件内容,如果文件太大,一次性读出文件内容可能会造成内存不足,分次读出可以避免此问题
  96. if size<=4096 then
  97. log.info("testHttp.cbFncFile",io.readFile(filePath))
  98. else
  99. end
  100. end
  101. --文件使用完之后,如果以后不再用到,需要自行删除
  102. if filePath then os.remove(filePath) end
  103. end
  104. -- sys.taskInit(function ( )
  105. -- while true do
  106. -- sys.wait(30000)
  107. -- log.info('开始下载')
  108. -- http.request("GET","http://cdn.openluat-luatcommunity.openluat.com/attachment/20211208190511374_1.zip",nil,nil,nil,30000,cbFncFile,"123.zip")
  109. -- sys.wait(30000)
  110. -- end
  111. -- end)
  112. sys.timerLoopStart(function ()
  113. log.info("打印占用的内存:", _G.collectgarbage("count"))
  114. end,5000)