multilink_mqtt.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. -- 自动低功耗, 轻休眠模式
  2. -- Air780E、Air780EP支持uart唤醒和网络数据下发唤醒, 但需要断开USB,或者pm.power(pm.USB, false) 但这样也看不到日志了
  3. -- pm.request(pm.LIGHT)
  4. --根据自己的服务器修改以下参数
  5. local mqtt_host = "lbsmqtt.airm2m.com"
  6. local mqtt_port = 1884
  7. local mqtt_isssl = false
  8. local client1_id = "abc"
  9. local client2_id = "abc2"
  10. local user_name = "user"
  11. local password = "password"
  12. local device_id = mobile.imei()
  13. local mqttc1 = nil
  14. local pub_topic_client = "/luatos/pub/client1/"
  15. local sub_topic_client = "/luatos/sub/client1/"
  16. -- local pub_topic2_client = "/luatos/2"
  17. -- local pub_topic3_client = "/luatos/3"
  18. local mqttc2 = nil
  19. local pub_topic_client2 = "/luatos/pub/client2/"
  20. local sub_topic_client2 = "/luatos/sub/client2/"
  21. -- local pub_topic2_client2 = "/luatos/2"
  22. -- local pub_topic3_client2 = "/luatos/3"
  23. -- 统一联网函数
  24. sys.taskInit(function()
  25. -- 默认都等到联网成功
  26. sys.waitUntil("IP_READY")
  27. sys.publish("net_ready")
  28. end)
  29. sys.taskInit(function()
  30. -- 等待联网
  31. local ret= sys.waitUntil("net_ready")
  32. -- 下面的是mqtt的参数均可自行修改
  33. pub_topic_client = pub_topic_client .. device_id
  34. sub_topic_client = sub_topic_client .. device_id
  35. -- 打印一下上报(pub)和下发(sub)的topic名称
  36. -- 上报: 设备 ---> 服务器
  37. -- 下发: 设备 <--- 服务器
  38. -- 可使用mqtt.x等客户端进行调试
  39. log.info("mqtt", "pub", pub_topic_client)
  40. log.info("mqtt", "sub", sub_topic_client)
  41. -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
  42. -- if crypto.cipher_suites then
  43. -- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  44. -- end
  45. if mqtt == nil then
  46. while 1 do
  47. sys.wait(1000)
  48. log.info("bsp", "本bsp未适配mqtt库, 请查证")
  49. end
  50. end
  51. -------------------------------------
  52. -------- MQTT 演示代码 --------------
  53. -------------------------------------
  54. mqttc1 = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl)
  55. mqttc1:auth(client1_id,user_name,password) -- client_id必填,其余选填
  56. -- mqttc1:keepalive(240) -- 默认值240s
  57. mqttc1:autoreconn(true, 3000) -- 自动重连机制
  58. mqttc1:on(function(mqtt_client, event, data, payload)
  59. -- 用户自定义代码
  60. log.info("mqtt", "event", event, mqtt_client, data, payload)
  61. if event == "conack" then
  62. -- 联上了
  63. sys.publish("mqtt_conack")
  64. mqtt_client:subscribe(sub_topic_client)--单主题订阅
  65. -- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
  66. elseif event == "recv" then
  67. log.info("mqtt", "downlink", "topic", data, "payload", payload)
  68. sys.publish("mqtt_payload", data, payload)
  69. elseif event == "sent" then
  70. -- log.info("mqtt", "sent", "pkgid", data)
  71. -- elseif event == "disconnect" then
  72. -- 非自动重连时,按需重启mqttc
  73. -- mqtt_client:connect()
  74. end
  75. end)
  76. -- mqttc自动处理重连, 除非自行关闭
  77. mqttc1:connect()
  78. sys.waitUntil("mqtt_conack")
  79. while true do
  80. -- 演示等待其他task发送过来的上报信息
  81. local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
  82. if ret then
  83. -- 提供关闭本while循环的途径, 不需要可以注释掉
  84. if topic == "close" then break end
  85. mqttc1:publish(topic, data, qos)
  86. end
  87. -- 如果没有其他task上报, 可以写个空等待
  88. --sys.wait(60000000)
  89. end
  90. mqttc1:close()
  91. mqttc1 = nil
  92. end)
  93. -- 这里演示在另一个task里上报数据, 会定时上报数据,不需要就注释掉
  94. sys.taskInit(function()
  95. sys.wait(3000)
  96. local data = "123,"
  97. local qos = 1 -- QOS0不带puback, QOS1是带puback的
  98. while true do
  99. sys.wait(3000)
  100. if mqttc1 and mqttc1:ready() then
  101. local pkgid = mqttc1:publish(pub_topic_client, data .. os.date(), qos)
  102. -- local pkgid = mqttc1:publish(topic2, data, qos)
  103. -- local pkgid = mqttc1:publish(topic3, data, qos)
  104. end
  105. end
  106. end)
  107. -- mqtt多链接示例
  108. sys.taskInit(function()
  109. -- 等待联网
  110. local ret= sys.waitUntil("net_ready")
  111. -- 下面的是mqtt的参数均可自行修改
  112. client2_id = device_id.."2"
  113. pub_topic_client2 = pub_topic_client2 .. device_id
  114. sub_topic_client2 = sub_topic_client2 .. device_id
  115. -- 打印一下上报(pub)和下发(sub)的topic名称
  116. -- 上报: 设备 ---> 服务器
  117. -- 下发: 设备 <--- 服务器
  118. -- 可使用mqtt.x等客户端进行调试
  119. log.info("mqtt", "pub", pub_topic_client2)
  120. log.info("mqtt", "sub", sub_topic_client2)
  121. -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
  122. -- if crypto.cipher_suites then
  123. -- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  124. -- end
  125. if mqtt == nil then
  126. while 1 do
  127. sys.wait(1000)
  128. log.info("bsp", "本bsp未适配mqtt库, 请查证")
  129. end
  130. end
  131. -------------------------------------
  132. -------- MQTT 演示代码 --------------
  133. -------------------------------------
  134. mqttc2 = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl)
  135. mqttc2:auth(client2_id,user_name,password) -- client_id必填,其余选填
  136. -- mqttc2:keepalive(240) -- 默认值240s
  137. mqttc2:autoreconn(true, 3000) -- 自动重连机制
  138. mqttc2:on(function(mqtt_client, event, data, payload)
  139. -- 用户自定义代码
  140. log.info("mqtt", "event", event, mqtt_client, data, payload)
  141. if event == "conack" then
  142. -- 联上了
  143. sys.publish("mqtt_conack")
  144. mqtt_client:subscribe(sub_topic_client2)--单主题订阅
  145. -- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
  146. elseif event == "recv" then
  147. log.info("mqtt", "downlink", "topic", data, "payload", payload)
  148. sys.publish("mqtt_payload", data, payload)
  149. elseif event == "sent" then
  150. log.info("mqtt", "sent", "pkgid", data)
  151. elseif event == "disconnect" then
  152. -- 非自动重连时,按需重启mqttc
  153. mqtt_client:connect()
  154. end
  155. end)
  156. -- mqttc自动处理重连, 除非自行关闭
  157. mqttc2:connect()
  158. sys.waitUntil("mqtt_conack")
  159. while true do
  160. -- 演示等待其他task发送过来的上报信息
  161. local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
  162. if ret then
  163. -- 提供关闭本while循环的途径, 不需要可以注释掉
  164. if topic == "close" then break end
  165. mqttc2:publish(topic, data, qos)
  166. end
  167. -- 如果没有其他task上报, 可以写个空等待
  168. --sys.wait(60000000)
  169. end
  170. mqttc2:close()
  171. mqttc2 = nil
  172. end)
  173. -- 这里演示在另一个task里上报数据, 会定时上报数据,不需要就注释掉
  174. sys.taskInit(function()
  175. sys.wait(3000)
  176. local data = "123,"
  177. local qos = 1 -- QOS0不带puback, QOS1是带puback的
  178. while true do
  179. sys.wait(3000)
  180. if mqttc2 and mqttc2:ready() then
  181. local pkgid = mqttc2:publish(pub_topic_client2, data .. os.date(), qos)
  182. -- local pkgid = mqttc2:publish(topic2, data, qos)
  183. -- local pkgid = mqttc2:publish(topic3, data, qos)
  184. end
  185. end
  186. end)
  187. sys.taskInit(function ()
  188. while true do
  189. sys.wait(3000)
  190. log.info("lua", rtos.meminfo())
  191. log.info("sys", rtos.meminfo("sys"))
  192. end
  193. end)
  194. -- 用户代码已结束---------------------------------------------
  195. -- 结尾总是这一句
  196. sys.run()
  197. -- sys.run()之后后面不要加任何语句!!!!!