main.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. PROJECT = "adcdemo"
  2. VERSION = "1.0.0"
  3. sys = require 'sys'
  4. local mqtt = require "mqtt"
  5. _G.point = function(...)
  6. --local name = debug.getinfo(2).short_src
  7. --local line = debug.getinfo(2).currentline
  8. --print("[" .. name .. " : " .. line .. "]", ...)
  9. end
  10. require "wifi"
  11. -- require "mqtt"
  12. -- -- 这里请填写修改为自己的IP和端口
  13. -- local host, port = "lbsmqtt.airm2m.com", 1884
  14. sys.taskInit(function()
  15. -- 服务器配置信息
  16. local host, port, selfid = "1.15.81.195", 1883, "AIR105"
  17. sys.waitUntil("IP_READY_IND")
  18. -- 等待联网成功
  19. while true do
  20. while not socket.isReady() do
  21. log.info("net", "wait for network ready")
  22. sys.waitUntil("IP_READY_IND", 1000)
  23. end
  24. log.info("main", "Airm2m mqtt loop")
  25. local mqttc = mqtt.client("clientid-123")
  26. while not mqttc:connect(host, port) do sys.wait(2000) end
  27. local topic_req = string.format("/device")
  28. local topic_report = string.format("/device")
  29. local topic_resp = string.format("/device/%s/resp", selfid)
  30. log.info("mqttc", "mqtt seem ok", "try subscribe", topic_req)
  31. if mqttc:subscribe(topic_req) then
  32. log.info("mqttc", "mqtt subscribe ok", "try publish")
  33. if mqttc:publish(topic_report, "test publish " .. os.date(), 1) then
  34. while true do
  35. log.info("mqttc", "wait for new msg")
  36. local r, data, param = mqttc:receive(120000, "pub_msg")
  37. log.info("mqttc", "mqttc:receive", r, data, param)
  38. if r then
  39. log.info("mqttc", "get message from server", data.payload or "nil", data.topic)
  40. elseif data == "pub_msg" then
  41. log.info("mqttc", "send message to server", data, param)
  42. mqttc:publish(topic_resp, "response " .. param)
  43. elseif data == "timeout" then
  44. log.info("mqttc", "wait timeout, send custom report")
  45. mqttc:publish(topic_report, "test publish " .. os.date())
  46. else
  47. log.info("mqttc", "ok, something happen", "close connetion")
  48. break
  49. end
  50. end
  51. end
  52. end
  53. mqttc:disconnect()
  54. sys.wait(5000) -- 等待一小会, 免得疯狂重连
  55. end
  56. end)
  57. -- sys.taskInit(function()
  58. -- sys.waitUntil("IP_READY_IND")
  59. -- while true do
  60. -- while not socket.isReady() do
  61. -- sys.wait(1000)
  62. -- end
  63. -- local c = socket.tcp()
  64. -- while not c:connect("112.125.89.8", 36091) do
  65. -- sys.wait(2000)
  66. -- end
  67. -- while true do
  68. -- c:send("1234567890")
  69. -- r, s, p = c:recv(5000, "pub_msg")
  70. -- if r then
  71. -- log.info("这是收到了服务器下发的消息:", s)
  72. -- elseif s == "pub_msg" then
  73. -- log.info("这是收到了订阅的消息和参数显示:", s, p)
  74. -- if not c:send(p) then
  75. -- break
  76. -- end
  77. -- elseif s == "timeout" then
  78. -- log.info("这是等待超时发送心跳包的显示!")
  79. -- if not c:send("\0") then
  80. -- break
  81. -- end
  82. -- else
  83. -- log.info("这是socket连接错误的显示!")
  84. -- break
  85. -- end
  86. -- sys.wait(5000)
  87. -- end
  88. -- c:close()
  89. -- end
  90. -- end)
  91. -- print(uart.setup(4,115200))
  92. -- uart.on(4,"receive",function (id,len)
  93. -- print(uart.read(id,len))
  94. -- end)
  95. -- sys.timerLoopStart(uart.write,1000,4,"ATE0\r\n")
  96. sys.timerLoopStart(function()
  97. log.info("mem.lua", rtos.meminfo())
  98. -- log.info("mem.lua", rtos.meminfo("sys"))
  99. -- 打印占用的RAM
  100. collectgarbage("collect")
  101. end, 3000)
  102. sys.run()
  103. sys.run()