main.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "httpdemo"
  3. VERSION = "1.0.0"
  4. --[[
  5. 本demo需要socket库, 大部分能联网的设备都具有这个库
  6. socket是内置库, 无需require
  7. ]]
  8. -- sys库是标配
  9. _G.sys = require("sys")
  10. httpplus = require "httpplus"
  11. -- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦
  12. if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then
  13. pm.power(pm.PWK_MODE, false)
  14. end
  15. sys.taskInit(function()
  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 改成esptouch配网
  25. -- LED = gpio.setup(12, 0, gpio.PULLUP)
  26. wlan.init()
  27. wlan.setMode(wlan.STATION)
  28. wlan.connect(ssid, password, 1)
  29. local result, data = sys.waitUntil("IP_READY", 30000)
  30. log.info("wlan", "IP_READY", result, data)
  31. device_id = wlan.getMac()
  32. elseif rtos.bsp() == "AIR105" then
  33. -- w5500 以太网, 当前仅Air105支持
  34. w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
  35. w5500.config() --默认是DHCP模式
  36. w5500.bind(socket.ETH0)
  37. -- LED = gpio.setup(62, 0, gpio.PULLUP)
  38. sys.wait(1000)
  39. -- TODO 获取mac地址作为device_id
  40. elseif mobile then
  41. -- Air780E/Air600E系列
  42. --mobile.simid(2)
  43. -- LED = gpio.setup(27, 0, gpio.PULLUP)
  44. device_id = mobile.imei()
  45. log.info("ipv6", mobile.ipv6(true))
  46. sys.waitUntil("IP_READY", 30000)
  47. elseif http then
  48. sys.waitUntil("IP_READY")
  49. else
  50. while 1 do
  51. sys.wait(1000)
  52. log.info("http", "当前固件未包含http库")
  53. end
  54. end
  55. log.info("已联网")
  56. sys.publish("net_ready")
  57. end)
  58. function test_httpplus()
  59. sys.waitUntil("net_ready")
  60. -- 调试开关
  61. httpplus.debug = true
  62. -- socket.sslLog(3)
  63. -- local code, resp =httpplus.request({method="POST", url="https://abc:qq@whoami.k8s.air32.cn/goupupup"})
  64. -- log.info("http", code, resp)
  65. -- 预期返回302
  66. -- local code, resp = httpplus.request({method="POST", url="https://air32.cn/goupupup"})
  67. -- log.info("http", code, resp)
  68. -- local code, resp = httpplus.request({method="POST", url="https://httpbin.air32.cn/post", files={abcd="/luadb/libfastlz.a"}})
  69. -- log.info("http", code, resp)
  70. -- local code, resp = httpplus.request({method="POST", url="https://httpbin.air32.cn/anything", forms={abcd="12345"}})
  71. -- log.info("http", code, resp)
  72. -- local code, resp = httpplus.request({method="POST", url="https://httpbin.air32.cn/post", files={abcd="/luadb/abc.txt"}})
  73. -- log.info("http", code, resp)
  74. -- 简单GET请求
  75. local code, resp = httpplus.request({url="https://httpbin.air32.cn/"})
  76. log.info("http", code, resp)
  77. -- 简单POST请求
  78. -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/post", body="123456", method="POST"})
  79. -- log.info("http", code, resp)
  80. -- 文件上传
  81. -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/post", files={myfile="/luadb/abc.txt"}})
  82. -- log.info("http", code, resp)
  83. -- 自定义header的GET请求
  84. -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/get", headers={Auth="12312234"}})
  85. -- log.info("http", code, resp)
  86. -- 带鉴权信息的GET请求
  87. -- local code, resp = httpplus.request({url="https://wendal:123@httpbin.air32.cn/get", headers={Auth="12312234"}})
  88. -- log.info("http", code, resp)
  89. -- PUT请求
  90. -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/put", method="PUT", body="123"})
  91. -- log.info("http", code, resp)
  92. -- 表单POST
  93. -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/post", forms={abc="123"}})
  94. -- log.info("http", code, resp)
  95. -- 响应体chucked编码测试
  96. local code, resp = httpplus.request({url="https://httpbin.air32.cn/stream/1"})
  97. log.info("http", code, resp)
  98. if code == 200 then
  99. log.info("http", "headers", json.encode(resp.headers))
  100. local body = resp.body:query()
  101. log.info("http", "body", body:toHex())
  102. log.info("http", "body", body)
  103. log.info("http", "body", json.decode(body:trim()))
  104. -- log.info("http", "body", json.decode(resp.body))
  105. end
  106. end
  107. sys.taskInit(test_httpplus)
  108. -- 用户代码已结束---------------------------------------------
  109. -- 结尾总是这一句
  110. sys.run()
  111. -- sys.run()之后后面不要加任何语句!!!!!