diff --git a/test/001.helloworld/main.lua b/test/001.helloworld/main.lua index 485778d..1e29d7c 100644 --- a/test/001.helloworld/main.lua +++ b/test/001.helloworld/main.lua @@ -1,14 +1,23 @@ _G.sys = require("sys") -print('Go') +-- print('Go') -sys.timerStart(function() - log.info("timer", "timeout once") -end, 1000) +-- sys.timerStart(function() +-- log.info("timer", "timeout once") +-- end, 1000) -sys.timerLoopStart(function() - log.info("timer", "3s repeat") -end, 3000) +-- sys.timerLoopStart(function() +-- log.info("timer", "3s repeat") +-- end, 3000) + +sys.taskInit(function() + while 1 do + sys.wait(1000) + log.info("lua", rtos.meminfo()) + log.info("sys", rtos.meminfo("sys")) + end + +end) sys.run() diff --git a/test/002.tcp/main.lua b/test/002.tcp/main.lua index 26056d4..0d418d1 100644 --- a/test/002.tcp/main.lua +++ b/test/002.tcp/main.lua @@ -1,6 +1,8 @@ _G.sys = require("sys") require "sysplus" +log.info("socket.ip", socket.localIP()) + sys.taskInit(function() sys.wait(100) while 1 do @@ -23,7 +25,7 @@ sys.taskInit(function() log.info("netc", netc) socket.config(netc) socket.debug(netc, true) - socket.connect(netc, "112.125.89.8", 46631) + socket.connect(netc, "112.125.89.8", 41506) while running do sys.wait(100) diff --git a/test/004.http/gzip b/test/004.http/gzip new file mode 100644 index 0000000..21304c3 Binary files /dev/null and b/test/004.http/gzip differ diff --git a/test/004.http/main.lua b/test/004.http/main.lua index 2d0089b..bb1e6c0 100644 --- a/test/004.http/main.lua +++ b/test/004.http/main.lua @@ -1,16 +1,53 @@ +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "httpdemo" +VERSION = "1.0.0" + _G.sys = require("sys") require "sysplus" sys.taskInit(function() - sys.wait(100) - local code, headers, body = http.request("GET", "https://www.air32.cn").wait() - log.info("http", code, json.encode(headers), body) + sys.waitUntil("IP_READY") + -- local code, headers, body = http.request("GET", "https://www.air32.cn").wait() + -- log.info("http", code, json.encode(headers), body) + local code, headers, body = http.request("POST", "http://ql.betterforyou.com.cn:9090/ck/app/appUpgrade/findMiniUpgrade?equId=QL10000001&equType=1").wait() + log.info("http.get", code, headers, body) + log.info("GoGoGo") sys.wait(1000) - local code, headers, body = http.request("GET", "https://air32.cn").wait() - log.info("http", code, json.encode(headers), body) + local body = io.readFile("/luadb/wifi.json") + local code, headers, body = http.request("POST", "http://wifi.air32.cn/wifi", nil, body).wait() + log.info("http", code, json.encode(headers), #body) + -- log.info("http", "body", #body) + -- log.info("http", "body", body) + + socket.sslLog(5) + local httpplus = require "httpplus" + -- local code, resp = httpplus.request({url="https://vaviri-back-ph4lj.ondigitalocean.app/state"}) + local code, resp = httpplus.request({ + url="http://wifi.air32.cn/wifi", + body=io.readFile("/luadb/wifi.json"), + headers={"Content-Type", "application/json"}, + method="POST"} + ) + local body = resp.body:query() + -- log.info("http", code, json.encode(resp.headers), #body) + log.info("http", "body", #body) + log.info("http", "body", body) + log.info("http", "body", body:toHex()) + + -- body = io.readFile("/luadb/gzip") + -- log.info("gzip", #body) + -- -- log.info("http", miniz.uncompress(body:sub(11))) + -- log.info("http", miniz.uncompress(body:sub(11), 0)) + -- log.info("http", body:toHex()) + + -- local code, headers, body = http.request("GET", "http://quan.suning.com/getSysTime.do", {Accept="*/*", ["User-Agent"]="ABC"}, nil, {debug=true}) + -- log.info("http", code, json.encode(headers), body) + + -- local code, resp = httpplus.request({url="http://quan.suning.com/getSysTime.do"}) + -- log.info("http", code, resp.body:query()) end) sys.run() diff --git a/test/004.http/wifi.json b/test/004.http/wifi.json new file mode 100644 index 0000000..2152408 --- /dev/null +++ b/test/004.http/wifi.json @@ -0,0 +1 @@ +{"imei":"861551056435449","wifis":[{"mac":"6EB158564935","rssi":-46},{"mac":"6CB158A64935","rssi":-47},{"mac":"88C397CE0E89","rssi":-62},{"mac":"8EC397CE0E89","rssi":-63},{"mac":"A439B3D208A2","rssi":-68},{"mac":"AA39B3D208A2","rssi":-70},{"mac":"50D2F516AA9D","rssi":-76},{"mac":"8CDEF97FEB89","rssi":-78},{"mac":"345BBBF2D112","rssi":-78},{"mac":"6C06D6080B30","rssi":-82},{"mac":"92DEF97FEB89","rssi":-86},{"mac":"3446ECF3AEE1","rssi":-86},{"mac":"0C31DCC75D34","rssi":-86},{"mac":"3446ECD3AEDC","rssi":-87},{"mac":"1475905ABE04","rssi":-89},{"mac":"143CC39C61CC","rssi":-89},{"mac":"3095E361696D","rssi":-91},{"mac":"CC08FB75FE85","rssi":-91},{"mac":"3295E311696D","rssi":-92},{"mac":"A29D7E7E6223","rssi":-93}],"iccid":"89860323750207458090","unique_id":"3238363435061A4C4330","key":"freedemo"} \ No newline at end of file diff --git a/test/005.mqtt/main.lua b/test/005.mqtt/main.lua index c652f45..2ff3200 100644 --- a/test/005.mqtt/main.lua +++ b/test/005.mqtt/main.lua @@ -24,12 +24,12 @@ end -- pm.request(pm.LIGHT) --根据自己的服务器修改以下参数 -local mqtt_host = "lbsmqtt.airm2m.com" -local mqtt_port = 1884 +local mqtt_host = "124.223.67.31" +local mqtt_port = 1883 local mqtt_isssl = false -local client_id = "abc" -local user_name = "user" -local password = "password" +local client_id = "124" +local user_name = "1" +local password = "124" local pub_topic = "/luatos/pub/" .. "pc" local sub_topic = "/luatos/sub/" .. "pc" diff --git a/test/009.ftp/main.lua b/test/009.ftp/main.lua new file mode 100644 index 0000000..da01fe9 --- /dev/null +++ b/test/009.ftp/main.lua @@ -0,0 +1,101 @@ + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "ftpdemo" +VERSION = "1.0.0" + +--[[ +本demo需要ftp库, 大部分能联网的设备都具有这个库 +ftp也是内置库, 无需require +]] + +-- sys库是标配 +_G.sys = require("sys") +--[[特别注意, 使用ftp库需要下列语句]] +_G.sysplus = require("sysplus") + +sys.taskInit(function() + ----------------------------- + -- 统一联网函数, 可自行删减 + ---------------------------- + if wlan and wlan.connect then + -- wifi 联网, ESP32系列均支持 + local ssid = "luatos1234" + local password = "12341234" + log.info("wifi", ssid, password) + -- TODO 改成esptouch配网 + -- LED = gpio.setup(12, 0, gpio.PULLUP) + wlan.init() + wlan.setMode(wlan.STATION) + wlan.connect(ssid, password, 1) + local result, data = sys.waitUntil("IP_READY", 30000) + log.info("wlan", "IP_READY", result, data) + device_id = wlan.getMac() + elseif rtos.bsp() == "AIR105" then + -- w5500 以太网, 当前仅Air105支持 + -- w5500.init(spi.SPI_2, 24000000, pin.PB03, pin.PC00, pin.PC03) + w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00) + log.info("auto mac", w5500.getMac():toHex()) + w5500.config() --默认是DHCP模式 + w5500.bind(socket.ETH0) + -- LED = gpio.setup(62, 0, gpio.PULLUP) + sys.wait(1000) + -- TODO 获取mac地址作为device_id + elseif mobile then + -- Air780E/Air600E系列 + --mobile.simid(2) + -- LED = gpio.setup(27, 0, gpio.PULLUP) + device_id = mobile.imei() + sys.waitUntil("IP_READY", 30000) + end + + -- -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件 + -- if crypto.cipher_suites then + -- log.info("cipher", "suites", json.encode(crypto.cipher_suites())) + -- end + while true do + sys.wait(1000) + log.info("ftp 启动") + print(ftp.login(nil,"121.43.224.154",21,"ftp_user","3QujbiMG").wait()) + + print(ftp.command("NOOP").wait()) + print(ftp.command("SYST").wait()) + + print(ftp.command("TYPE I").wait()) + print(ftp.command("PWD").wait()) + print(ftp.command("MKD QWER").wait()) + print(ftp.command("CWD /QWER").wait()) + + print(ftp.command("CDUP").wait()) + print(ftp.command("RMD QWER").wait()) + + print(ftp.command("LIST").wait()) + + -- io.writeFile("/1222.txt", "23noianfdiasfhnpqw39fhawe;fuibnnpw3fheaios;fna;osfhisao;fadsfl") + -- print(ftp.push("/1222.txt","/12222.txt").wait()) + + print(ftp.pull("/122224.txt","/122224.txt").wait()) + + local f = io.open("/122224.txt", "r") + if f then + local data = f:read("*a") + f:close() + log.info("fs", "writed data", data) + else + log.info("fs", "open file for read failed") + end + + print(ftp.command("DELE /12222.txt").wait()) + print(ftp.push("/122224.txt","/12222.txt").wait()) + print(ftp.close().wait()) + log.info("meminfo", rtos.meminfo("sys")) + sys.wait(15000) + end + + +end) + + +-- 用户代码已结束--------------------------------------------- +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!! diff --git a/test/011.onenet_coap/main.lua b/test/011.onenet_coap/main.lua index a5ed460..f607117 100644 --- a/test/011.onenet_coap/main.lua +++ b/test/011.onenet_coap/main.lua @@ -10,11 +10,11 @@ require "sysplus" udp_host = "183.230.102.122" udp_port = 5683 -- 设备信息 -produt_id = "SJaLt5cVL2" +product_id = "SJaLt5cVL2" device_name = "luatospc" device_key = "dUZVVWRIcjVsV2pSbTJsckd0TmgyRXNnMTJWMXhIMkk=" -_, _, main_token = iotauth.onenet(produt_id,device_name,device_key,"sha1") +_, _, main_token = iotauth.onenet(product_id,device_name,device_key,"sha1") -- UDP事件处理函数 local rxbuff = zbuff.create(1500) @@ -46,8 +46,9 @@ function udpcb(sc, event) elseif event == socket.ON_LINE then log.info("udp", "UDP已准备就绪,可以上行") -- 上行登陆包 - local data = ercoap.onenet("login", produt_id, device_name, main_token) - -- log.info("上行登陆包", data:toHex()) + log.info("登陆参数", product_id, device_name, main_token) + local data = ercoap.onenet("login", product_id, device_name, main_token) + log.info("上行登陆包", data:toHex()) socket.tx(sc, data) else log.info("udp", "其他事件", event) @@ -72,8 +73,8 @@ sys.taskInit(function() break end -- 上行心跳包 - -- onenet_coap_auth(string.format("$sys/%s/%s/keep_alive", produt_id, device_name)) - local data = ercoap.onenet("keep_alive", produt_id, device_name, main_token) + -- onenet_coap_auth(string.format("$sys/%s/%s/keep_alive", product_id, device_name)) + local data = ercoap.onenet("keep_alive", product_id, device_name, main_token) -- log.info("上行心跳包", data:toHex()) socket.tx(netc, data) @@ -94,7 +95,8 @@ sys.taskInit(function() log.info("uplink", jdata) -- jdata = [[{"id":"3","version":"1.0","params":{"WaterMeterState":{"value":0}}}]] -- log.info("uplink2", jdata) - local data = ercoap.onenet("thing/property/post", produt_id, device_name, post_token, jdata) + log.info("uplink", "thing/property/post", product_id, device_name, post_token:toHex(), jdata) + local data = ercoap.onenet("thing/property/post", product_id, device_name, post_token, jdata) -- log.info("onenet", "上行物模型数据", data:toHex()) socket.tx(netc, data) end diff --git a/test/012.sqlite3/main.lua b/test/012.sqlite3/main.lua index b7132b8..d4c28e8 100644 --- a/test/012.sqlite3/main.lua +++ b/test/012.sqlite3/main.lua @@ -3,14 +3,14 @@ _G.sys = require("sys") sys.taskInit(function() sys.wait(1000) - local db = sqlite3.open("/ram/test.db") + local db = sqlite3.open("test.db") log.info("sqlite3", db) if db then - sqlite3.exec(db, "CREATE TABLE devs(ID INT PRIMARY KEY NOT NULL, name CHAR(50));") - sqlite3.exec(db, "insert into devs values(1, \"ABC\");") - sqlite3.exec(db, "insert into devs values(2, \"DEF\");") - sqlite3.exec(db, "insert into devs values(3, \"HIJ\");") - local ret, data = sqlite3.exec(db, "select * from devs;") + sqlite3.exec(db, "CREATE TABLE devs(ID INT PRIMARY KEY NOT NULL, name CHAR(50))") + sqlite3.exec(db, "insert into devs values(1, \"ABC\")") + sqlite3.exec(db, "insert into devs values(2, \"DEF\")") + sqlite3.exec(db, "insert into devs values(3, \"HIJ\")") + local ret, data = sqlite3.exec(db, "select * from devs where id > 1 order by id desc limit 2") log.info("查询结果", ret, data) if ret then for k, v in pairs(data) do diff --git a/test/013.libgnss/main.lua b/test/013.libgnss/main.lua new file mode 100644 index 0000000..b461f37 --- /dev/null +++ b/test/013.libgnss/main.lua @@ -0,0 +1,41 @@ + +_G.sys = require("sys") + +PROJECT = "logtest" +VERSION = "1.0.0" + +sys.taskInit(function() + local tm = { + year = 2023, + month = 11, + mon = 11, + day = 22, + hour = 15, + min = 30, + sec = 44 + } + local lla = { + lat = "113.5", + lng = "022.5" + } + log.info(">>", json.encode(os.date("!*t"))) + local aid = libgnss.casic_aid(tm, lla) + log.info("AID", aid:toHex()) + -- BACE38000B010000000000605C400000000000803640000000000000000000000000D43A1341 + -- 0000000000000000000000000000000000000000F108 00 23 FD 23 B1 E5 + -- BACE38000B010000000000605C400000000000803640000000000000000000000000D43A1341 + -- 0000000000000000000000000000000060FD0601F108 00 23 5D 21 B8 E6 + -- BACE38000B010000000000605C400000000000803640000000000000000000000000D43A1341 + -- 0000000000000000000000000000000000000000F108 00 23 FD 23 B1 E5 + + local str = "$GNRMC,000625.00,A,3557.35652,N,13854.27058,E,1.681,73.98,111223,,,A,V*32\r\n" + -- libgnss.init() + libgnss.parse(str) + log.info("GNSS", libgnss.getIntLocation()) + log.info("GNSS", 1.681 * 1852) + log.info("GNSS", libgnss.getIntLocation(1)) + log.info("GNSS", libgnss.getIntLocation(2)) + log.info("GNSS", libgnss.getIntLocation(3)) +end) + +sys.run() diff --git a/test/014.ntp/main.lua b/test/014.ntp/main.lua new file mode 100644 index 0000000..effa64a --- /dev/null +++ b/test/014.ntp/main.lua @@ -0,0 +1,16 @@ + +_G.sys = require("sys") + +sys.taskInit(function() + sys.waitUntil("IP_READY") + while 1 do + socket.sntp() + sys.waitUntil("NTP_UPDATE", 10000) + local tm = socket.ntptm() + log.info("tm数据", json.encode(tm)) + log.info("时间戳", string.format("%u.%03d", tm.tsec, tm.tms)) + sys.wait(5000) + end +end) + +sys.run() diff --git a/test/015.lvgl_switch_event/main.lua b/test/015.lvgl_switch_event/main.lua new file mode 100644 index 0000000..ca6d289 --- /dev/null +++ b/test/015.lvgl_switch_event/main.lua @@ -0,0 +1,23 @@ + +sys = require "sys" +log.info("lvgl", lvgl.init()) + +local function event_handler(obj, event) + log.info("event", event) +end + +sys.taskInit(function() + local sw1 = lvgl.switch_create(lvgl.scr_act(), nil); + lvgl.obj_align(sw1, nil, lvgl.ALIGN_CENTER, 0, -50); + lvgl.obj_set_event_cb(sw1, event_handler); + + while 1 do + sys.wait(1000) + lvgl.switch_on(sw1, lvgl.ANIM_ON) + sys.wait(1000) + lvgl.switch_off(sw1, lvgl.ANIM_OFF) + end +end) + +sys.run() + diff --git a/test/016.lvgl_chinese/main.lua b/test/016.lvgl_chinese/main.lua new file mode 100644 index 0000000..d77110b --- /dev/null +++ b/test/016.lvgl_chinese/main.lua @@ -0,0 +1,29 @@ + +sys = require "sys" +log.info("lvgl", lvgl.init()) + +local function event_handler(obj, event) + log.info("event", event) +end + +sys.taskInit(function() + local scr = lvgl.obj_create(nil, nil) + local btn = lvgl.btn_create(scr) + local btn2 = lvgl.btn_create(scr) + lvgl.obj_align(btn, lvgl.scr_act(), lvgl.ALIGN_CENTER, 0, 0) + lvgl.obj_align(btn2, lvgl.scr_act(), lvgl.ALIGN_CENTER, 0, 50) + local label = lvgl.label_create(btn) + local label2 = lvgl.label_create(btn2) + lvgl.label_set_text(label, "LuatOS!") + lvgl.label_set_text(label2, "共和国") + local font = lvgl.font_get("opposans_m_10") + lvgl.obj_set_style_local_text_font(label2, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, font); + + lvgl.scr_load(scr) + + sys.wait(1000) + -- lvgl.obj_set_style_local_text_font(label2, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, lvgl.font_get("opposans_m_102")); +end) + +sys.run() + diff --git a/test/017.libgnss_hour0/main.lua b/test/017.libgnss_hour0/main.lua new file mode 100644 index 0000000..001c233 --- /dev/null +++ b/test/017.libgnss_hour0/main.lua @@ -0,0 +1,11 @@ + +sys = require "sys" + +sys.taskInit(function() + local str = "$GNRMC,005814.000,V,,,,,,,251123,,,M,V*24\r\n" + libgnss.parse(str) + log.info("rmc", json.encode(libgnss.getRmc(), "7f")) +end) + +sys.run() + diff --git a/test/018.sntp/main.lua b/test/018.sntp/main.lua new file mode 100644 index 0000000..3350f68 --- /dev/null +++ b/test/018.sntp/main.lua @@ -0,0 +1,18 @@ + +_G.sys = require("sys") + +sys.taskInit(function() + sys.waitUntil("IP_READY") + while 1 do + sys.wait(1000) + socket.sntp({ + "ntp.aliyun.com", + "ntp2.aliyun.com", + "ntp3.aliyun.com", + "ntp4.aliyun.com", + }) + end + +end) + +sys.run() diff --git a/test/019.httplong/main.lua b/test/019.httplong/main.lua new file mode 100644 index 0000000..9fc14c3 --- /dev/null +++ b/test/019.httplong/main.lua @@ -0,0 +1,288 @@ + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "httpdemo" +VERSION = "1.0.0" + +--[[ +本demo需要http库, 大部分能联网的设备都具有这个库 +http也是内置库, 无需require +]] + +-- sys库是标配 +_G.sys = require("sys") +--[[特别注意, 使用http库需要下列语句]] +_G.sysplus = require("sysplus") + + +-- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦 +if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then + pm.power(pm.PWK_MODE, false) +end + + +sys.taskInit(function() + ----------------------------- + -- 统一联网函数, 可自行删减 + ---------------------------- + if wlan and wlan.connect then + -- wifi 联网, ESP32系列均支持 + local ssid = "HJ" + local password = "HJ@88888" + log.info("wifi", ssid, password) + -- TODO 改成esptouch配网 + -- LED = gpio.setup(12, 0, gpio.PULLUP) + wlan.init() + wlan.setMode(wlan.STATION) + wlan.connect(ssid, password, 1) + local result, data = sys.waitUntil("IP_READY", 30000) + log.info("wlan", "IP_READY", result, data) + device_id = wlan.getMac() + elseif rtos.bsp() == "AIR105" then + -- w5500 以太网, 当前仅Air105支持 + w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00) + w5500.config() --默认是DHCP模式 + w5500.bind(socket.ETH0) + -- LED = gpio.setup(62, 0, gpio.PULLUP) + sys.wait(1000) + -- TODO 获取mac地址作为device_id + elseif mobile then + -- Air780E/Air600E系列 + --mobile.simid(2) + -- LED = gpio.setup(27, 0, gpio.PULLUP) + device_id = mobile.imei() + log.info("ipv6", mobile.ipv6(true)) + sys.waitUntil("IP_READY", 30000) + elseif http then + sys.waitUntil("IP_READY") + else + while 1 do + sys.wait(1000) + log.info("http", "当前固件未包含http库") + end + end + log.info("已联网") + sys.publish("net_ready") +end) + +function demo_http_get() + -- 最普通的Http GET请求 + local code, headers, body = http.request("POST", "http://ql.betterforyou.com.cn:9090/ck/app/appUpgrade/findMiniUpgrade?equId=QL10000001&equType=1").wait() + log.info("http.get", code, headers, body) + -- local code, headers, body = http.request("GET", "https://mirrors6.tuna.tsinghua.edu.cn/", nil, nil, {ipv6=true}).wait() + -- log.info("http.get", code, headers, body) + -- sys.wait(100) + -- local code, headers, body = http.request("GET", "https://www.luatos.com/").wait() + -- log.info("http.get", code, headers, body) + + -- 按需打印 + -- code 响应值, 若大于等于 100 为服务器响应, 小于的均为错误代码 + -- headers是个table, 一般作为调试数据存在 + -- body是字符串. 注意lua的字符串是带长度的byte[]/char*, 是可以包含不可见字符的 + -- log.info("http", code, json.encode(headers or {}), #body > 512 and #body or body) +end + +-- function demo_http_post_json() +-- -- POST request 演示 +-- local req_headers = {} +-- req_headers["Content-Type"] = "application/json" +-- local body = json.encode({equId="QL10000001",equType="1"}) +-- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", +-- req_headers, +-- body -- POST请求所需要的body, string, zbuff, file均可 +-- ).wait() +-- log.info("http.post", code, headers, body) +-- end + +-- function demo_http_post_form() +-- -- POST request 演示 +-- local req_headers = {} +-- req_headers["Content-Type"] = "application/x-www-form-urlencoded" +-- local params = { +-- ABC = "123", +-- DEF = 345 +-- } +-- local body = "" +-- for k, v in pairs(params) do +-- body = body .. tostring(k) .. "=" .. tostring(v):urlEncode() .. "&" +-- end +-- local code, headers, body = http.request("POST","http://echohttp.wendal.cn/post", +-- req_headers, +-- body -- POST请求所需要的body, string, zbuff, file均可 +-- ).wait() +-- log.info("http.post.form", code, headers, body) +-- end + +-- -- local function http_download_callback(content_len,body_len,userdata) +-- -- print("http_download_callback",content_len,body_len,userdata) +-- -- end + +-- -- local http_userdata = "123456789" + +-- function demo_http_download() + +-- -- POST and download, task内的同步操作 +-- local opts = {} -- 额外的配置项 +-- opts["dst"] = "/data.bin" -- 下载路径,可选 +-- opts["timeout"] = 30000 -- 超时时长,单位ms,可选 +-- -- opts["adapter"] = socket.ETH0 -- 使用哪个网卡,可选 +-- -- opts["callback"] = http_download_callback +-- -- opts["userdata"] = http_userdata + +-- for k, v in pairs(opts) do +-- print("opts",k,v) +-- end + +-- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", +-- {}, -- 请求所添加的 headers, 可以是nil +-- "", +-- opts +-- ).wait() +-- log.info("http.post", code, headers, body) -- 只返回code和headers + +-- -- local f = io.open("/data.bin", "rb") +-- -- if f then +-- -- local data = f:read("*a") +-- -- log.info("fs", "data", data, data:toHex()) +-- -- end + +-- -- GET request, 开个task让它自行执行去吧, 不管执行结果了 +-- sys.taskInit(http.request("GET","http://site0.cn/api/httptest/simple/time").wait) +-- end + +-- function demo_http_post_file() +-- -- -- POST multipart/form-data模式 上传文件---手动拼接 +-- local boundary = "----WebKitFormBoundary"..os.time() +-- local req_headers = { +-- ["Content-Type"] = "multipart/form-data; boundary="..boundary, +-- } +-- local body = "--"..boundary.."\r\n".. +-- "Content-Disposition: form-data; name=\"uploadFile\"; filename=\"luatos_uploadFile_TEST01.txt\"".. +-- "\r\nContent-Type: text/plain\r\n\r\n".. +-- "1111http_测试一二三四654zacc\r\n".. +-- "--"..boundary + +-- log.info("headers: ", "\r\n"..json.encode(req_headers)) +-- log.info("body: ", "\r\n"..body) +-- local code, headers, body = http.request("POST","http://airtest.openluat.com:2900/uploadFileToStatic", +-- req_headers, +-- body -- POST请求所需要的body, string, zbuff, file均可 +-- ).wait() +-- log.info("http.post", code, headers, body) + +-- -- 也可用postMultipartFormData(url, params) 上传文件 +-- postMultipartFormData( +-- "http://airtest.openluat.com:2900/uploadFileToStatic", +-- { +-- -- texts = +-- -- { +-- -- ["imei"] = "862991234567890", +-- -- ["time"] = "20180802180345" +-- -- }, + +-- files = +-- { +-- ["uploadFile"] = "/luadb/luatos_uploadFile.txt", +-- } +-- } +-- ) +-- end + + + +sys.taskInit(function() + sys.wait(100) + -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件 + -- if crypto.cipher_suites then + -- log.info("cipher", "suites", json.encode(crypto.cipher_suites())) + -- end + + ------------------------------------- + -------- HTTP 演示代码 -------------- + ------------------------------------- + sys.waitUntil("net_ready") -- 等联网 + + while 1 do + -- 演示GET请求 + demo_http_get() + -- 表单提交 + -- demo_http_post_form() + -- POST一个json字符串 + -- demo_http_post_json() + -- 上传文件, mulitform形式 + -- demo_http_post_file() + -- 文件下载 + -- demo_http_download() + + sys.wait(1000) + -- 打印一下内存状态 + log.info("sys", rtos.meminfo("sys")) + log.info("lua", rtos.meminfo("lua")) + sys.wait(6000) + end +end) + +---- MultipartForm上传文件 +-- url string 请求URL地址 +-- req_headers table 请求头 +-- params table 需要传输的数据参数 +-- function postMultipartFormData(url, params) +-- local boundary = "----WebKitFormBoundary"..os.time() +-- local req_headers = { +-- ["Content-Type"] = "multipart/form-data; boundary="..boundary, +-- } +-- local body = {} + +-- -- 解析拼接 body +-- for k,v in pairs(params) do +-- if k=="texts" then +-- local bodyText = "" +-- for kk,vv in pairs(v) do +-- print(kk,vv) +-- bodyText = bodyText.."--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"\r\n\r\n"..vv.."\r\n" +-- end +-- table.insert(body, bodyText) +-- elseif k=="files" then +-- local contentType = +-- { +-- txt = "text/plain", -- 文本 +-- jpg = "image/jpeg", -- JPG 格式图片 +-- jpeg = "image/jpeg", -- JPEG 格式图片 +-- png = "image/png", -- PNG 格式图片 +-- gif = "image/gif", -- GIF 格式图片 +-- html = "image/html", -- HTML +-- json = "application/json" -- JSON +-- } + +-- for kk,vv in pairs(v) do +-- if type(vv) == "table" then +-- for i=1, #vv do +-- print(kk,vv[i]) +-- 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") +-- table.insert(body, io.readFile(vv[i])) +-- table.insert(body, "\r\n") +-- end +-- else +-- print(kk,vv) +-- 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") +-- table.insert(body, io.readFile(vv)) +-- table.insert(body, "\r\n") +-- end +-- end +-- end +-- end +-- table.insert(body, "--"..boundary.."--\r\n") +-- body = table.concat(body) +-- log.info("headers: ", "\r\n" .. json.encode(req_headers), type(body)) +-- log.info("body: " .. body:len() .. "\r\n" .. body) +-- local code, headers, body = http.request("POST",url, +-- req_headers, +-- body +-- ).wait() +-- log.info("http.post", code, headers, body) +-- end + + +-- 用户代码已结束--------------------------------------------- +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!! diff --git a/test/020.u8g2_buff/main.lua b/test/020.u8g2_buff/main.lua new file mode 100644 index 0000000..4b655fe --- /dev/null +++ b/test/020.u8g2_buff/main.lua @@ -0,0 +1,33 @@ + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "httpdemo" +VERSION = "1.0.0" + +--[[ +本demo需要http库, 大部分能联网的设备都具有这个库 +http也是内置库, 无需require +]] + +-- sys库是标配 +_G.sys = require("sys") +--[[特别注意, 使用http库需要下列语句]] +_G.sysplus = require("sysplus") + + +sys.taskInit(function() + sys.wait(100) + u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_hw",i2c_id=1,i2c_speed = i2c.FAST}) + u8g2.DrawUTF8("U8g2+LuatOS", 4, 4) + u8g2.SendBuffer() + local len = u8g2.CopyBuffer() + log.info("缓冲区大小", len) + local buff = zbuff.create(len) + log.info("zbuff", buff) + u8g2.CopyBuffer(buff) + log.info("buff数据", buff:toStr(0, 256):toHex()) +end) + +-- 用户代码已结束--------------------------------------------- +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!! diff --git a/test/021.xxtea/main.lua b/test/021.xxtea/main.lua new file mode 100644 index 0000000..665762e --- /dev/null +++ b/test/021.xxtea/main.lua @@ -0,0 +1,32 @@ + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "httpdemo" +VERSION = "1.0.0" + +--[[ +本demo需要http库, 大部分能联网的设备都具有这个库 +http也是内置库, 无需require +]] + +-- sys库是标配 +_G.sys = require("sys") +--[[特别注意, 使用http库需要下列语句]] +_G.sysplus = require("sysplus") + + +sys.taskInit(function() + local text = "Hello World!" + local key = "07946" + local encrypt_data = xxtea.encrypt(text, key) + log.info("testCrypto.xxteaTest","xxtea_encrypt:", encrypt_data:toHex()) + + print("========================================================") + + local decrypt_data = xxtea.decrypt(encrypt_data, key) + log.info("testCrypto.xxteaTest","decrypt_data:", decrypt_data:toHex()) +end) + +-- 用户代码已结束--------------------------------------------- +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!!