testMqtt.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. local mqttc = nil
  2. -- mqtt 上传任务
  3. sys.taskInit(function()
  4. sys.waitUntil("IP_READY", 15000)
  5. mqttc = mqtt.create(nil, "lbsmqtt.airm2m.com", 1886) -- mqtt客户端创建
  6. mqttc:auth(mobile.imei(), mobile.imei(), mobile.muid()) -- mqtt三元组配置
  7. log.info("mqtt", mobile.imei(), mobile.imei(), mobile.muid())
  8. mqttc:keepalive(30) -- 默认值240s
  9. mqttc:autoreconn(true, 3000) -- 自动重连机制
  10. mqttc:on(function(mqtt_client, event, data, payload) -- mqtt回调注册
  11. -- 用户自定义代码,按event处理
  12. -- log.info("mqtt", "event", event, mqtt_client, data, payload)
  13. if event == "conack" then -- mqtt成功完成鉴权后的消息
  14. sys.publish("mqtt_conack") -- 小写字母的topic均为自定义topic
  15. -- 订阅不是必须的,但一般会有
  16. mqtt_client:subscribe("/gnss/" .. mobile.imei() .. "/down/#")
  17. elseif event == "recv" then -- 服务器下发的数据
  18. log.info("mqtt", "downlink", "topic", data, "payload", payload)
  19. local dl = json.decode(data)
  20. if dl then
  21. -- 检测命令
  22. if dl.cmd then
  23. -- 直接写uart
  24. if dl.cmd == "uart" and dl.data then
  25. uart.write(gps_uart_id, dl.data)
  26. -- 重启命令
  27. elseif dl.cmd == "reboot" then
  28. rtos.reboot()
  29. elseif dl.cmd == "stat" then
  30. upload_stat()
  31. end
  32. end
  33. end
  34. elseif event == "sent" then -- publish成功后的事件
  35. log.info("mqtt", "sent", "pkgid", data)
  36. end
  37. end)
  38. -- 发起连接之后,mqtt库会自动维护链接,若连接断开,默认会自动重连
  39. mqttc:connect()
  40. -- sys.waitUntil("mqtt_conack")
  41. -- log.info("mqtt连接成功")
  42. sys.timerStart(upload_stat, 3000) -- 一秒后主动上传一次
  43. while true do
  44. sys.wait(60*1000)
  45. end
  46. mqttc:close()
  47. mqttc = nil
  48. end)
  49. sys.taskInit(function()
  50. while 1 do
  51. sys.wait(3600 * 1000) -- 一小时检查一次
  52. local fixed, time_fixed = libgnss.isFix()
  53. if not fixed then
  54. exec_agnss()
  55. end
  56. end
  57. end)
  58. sys.timerLoopStart(upload_stat, 60000)
  59. sys.taskInit(function()
  60. local msgs = {}
  61. while 1 do
  62. local ret, topic, data, qos = sys.waitUntil("uplink", 30000)
  63. if ret then
  64. if topic == "close" then
  65. break
  66. end
  67. log.info("mqtt", "publish", "topic", topic)
  68. -- if #data > 512 then
  69. -- local start = mcu.ticks()
  70. -- local cdata = miniz.compress(data)
  71. -- local endt = mcu.ticks() - start
  72. -- if cdata then
  73. -- log.info("miniz", #data, #cdata, endt)
  74. -- end
  75. -- end
  76. if mqttc:ready() then
  77. local tmp = msgs
  78. if #tmp > 0 then
  79. log.info("mqtt", "ready, send buff", #tmp)
  80. msgs = {}
  81. for k, msg in pairs(tmp) do
  82. mqttc:publish(msg.topic, msg.data, 0)
  83. end
  84. end
  85. mqttc:publish(topic, data, qos)
  86. else
  87. log.info("mqtt", "not ready, insert into buff")
  88. if #msgs > 60 then
  89. table.remove(msgs, 1)
  90. end
  91. table.insert(msgs, {
  92. topic = topic,
  93. data = data
  94. })
  95. end
  96. end
  97. end
  98. end)
  99. function upload_stat()
  100. if mqttc == nil or not mqttc:ready() then return end
  101. local stat = {
  102. csq = mobile.csq(),
  103. rssi = mobile.rssi(),
  104. rsrq = mobile.rsrq(),
  105. rsrp = mobile.rsrp(),
  106. -- iccid = mobile.iccid(),
  107. snr = mobile.snr(),
  108. vbat = adc.get(adc.CH_VBAT),
  109. temp = adc.get(adc.CH_CPU),
  110. memsys = {rtos.meminfo("sys")},
  111. memlua = {rtos.meminfo()},
  112. fixed = libgnss.isFix()
  113. }
  114. sys.publish("uplink", "/gnss/" .. mobile.imei() .. "/up/stat", (json.encode(stat)), 1)
  115. end
  116. sys.timerLoopStart(upload_stat, 60 * 1000)