main.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "httpdemo"
  3. VERSION = "1.0.0"
  4. --[[
  5. 本demo需要http库, 大部分能联网的设备都具有这个库
  6. http也是内置库, 无需require
  7. ]]
  8. -- sys库是标配
  9. _G.sys = require("sys")
  10. --[[特别注意, 使用http库需要下列语句]]
  11. _G.sysplus = require("sysplus")
  12. ---- MultipartForm上传文件
  13. -- url string 请求URL地址
  14. -- req_headers table 请求头
  15. -- params table 需要传输的数据参数
  16. local function postMultipartFormData(url, params)
  17. local boundary = "----WebKitFormBoundary"..os.time()
  18. local req_headers = {
  19. ["Content-Type"] = "multipart/form-data; boundary="..boundary,
  20. }
  21. local body = {}
  22. -- 解析拼接 body
  23. for k,v in pairs(params) do
  24. if k=="texts" then
  25. local bodyText = ""
  26. for kk,vv in pairs(v) do
  27. print(kk,vv)
  28. bodyText = bodyText.."--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"\r\n\r\n"..vv.."\r\n"
  29. end
  30. table.insert(body, bodyText)
  31. elseif k=="files" then
  32. local contentType =
  33. {
  34. txt = "text/plain", -- 文本
  35. jpg = "image/jpeg", -- JPG 格式图片
  36. jpeg = "image/jpeg", -- JPEG 格式图片
  37. png = "image/png", -- PNG 格式图片
  38. gif = "image/gif", -- GIF 格式图片
  39. html = "image/html", -- HTML
  40. json = "application/json" -- JSON
  41. }
  42. for kk,vv in pairs(v) do
  43. if type(vv) == "table" then
  44. for i=1, #vv do
  45. print(kk,vv[i])
  46. table.insert(body, "--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"; filename=\""..vv[i]:match("[^%/]+%w$").."\"\r\nContent-Type: "..contentType[vv[i]:match("%.(%w+)$")].."\r\n\r\n")
  47. table.insert(body, io.readFile(vv[i]))
  48. table.insert(body, "\r\n")
  49. end
  50. else
  51. print(kk,vv)
  52. table.insert(body, "--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"; filename=\""..vv:match("[^%/]+%w$").."\"\r\nContent-Type: "..contentType[vv:match("%.(%w+)$")].."\r\n\r\n")
  53. table.insert(body, io.readFile(vv))
  54. table.insert(body, "\r\n")
  55. end
  56. end
  57. end
  58. end
  59. table.insert(body, "--"..boundary.."--\r\n")
  60. body = table.concat(body)
  61. log.info("headers: ", "\r\n" .. json.encode(req_headers), type(body))
  62. log.info("body: " .. body:len() .. "\r\n" .. body)
  63. local code, headers, body = http.request("POST",url,
  64. req_headers,
  65. body
  66. ).wait()
  67. log.info("http.post", code, headers, body)
  68. end
  69. sys.taskInit(function()
  70. -----------------------------
  71. -- 统一联网函数, 可自行删减
  72. ----------------------------
  73. if rtos.bsp():startsWith("ESP32") then
  74. -- wifi 联网, ESP32系列均支持
  75. local ssid = "uiot"
  76. local password = "12345678"
  77. log.info("wifi", ssid, password)
  78. -- TODO 改成esptouch配网
  79. LED = gpio.setup(12, 0, gpio.PULLUP)
  80. wlan.init()
  81. wlan.setMode(wlan.STATION)
  82. wlan.connect(ssid, password, 1)
  83. local result, data = sys.waitUntil("IP_READY", 30000)
  84. log.info("wlan", "IP_READY", result, data)
  85. device_id = wlan.getMac()
  86. elseif rtos.bsp() == "AIR105" then
  87. -- w5500 以太网, 当前仅Air105支持
  88. w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
  89. w5500.config() --默认是DHCP模式
  90. w5500.bind(socket.ETH0)
  91. LED = gpio.setup(62, 0, gpio.PULLUP)
  92. sys.wait(1000)
  93. -- TODO 获取mac地址作为device_id
  94. elseif rtos.bsp() == "EC618" then
  95. -- Air780E/Air600E系列
  96. --mobile.simid(2)
  97. LED = gpio.setup(27, 0, gpio.PULLUP)
  98. device_id = mobile.imei()
  99. log.info("ipv6", mobile.ipv6(true))
  100. sys.waitUntil("IP_READY", 30000)
  101. end
  102. -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
  103. if crypto.cipher_suites then
  104. log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  105. end
  106. -------------------------------------
  107. -------- HTTP 演示代码 --------------
  108. -------------------------------------
  109. while 1 do
  110. -- 最普通的Http GET请求
  111. -- local code, headers, body = http.request("GET", "https://www.air32.cn/").wait()
  112. -- log.info("http.get", code, headers, body)
  113. -- local code, headers, body = http.request("GET", "https://mirrors6.tuna.tsinghua.edu.cn/", nil, nil, {ipv6=true}).wait()
  114. -- log.info("http.get", code, headers, body)
  115. -- sys.wait(100)
  116. -- local code, headers, body = http.request("GET", "https://www.luatos.com/").wait()
  117. -- log.info("http.get", code, headers, body)
  118. -- sys.wait(100)
  119. -- 按需打印
  120. -- code 响应值, 若大于等于 100 为服务器响应, 小于的均为错误代码
  121. -- headers是个table, 一般作为调试数据存在
  122. -- body是字符串. 注意lua的字符串是带长度的byte[]/char*, 是可以包含不可见字符的
  123. -- log.info("http", code, json.encode(headers or {}), #body > 512 and #body or body)
  124. -- -- POST request 演示
  125. -- local req_headers = {}
  126. -- req_headers["Content-Type"] = "application/json"
  127. -- local body = json.encode({name="LuatOS"})
  128. -- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date",
  129. -- req_headers,
  130. -- body -- POST请求所需要的body, string, zbuff, file均可
  131. -- ).wait()
  132. -- log.info("http.post", code, headers, body)
  133. -- -- POST multipart/form-data模式 上传文件---手动拼接
  134. local boundary = "----WebKitFormBoundary"..os.time()
  135. local req_headers = {
  136. ["Content-Type"] = "multipart/form-data; boundary="..boundary,
  137. }
  138. local body = "--"..boundary.."\r\n"..
  139. "Content-Disposition: form-data; name=\"uploadFile\"; filename=\"luatos_uploadFile_TEST01.txt\""..
  140. "\r\nContent-Type: text/plain\r\n\r\n"..
  141. "1111http_测试一二三四654zacc\r\n"..
  142. "--"..boundary
  143. log.info("headers: ", "\r\n"..json.encode(req_headers))
  144. log.info("body: ", "\r\n"..body)
  145. local code, headers, body = http.request("POST","http://airtest.openluat.com:2900/uploadFileToStatic",
  146. req_headers,
  147. body -- POST请求所需要的body, string, zbuff, file均可
  148. ).wait()
  149. log.info("http.post", code, headers, body)
  150. -- 也可用postMultipartFormData(url, params) 上传文件
  151. postMultipartFormData(
  152. "http://airtest.openluat.com:2900/uploadFileToStatic",
  153. {
  154. -- texts =
  155. -- {
  156. -- ["imei"] = "862991234567890",
  157. -- ["time"] = "20180802180345"
  158. -- },
  159. files =
  160. {
  161. ["uploadFile"] = "/luadb/luatos_uploadFile.txt",
  162. }
  163. }
  164. )
  165. -- -- POST and download, task内的同步操作
  166. -- local opts = {} -- 额外的配置项
  167. -- opts["dst"] = "/data.bin" -- 下载路径,可选
  168. -- opts["timeout"] = 30 -- 超时时长,单位秒,可选
  169. -- opts["adapter"] = socket.ETH0 -- 使用哪个网卡,可选
  170. -- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date",
  171. -- {}, -- 请求所添加的 headers, 可以是nil
  172. -- "",
  173. -- opts
  174. -- ).wait()
  175. -- log.info("http.post", code, headers, body) -- 只返回code和headers
  176. -- local f = io.open("/data.bin", "rb")
  177. -- if f then
  178. -- local data = f:read("*a")
  179. -- log.info("fs", "data", data, data:toHex())
  180. -- end
  181. -- -- GET request, 开个task让它自行执行去吧, 不管执行结果了
  182. -- sys.taskInit(http.request("GET","http://site0.cn/api/httptest/simple/time").wait)
  183. log.info("sys", rtos.meminfo("sys"))
  184. log.info("lua", rtos.meminfo("lua"))
  185. sys.wait(600000)
  186. end
  187. end)
  188. -- 用户代码已结束---------------------------------------------
  189. -- 结尾总是这一句
  190. sys.run()
  191. -- sys.run()之后后面不要加任何语句!!!!!