multilink_mqtt.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 mqttc1 = nil
  13. local pub_topic_client = "/luatos/pub/client1/"
  14. local sub_topic_client = "/luatos/sub/client1/"
  15. -- local pub_topic2_client = "/luatos/2"
  16. -- local pub_topic3_client = "/luatos/3"
  17. local mqttc2 = nil
  18. local pub_topic_client2 = "/luatos/pub/client2/"
  19. local sub_topic_client2 = "/luatos/sub/client2/"
  20. -- local pub_topic2_client2 = "/luatos/2"
  21. -- local pub_topic3_client2 = "/luatos/3"
  22. -- 统一联网函数
  23. sys.taskInit(function()
  24. local device_id = mcu.unique_id():toHex()
  25. -----------------------------
  26. -- 统一联网函数, 可自行删减
  27. ----------------------------
  28. if wlan and wlan.connect then
  29. -- wifi 联网, ESP32系列均支持
  30. local ssid = "luatos1234"
  31. local password = "12341234"
  32. log.info("wifi", ssid, password)
  33. -- TODO 改成自动配网
  34. -- LED = gpio.setup(12, 0, gpio.PULLUP)
  35. wlan.init()
  36. wlan.setMode(wlan.STATION) -- 默认也是这个模式,不调用也可以
  37. device_id = wlan.getMac()
  38. wlan.connect(ssid, password, 1)
  39. elseif mobile then
  40. -- Air780E/Air600E系列
  41. --mobile.simid(2) -- 自动切换SIM卡
  42. -- LED = gpio.setup(27, 0, gpio.PULLUP)
  43. device_id = mobile.imei()
  44. elseif w5500 then
  45. -- w5500 以太网, 当前仅Air105支持
  46. w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
  47. w5500.config() --默认是DHCP模式
  48. w5500.bind(socket.ETH0)
  49. -- LED = gpio.setup(62, 0, gpio.PULLUP)
  50. elseif socket or mqtt then
  51. -- 适配的socket库也OK
  52. -- 没有其他操作, 单纯给个注释说明
  53. else
  54. -- 其他不认识的bsp, 循环提示一下吧
  55. while 1 do
  56. sys.wait(1000)
  57. log.info("bsp", "本bsp可能未适配网络层, 请查证")
  58. end
  59. end
  60. -- 默认都等到联网成功
  61. sys.waitUntil("IP_READY")
  62. sys.publish("net_ready", device_id)
  63. end)
  64. sys.taskInit(function()
  65. -- 等待联网
  66. local ret, device_id = sys.waitUntil("net_ready")
  67. -- 下面的是mqtt的参数均可自行修改
  68. -- client1_id = device_id
  69. pub_topic_client = pub_topic_client .. device_id
  70. sub_topic_client = sub_topic_client .. device_id
  71. -- 打印一下上报(pub)和下发(sub)的topic名称
  72. -- 上报: 设备 ---> 服务器
  73. -- 下发: 设备 <--- 服务器
  74. -- 可使用mqtt.x等客户端进行调试
  75. log.info("mqtt", "pub", pub_topic_client)
  76. log.info("mqtt", "sub", sub_topic_client)
  77. -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
  78. -- if crypto.cipher_suites then
  79. -- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  80. -- end
  81. if mqtt == nil then
  82. while 1 do
  83. sys.wait(1000)
  84. log.info("bsp", "本bsp未适配mqtt库, 请查证")
  85. end
  86. end
  87. -------------------------------------
  88. -------- MQTT 演示代码 --------------
  89. -------------------------------------
  90. mqttc1 = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl)
  91. mqttc1:auth(client1_id,user_name,password) -- client_id必填,其余选填
  92. -- mqttc1:keepalive(240) -- 默认值240s
  93. mqttc1:autoreconn(true, 3000) -- 自动重连机制
  94. mqttc1:on(function(mqtt_client, event, data, payload)
  95. -- 用户自定义代码
  96. log.info("mqtt", "event", event, mqtt_client, data, payload)
  97. if event == "conack" then
  98. -- 联上了
  99. sys.publish("mqtt_conack")
  100. mqtt_client:subscribe(sub_topic_client)--单主题订阅
  101. -- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
  102. elseif event == "recv" then
  103. log.info("mqtt", "downlink", "topic", data, "payload", payload)
  104. sys.publish("mqtt_payload", data, payload)
  105. elseif event == "sent" then
  106. -- log.info("mqtt", "sent", "pkgid", data)
  107. -- elseif event == "disconnect" then
  108. -- 非自动重连时,按需重启mqttc
  109. -- mqtt_client:connect()
  110. end
  111. end)
  112. -- mqttc自动处理重连, 除非自行关闭
  113. mqttc1:connect()
  114. sys.waitUntil("mqtt_conack")
  115. while true do
  116. -- 演示等待其他task发送过来的上报信息
  117. local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
  118. if ret then
  119. -- 提供关闭本while循环的途径, 不需要可以注释掉
  120. if topic == "close" then break end
  121. mqttc1:publish(topic, data, qos)
  122. end
  123. -- 如果没有其他task上报, 可以写个空等待
  124. --sys.wait(60000000)
  125. end
  126. mqttc1:close()
  127. mqttc1 = nil
  128. end)
  129. -- 这里演示在另一个task里上报数据, 会定时上报数据,不需要就注释掉
  130. sys.taskInit(function()
  131. sys.wait(3000)
  132. local data = "123,"
  133. local qos = 1 -- QOS0不带puback, QOS1是带puback的
  134. while true do
  135. sys.wait(3000)
  136. if mqttc1 and mqttc1:ready() then
  137. local pkgid = mqttc1:publish(pub_topic_client, data .. os.date(), qos)
  138. -- local pkgid = mqttc1:publish(topic2, data, qos)
  139. -- local pkgid = mqttc1:publish(topic3, data, qos)
  140. end
  141. end
  142. end)
  143. -- mqtt多链接示例
  144. sys.taskInit(function()
  145. -- 等待联网
  146. local ret, device_id = sys.waitUntil("net_ready")
  147. -- 下面的是mqtt的参数均可自行修改
  148. client2_id = device_id.."2"
  149. pub_topic_client2 = pub_topic_client2 .. device_id
  150. sub_topic_client2 = sub_topic_client2 .. device_id
  151. -- 打印一下上报(pub)和下发(sub)的topic名称
  152. -- 上报: 设备 ---> 服务器
  153. -- 下发: 设备 <--- 服务器
  154. -- 可使用mqtt.x等客户端进行调试
  155. log.info("mqtt", "pub", pub_topic_client2)
  156. log.info("mqtt", "sub", sub_topic_client2)
  157. -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
  158. -- if crypto.cipher_suites then
  159. -- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  160. -- end
  161. if mqtt == nil then
  162. while 1 do
  163. sys.wait(1000)
  164. log.info("bsp", "本bsp未适配mqtt库, 请查证")
  165. end
  166. end
  167. -------------------------------------
  168. -------- MQTT 演示代码 --------------
  169. -------------------------------------
  170. mqttc2 = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl)
  171. mqttc2:auth(client2_id,user_name,password) -- client_id必填,其余选填
  172. -- mqttc2:keepalive(240) -- 默认值240s
  173. mqttc2:autoreconn(true, 3000) -- 自动重连机制
  174. mqttc2:on(function(mqtt_client, event, data, payload)
  175. -- 用户自定义代码
  176. log.info("mqtt", "event", event, mqtt_client, data, payload)
  177. if event == "conack" then
  178. -- 联上了
  179. sys.publish("mqtt_conack")
  180. mqtt_client:subscribe(sub_topic_client2)--单主题订阅
  181. -- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
  182. elseif event == "recv" then
  183. log.info("mqtt", "downlink", "topic", data, "payload", payload)
  184. sys.publish("mqtt_payload", data, payload)
  185. elseif event == "sent" then
  186. -- log.info("mqtt", "sent", "pkgid", data)
  187. -- elseif event == "disconnect" then
  188. -- 非自动重连时,按需重启mqttc
  189. -- mqtt_client:connect()
  190. end
  191. end)
  192. -- mqttc自动处理重连, 除非自行关闭
  193. mqttc2:connect()
  194. sys.waitUntil("mqtt_conack")
  195. while true do
  196. -- 演示等待其他task发送过来的上报信息
  197. local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
  198. if ret then
  199. -- 提供关闭本while循环的途径, 不需要可以注释掉
  200. if topic == "close" then break end
  201. mqttc2:publish(topic, data, qos)
  202. end
  203. -- 如果没有其他task上报, 可以写个空等待
  204. --sys.wait(60000000)
  205. end
  206. mqttc2:close()
  207. mqttc2 = nil
  208. end)
  209. -- 这里演示在另一个task里上报数据, 会定时上报数据,不需要就注释掉
  210. sys.taskInit(function()
  211. sys.wait(3000)
  212. local data = "123,"
  213. local qos = 1 -- QOS0不带puback, QOS1是带puback的
  214. while true do
  215. sys.wait(3000)
  216. if mqttc2 and mqttc2:ready() then
  217. local pkgid = mqttc2:publish(pub_topic_client2, data .. os.date(), qos)
  218. -- local pkgid = mqttc2:publish(topic2, data, qos)
  219. -- local pkgid = mqttc2:publish(topic3, data, qos)
  220. end
  221. end
  222. end)
  223. sys.taskInit(function ()
  224. while true do
  225. sys.wait(3000)
  226. log.info("lua", rtos.meminfo())
  227. log.info("sys", rtos.meminfo("sys"))
  228. end
  229. end)
  230. -- 用户代码已结束---------------------------------------------
  231. -- 结尾总是这一句
  232. sys.run()
  233. -- sys.run()之后后面不要加任何语句!!!!!