main.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "mqttdemo"
  3. VERSION = "1.0.0"
  4. --[[
  5. 本demo需要mqtt库, 大部分能联网的设备都具有这个库
  6. mqtt也是内置库, 无需require
  7. ]]
  8. -- sys库是标配
  9. _G.sys = require("sys")
  10. --[[特别注意, 使用mqtt库需要下列语句]]
  11. _G.sysplus = require("sysplus")
  12. -- 自动低功耗, 轻休眠模式
  13. -- Air780E支持uart唤醒和网络数据下发唤醒, 但需要断开USB,或者pm.power(pm.USB, false) 但这样也看不到日志了
  14. -- pm.request(pm.LIGHT)
  15. --根据自己的服务器修改以下参数
  16. local mqtt_host = "lbsmqtt.airm2m.com"
  17. local mqtt_port = 1884
  18. local mqtt_isssl = false
  19. local client_id = "abc"
  20. local user_name = "user"
  21. local password = "password"
  22. local pub_topic = "/luatos/pub/" .. (mcu.unique_id():toHex())
  23. local sub_topic = "/luatos/sub/" .. (mcu.unique_id():toHex())
  24. -- local topic2 = "/luatos/2"
  25. -- local topic3 = "/luatos/3"
  26. local mqttc = nil
  27. sys.taskInit(function()
  28. -----------------------------
  29. -- 统一联网函数, 可自行删减
  30. ----------------------------
  31. if rtos.bsp():startsWith("ESP32") then
  32. -- wifi 联网, ESP32系列均支持
  33. local ssid = "uiot"
  34. local password = "12345678"
  35. log.info("wifi", ssid, password)
  36. -- TODO 改成esptouch配网
  37. -- LED = gpio.setup(12, 0, gpio.PULLUP)
  38. wlan.init()
  39. wlan.setMode(wlan.STATION)
  40. wlan.connect(ssid, password, 1)
  41. local result, data = sys.waitUntil("IP_READY", 30000)
  42. log.info("wlan", "IP_READY", result, data)
  43. device_id = wlan.getMac()
  44. pub_topic = "/luatos/pub/" .. (wlan.getMac():toHex())
  45. sub_topic = "/luatos/sub/" .. (wlan.getMac():toHex())
  46. client_id = wlan.getMac():toHex()
  47. elseif rtos.bsp() == "AIR105" then
  48. -- w5500 以太网, 当前仅Air105支持
  49. w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
  50. w5500.config() --默认是DHCP模式
  51. w5500.bind(socket.ETH0)
  52. -- LED = gpio.setup(62, 0, gpio.PULLUP)
  53. sys.wait(1000)
  54. pub_topic = "/luatos/pub/" .. (w5500.getMac():toHex())
  55. sub_topic = "/luatos/sub/" .. (w5500.getMac():toHex())
  56. elseif rtos.bsp() == "EC618" then
  57. -- Air780E/Air600E系列
  58. --mobile.simid(2) -- 自动切换SIM卡
  59. -- LED = gpio.setup(27, 0, gpio.PULLUP)
  60. device_id = mobile.imei()
  61. sys.waitUntil("IP_READY", 30000)
  62. pub_topic = "/luatos/pub/" .. (mobile.imei())
  63. sub_topic = "/luatos/sub/" .. (mobile.imei())
  64. client_id = mobile.imei()
  65. end
  66. -- 打印一下上报(pub)和下发(sub)的topic名称
  67. -- 上报: 设备 ---> 服务器
  68. -- 下发: 设备 <--- 服务器
  69. -- 可使用mqtt.x等客户端进行调试
  70. log.info("mqtt", "pub", pub_topic)
  71. log.info("mqtt", "sub", sub_topic)
  72. -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
  73. -- if crypto.cipher_suites then
  74. -- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  75. -- end
  76. -------------------------------------
  77. -------- MQTT 演示代码 --------------
  78. -------------------------------------
  79. mqttc = mqtt.create(nil,mqtt_host, mqtt_port, mqtt_isssl, ca_file)
  80. mqttc:auth(client_id,user_name,password) -- client_id必填,其余选填
  81. -- mqttc:keepalive(240) -- 默认值240s
  82. mqttc:autoreconn(true, 3000) -- 自动重连机制
  83. mqttc:on(function(mqtt_client, event, data, payload)
  84. -- 用户自定义代码
  85. log.info("mqtt", "event", event, mqtt_client, data, payload)
  86. if event == "conack" then
  87. sys.publish("mqtt_conack")
  88. mqtt_client:subscribe(sub_topic)--单主题订阅
  89. -- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
  90. elseif event == "recv" then
  91. log.info("mqtt", "downlink", "topic", data, "payload", payload)
  92. sys.publish("mqtt_payload", data, payload)
  93. elseif event == "sent" then
  94. log.info("mqtt", "sent", "pkgid", data)
  95. -- elseif event == "disconnect" then
  96. -- 非自动重连时,按需重启mqttc
  97. -- mqtt_client:connect()
  98. end
  99. end)
  100. mqttc:connect()
  101. sys.waitUntil("mqtt_conack")
  102. while true do
  103. -- mqttc自动处理重连
  104. local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
  105. if ret then
  106. if topic == "close" then break end
  107. mqttc:publish(topic, data, qos)
  108. end
  109. end
  110. mqttc:close()
  111. mqttc = nil
  112. end)
  113. -- 这里演示在另一个task里上报数据, 会定时上报数据,不需要就注释掉
  114. sys.taskInit(function()
  115. local data = "123,"
  116. local qos = 1 -- QOS0不带puback, QOS1是带puback的
  117. while true do
  118. sys.wait(60000)
  119. if mqttc and mqttc:ready() then
  120. local pkgid = mqttc:publish(pub_topic, data .. os.date(), qos)
  121. -- local pkgid = mqttc:publish(topic2, data, qos)
  122. -- local pkgid = mqttc:publish(topic3, data, qos)
  123. end
  124. end
  125. end)
  126. -- 以下是演示与uart结合, 简单的mqtt-uart透传实现,不需要就注释掉
  127. local uart_id = 1
  128. uart.setup(uart_id, 9600)
  129. uart.on(uart_id, "receive", function(id, len)
  130. local data = ""
  131. while 1 do
  132. local tmp = uart.read(uart_id)
  133. if not tmp or #tmp == 0 then
  134. break
  135. end
  136. data = data .. tmp
  137. end
  138. log.info("uart", "uart收到数据长度", #data)
  139. sys.publish("mqtt_pub", pub_topic, data)
  140. end)
  141. sys.subscribe("mqtt_payload", function(topic, payload)
  142. log.info("uart", "uart发送数据长度", #payload)
  143. uart.write(1, payload)
  144. end)
  145. -- 用户代码已结束---------------------------------------------
  146. -- 结尾总是这一句
  147. sys.run()
  148. -- sys.run()之后后面不要加任何语句!!!!!