main.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "iotclouddemo"
  3. VERSION = "1.0.0"
  4. -- sys库是标配
  5. _G.sys = require("sys")
  6. --[[特别注意, 使用http库需要下列语句]]
  7. _G.sysplus = require("sysplus")
  8. local iotcloud = require("iotcloud")
  9. -- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦
  10. if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then
  11. pm.power(pm.PWK_MODE, false)
  12. end
  13. -- 统一联网函数
  14. sys.taskInit(function()
  15. local device_id = mcu.unique_id():toHex()
  16. -----------------------------
  17. -- 统一联网函数, 可自行删减
  18. ----------------------------
  19. if wlan and wlan.connect then
  20. -- wifi 联网, ESP32系列均支持
  21. local ssid = "luatos1234"
  22. local password = "12341234"
  23. log.info("wifi", ssid, password)
  24. -- TODO 改成自动配网
  25. -- LED = gpio.setup(12, 0, gpio.PULLUP)
  26. wlan.init()
  27. wlan.setMode(wlan.STATION) -- 默认也是这个模式,不调用也可以
  28. device_id = wlan.getMac()
  29. wlan.connect(ssid, password, 1)
  30. elseif mobile then
  31. -- Air780E/Air600E系列
  32. --mobile.simid(2) -- 自动切换SIM卡
  33. -- LED = gpio.setup(27, 0, gpio.PULLUP)
  34. device_id = mobile.imei()
  35. elseif w5500 then
  36. -- w5500 以太网, 当前仅Air105支持
  37. w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
  38. w5500.config() --默认是DHCP模式
  39. w5500.bind(socket.ETH0)
  40. -- LED = gpio.setup(62, 0, gpio.PULLUP)
  41. elseif socket or mqtt then
  42. -- 适配的socket库也OK
  43. -- 没有其他操作, 单纯给个注释说明
  44. else
  45. -- 其他不认识的bsp, 循环提示一下吧
  46. while 1 do
  47. sys.wait(1000)
  48. log.info("bsp", "本bsp可能未适配网络层, 请查证")
  49. end
  50. end
  51. -- 默认都等到联网成功
  52. sys.waitUntil("IP_READY")
  53. sys.publish("net_ready", device_id)
  54. end)
  55. sys.taskInit(function()
  56. -- 等待联网
  57. local ret, device_id = sys.waitUntil("net_ready")
  58. -------- 以下接入方式根据自己需要修改,相关参数修改为自己的 ---------
  59. -- 腾讯云
  60. -- 动态注册
  61. -- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx" ,product_secret = "xxx"})
  62. -- 密钥校验
  63. -- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx",device_name = "123456789",device_secret = "xxx=="})
  64. -- 证书校验
  65. -- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx",device_name = "123456789"},{tls={client_cert=io.readFile("/luadb/client_cert.crt")}})
  66. -- 阿里云
  67. -- 动态注册(免预注册)(一型一密)(仅企业版支持)
  68. -- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{instance_id = "xxx",produt_id = "xxx",product_secret = "xxx"}) -- 企业版公共实例
  69. -- 动态注册(预注册)(一型一密)
  70. -- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{produt_id = "xxx",device_name = "xxx",product_secret = "xxx"}) -- 旧版公共实例
  71. -- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{instance_id = "xxx",produt_id = "xxx",device_name = "xxx",product_secret = "xxx"}) -- 新版公共实例
  72. -- 密钥校验 (预注册)(一机一密)
  73. -- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{produt_id = "xxx",device_name = "xxx",device_secret = "xxx"}) -- 旧版公共实例
  74. -- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{instance_id = "xxx",produt_id = "xxx",device_name = "xxx",device_secret = "xxx"})-- 新版公共实例
  75. -- ONENET云
  76. -- 动态注册
  77. -- iotcloudc = iotcloud.new(iotcloud.ONENET,{produt_id = "xxx",userid = "xxx",userkey = "xxx"})
  78. -- 一型一密
  79. -- iotcloudc = iotcloud.new(iotcloud.ONENET,{produt_id = "xxx",product_secret = "xxx"})
  80. -- 一机一密
  81. -- iotcloudc = iotcloud.new(iotcloud.ONENET,{produt_id = "xxx",device_name = "xxx",device_secret = "xxx"})
  82. -- 华为云
  83. -- 动态注册(免预注册)
  84. -- iotcloudc = iotcloud.new(iotcloud.HUAWEI,{produt_id = "xxx",project_id = "xxx",endpoint = "xxx",
  85. -- iam_username="xxx",iam_password="xxx",iam_domain="xxx"})
  86. -- 密钥校验 (预注册)
  87. -- iotcloudc = iotcloud.new(iotcloud.HUAWEI,{produt_id = "xxx",endpoint = "xxx",device_name = "xxx",device_secret = "xxx"})
  88. -- 涂鸦云
  89. -- iotcloudc = iotcloud.new(iotcloud.TUYA,{device_name = "xxx",device_secret = "xxx"})
  90. -- 百度云
  91. -- iotcloudc = iotcloud.new(iotcloud.BAIDU,{produt_id = "afadjlw",device_name = "test",device_secret = "BBDVsSRGefaknffT"})
  92. -- iotcloudc = iotcloud.new(iotcloud.BAIDU,{produt_id = "xxx",device_name = "xxx"},{tls={server_cert=io.readFile("/luadb/GlobalSign.cer"),client_cert=io.readFile("/luadb/client_cert"),client_key=io.readFile("/luadb/client_private_key")}})
  93. -- Tlink云
  94. -- iotcloudc = iotcloud.new(iotcloud.TLINK,{produt_id = "xxx",product_secret = "xxx",device_name = "xxx"})
  95. -- iotcloudc = iotcloud.new(iotcloud.TLINK,{produt_id = "xxx",product_secret = "xxx",device_name = "xxx"},{tls={client_cert=io.readFile("/luadb/client_cert.crt")}})
  96. if iotcloudc then
  97. iotcloudc:connect()
  98. else
  99. log.error("iotcloud", "创建失败, 请检查参数")
  100. end
  101. end)
  102. sys.subscribe("iotcloud", function(cloudc,event,data,payload)
  103. -- 注意,此处不是协程内,复杂操作发消息给协程内进行处理
  104. if event == iotcloud.CONNECT then -- 云平台联上了
  105. print("iotcloud","CONNECT", "云平台连接成功")
  106. -- cloudc:subscribe("test") -- 可以自由定阅主题等
  107. elseif event == iotcloud.RECEIVE then
  108. print("iotcloud","topic", data, "payload", payload)
  109. -- 用户处理代码
  110. elseif event == iotcloud.OTA then
  111. if data then
  112. rtos.reboot()
  113. end
  114. elseif event == iotcloud.DISCONNECT then -- 云平台断开了
  115. -- 用户处理代码
  116. end
  117. end)
  118. -- 用户代码已结束---------------------------------------------
  119. -- 结尾总是这一句
  120. sys.run()
  121. -- sys.run()之后后面不要加任何语句!!!!!