wan.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. -- 引入必要的库文件(lua编写), 内部库不需要require
  2. sys = require("sys")
  3. sysplus = require("sysplus")
  4. sys.taskInit(function ()
  5. -- sys.wait(3000)
  6. local result = spi.setup(
  7. 0,--spi id
  8. nil,
  9. 0,--CPHA
  10. 0,--CPOL
  11. 8,--数据宽度
  12. 25600000--,--频率
  13. -- spi.MSB,--高低位顺序 可选,默认高位在前
  14. -- spi.master,--主模式 可选,默认主
  15. -- spi.full--全双工 可选,默认全双工
  16. )
  17. log.info("main", "open",result)
  18. if result ~= 0 then--返回值为0,表示打开成功
  19. log.info("main", "spi open error",result)
  20. return
  21. end
  22. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spi=0,cs=8})
  23. netdrv.dhcp(socket.LWIP_ETH, true)
  24. end)
  25. LEDA = gpio.setup(27, 0, gpio.PULLUP)
  26. sys.taskInit(function()
  27. -- 等以太网就绪
  28. while 1 do
  29. local result, ip, adapter = sys.waitUntil("IP_READY", 3000)
  30. log.info("ready?", result, ip, adapter)
  31. if adapter and adapter == socket.LWIP_ETH then
  32. break
  33. end
  34. end
  35. sys.wait(200)
  36. httpsrv.start(80, function(client, method, uri, headers, body)
  37. log.info("httpsrv", method, uri, json.encode(headers), body)
  38. -- meminfo()
  39. if uri == "/led/1" then
  40. LEDA(1)
  41. return 200, {}, "ok"
  42. elseif uri == "/led/0" then
  43. LEDA(0)
  44. return 200, {}, "ok"
  45. end
  46. return 404, {}, "Not Found" .. uri
  47. end, socket.LWIP_ETH)
  48. iperf.server(socket.LWIP_ETH)
  49. if netdrv.on then
  50. netdrv.on(socket.LWIP_ETH, netdrv.EVT_SOCKET, function(id, event, params)
  51. log.info("netdrv", "socket event", id, event, json.encode(params or {}))
  52. end)
  53. else
  54. log.warn("netdrv", "not support netdrv.on")
  55. end
  56. while 1 do
  57. sys.wait(6000)
  58. -- 正常请求
  59. local code, headers, body = http.request("GET", "http://httpbin.air32.cn/bytes/4096", nil, nil, {adapter=socket.LWIP_ETH}).wait()
  60. log.info("http", code, headers, body and #body)
  61. -- 故意失败1
  62. local code, headers, body = http.request("GET", "http://httpbin.air323.cn/status/404", nil, nil, {adapter=socket.LWIP_ETH, timeout=5000}).wait()
  63. log.info("http", code, headers, body and #body)
  64. -- 故意失败2
  65. local code, headers, body = http.request("GET", "http://112.125.89.8:40000/status/404", nil, nil, {adapter=socket.LWIP_ETH, timeout=5000}).wait()
  66. log.info("http", code, headers, body and #body)
  67. -- log.info("lua", rtos.meminfo())
  68. -- log.info("sys", rtos.meminfo("sys"))
  69. end
  70. end)
  71. --根据自己的服务器修改以下参数
  72. local mqtt_host = "lbsmqtt.airm2m.com"
  73. local mqtt_port = 18840
  74. local mqtt_isssl = false
  75. local client_id = "abc"
  76. local user_name = "user"
  77. local password = "password"
  78. local pub_topic = "/luatos/pub/" .. (mcu.unique_id():toHex())
  79. local sub_topic = "/luatos/sub/" .. (mcu.unique_id():toHex())
  80. -- local topic2 = "/luatos/2"
  81. -- local topic3 = "/luatos/3"
  82. local mqttc = nil
  83. sys.taskInit(function()
  84. -- 等待联网
  85. local ret, device_id = sys.waitUntil("IP_READY")
  86. -- 下面的是mqtt的参数均可自行修改
  87. client_id = device_id
  88. pub_topic = "/luatos/pub/" .. device_id
  89. sub_topic = "/luatos/sub/" .. device_id
  90. -- 打印一下上报(pub)和下发(sub)的topic名称
  91. -- 上报: 设备 ---> 服务器
  92. -- 下发: 设备 <--- 服务器
  93. -- 可使用mqtt.x等客户端进行调试
  94. log.info("mqtt", "pub", pub_topic)
  95. log.info("mqtt", "sub", sub_topic)
  96. -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
  97. -- if crypto.cipher_suites then
  98. -- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  99. -- end
  100. if mqtt == nil then
  101. while 1 do
  102. sys.wait(1000)
  103. log.info("bsp", "本bsp未适配mqtt库, 请查证")
  104. end
  105. end
  106. -------------------------------------
  107. -------- MQTT 演示代码 --------------
  108. -------------------------------------
  109. -- socket.dft(socket.LWIP_ETH)
  110. mqttc = mqtt.create(socket.LWIP_ETH, mqtt_host, mqtt_port, mqtt_isssl)
  111. mqttc:auth(client_id,user_name,password) -- client_id必填,其余选填
  112. -- mqttc:keepalive(240) -- 默认值240s
  113. mqttc:autoreconn(true, 3000) -- 自动重连机制
  114. mqttc:on(function(mqtt_client, event, data, payload)
  115. -- 用户自定义代码
  116. log.info("mqtt", "event", event, mqtt_client, data, payload)
  117. if event == "conack" then
  118. -- 联上了
  119. sys.publish("mqtt_conack")
  120. mqtt_client:subscribe(sub_topic)--单主题订阅
  121. -- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
  122. elseif event == "recv" then
  123. log.info("mqtt", "downlink", "topic", data, "payload", payload)
  124. sys.publish("mqtt_payload", data, payload)
  125. elseif event == "sent" then
  126. -- log.info("mqtt", "sent", "pkgid", data)
  127. -- elseif event == "disconnect" then
  128. -- 非自动重连时,按需重启mqttc
  129. -- mqtt_client:connect()
  130. end
  131. end)
  132. -- mqttc自动处理重连, 除非自行关闭
  133. mqttc:connect()
  134. sys.waitUntil("mqtt_conack")
  135. while true do
  136. -- 演示等待其他task发送过来的上报信息
  137. local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
  138. if ret then
  139. -- 提供关闭本while循环的途径, 不需要可以注释掉
  140. if topic == "close" then break end
  141. mqttc:publish(topic, data, qos)
  142. end
  143. -- 如果没有其他task上报, 可以写个空等待
  144. --sys.wait(60000000)
  145. end
  146. mqttc:close()
  147. mqttc = nil
  148. end)