main.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "aliyun_demo"
  3. VERSION = "1.0.0"
  4. -- 引入必要的库文件(lua编写), 内部库不需要require
  5. sys = require("sys")
  6. local mqtt = require "mqtt"
  7. log.info("version", _VERSION, VERSION)
  8. --- UART 相关------------------------------
  9. -- 配置uart2, 115200 8 N 1
  10. uart.on(2, "receive", function(id, len)
  11. log.info("uart", "receive", uart.read(id, 1024))
  12. end)
  13. uart.setup(2, 115200)
  14. -- GPIO 和 PWM 相关 -------------------------------
  15. -- 网络灯 GPIO19/PWM5
  16. gpio.setup(19, 0) -- 初始化GPIO19, 并设置为低电平
  17. gpio.set(19, 1) -- 设置为高电平
  18. -- pwm.open(5, 1000, 50) -- 初始化PWM5, 频率1000hz, 占空比50%
  19. -- GPIO18/PWM4
  20. -- GPIO17/PWM3
  21. -- 低功耗sleep2模式下依然能输出电平的AON系列GPIO,电平1.8v
  22. -- AON_GPIO2 --> GPIO21
  23. -- AON_GPIO3 --> GPIO23
  24. -- ADC相关---------------------------------------
  25. -- 通道 0-内部温度, 1-供电电压, 2 外部ADC管脚
  26. -- adc.open(2)
  27. -- adc.read(2)
  28. -- adc.close(2)
  29. local aliyun_msgid = 1
  30. local vPowerSwitch = 1
  31. function aliyun_params_post()
  32. --[[
  33. {
  34. "id": "123",
  35. "version": "1.0",
  36. "params": {
  37. "Power": {
  38. "value": "on",
  39. "time": 1524448722000
  40. },
  41. "WF": {
  42. "value": 23.6,
  43. "time": 1524448722000
  44. }
  45. },
  46. "method": "thing.event.property.post"
  47. }
  48. ]]
  49. aliyun_msgid = aliyun_msgid + 1
  50. local re = {
  51. id = tostring(aliyun_msgid),
  52. version = "1.0",
  53. params = {
  54. PowerSwitch = {
  55. value = vPowerSwitch,
  56. time = os.time() * 1000
  57. }
  58. -- ,RSSI = {
  59. -- value = nbiot.rssi(),
  60. -- time = os.time() * 1000
  61. -- },
  62. },
  63. method = "thing.event.property.post"
  64. }
  65. return json.encode(re)
  66. end
  67. -- 连接到阿里云物联网的Task
  68. sys.taskInit(function()
  69. sys.wait(2000)
  70. -- 阿里云物联网的设备信息
  71. -- https://help.aliyun.com/document_detail/73742.html?spm=a2c4g.11186623.6.593.11a22cf0rGX1bC
  72. -- deviceName 可以是imei, 也可以自定义, 填写正确才能连接上
  73. local productKey,deviceName,deviceSecret = "a1YFuY6OC1e","azNhIbNNTdsVwY2mhZno","5iRxTePbEMguOuZqltZrJBR0JjWJSdA7"
  74. local host, port, selfid = productKey .. ".iot-as-mqtt.cn-shanghai.aliyuncs.com", 1883, nbiot.imei()
  75. local mqttClientId = selfid .. "|securemode=3,signmethod=hmacsha1,timestamp=132323232|"
  76. local mqttUsername = deviceName .. "&" .. productKey
  77. local signstr = "clientId"..selfid.."deviceName"..deviceName.."productKey"..productKey.."timestamp".."132323232"
  78. local mqttPassword = crypto.hmac_sha1(signstr, deviceSecret)
  79. --log.info("aliiot", "mqttClientId", mqttClientId)
  80. --log.info("aliiot", "mqttUsername", mqttUsername)
  81. --log.info("aliiot", "signstr", signstr)
  82. --log.info("aliiot", "mqttPassword", mqttPassword)
  83. local topic_post = string.format("/sys/%s/%s/thing/event/property/post", productKey, deviceName)
  84. local topic_post_reply = string.format("/sys/%s/%s/thing/event/property/post_reply", productKey, deviceName)
  85. local topic_set = string.format("/sys/%s/%s/thing/service/property/set", productKey, deviceName)
  86. local topic_user_get = string.format("/%s/%s/user/get", productKey, deviceName)
  87. log.info("mqtt", "topic_post", topic_post)
  88. log.info("mqtt", "topic_set", topic_set)
  89. log.info("mqtt", "topic_user_get", topic_user_get)
  90. while true do
  91. -- 等待联网成功
  92. while not socket.isReady() do
  93. log.info("net", "wait for network ready")
  94. sys.waitUntil("NET_READY", 1000)
  95. end
  96. log.info("main", "net is ready!!")
  97. sys.wait(1000) -- 稍等一会
  98. -- 开始连接到阿里云物联网
  99. local mqttc = mqtt.client(mqttClientId, 240, mqttUsername, mqttPassword)
  100. -- 等待底层tcp连接完成
  101. while not mqttc:connect(host, port) do sys.wait(15000) end
  102. -- 连接成功, 开始订阅
  103. log.info("mqttc", "mqtt seem ok", "try subscribe", topic_set)
  104. if mqttc:subscribe(topic_set) then
  105. mqttc:subscribe(topic_post_reply)
  106. mqttc:subscribe(topic_user_get)
  107. -- 订阅完成, 发布业务数据
  108. log.info("mqttc", "mqtt subscribe ok", "try publish", topic_post)
  109. if mqttc:publish(topic_post, aliyun_params_post(), 1) then
  110. -- 发布也ok了, 等待数据下发或数据上传
  111. while true do
  112. log.info("mqttc", "wait for new msg")
  113. local r, data, param = mqttc:receive(120000, "pub_msg")
  114. log.info("mqttc", "mqttc:receive", r, data, param)
  115. if r then -- 有下发的数据
  116. log.info("mqttc", "get message from server", data.payload or "nil", data.topic)
  117. local tjsondata,result,errinfo = json.decode(data.payload)
  118. if result then
  119. log.info("mqtt", tjsondata.id, tjsondata.method)
  120. if tjsondata.method == "thing.service.property.set" and tjsondata.params then
  121. vPowerSwitch = tjsondata.params.PowerSwitch
  122. log.info("mqtt", "vPowerSwitch", "set as", vPowerSwitch)
  123. end
  124. else
  125. log.info("mqtt", "json.decode error",errinfo)
  126. end
  127. elseif data == "pub_msg" then -- 需要上报数据
  128. log.info("mqttc", "send message to server", data, param)
  129. mqttc:publish(topic_post, param, 1)
  130. elseif data == "timeout" then -- 无交互,发个定时report也行
  131. log.info("mqttc", "wait timeout, send custom report")
  132. mqttc:publish(topic_post, aliyun_params_post(), 1)
  133. else -- 其他情况不太可能,退出连接吧
  134. log.info("mqttc", "ok, something happen", "close connetion")
  135. break
  136. end
  137. end
  138. end
  139. end
  140. -- 关掉连接,清理资源
  141. mqttc:disconnect()
  142. -- 避免频繁重连, 必须加延时
  143. sys.wait(30000)
  144. end
  145. end)
  146. -- 用户代码已结束---------------------------------------------
  147. -- 结尾总是这一句
  148. sys.run()
  149. -- sys.run()之后后面不要加任何语句!!!!!