-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "wifidemo"
VERSION = "1.0.0"

-- 引入必要的库文件(lua编写), 内部库不需要require
sys = require("sys")
require("sysplus")
-- sys.subscribe("WLAN_READY", function ()
--     print("!!! wlan ready event !!!")
-- end)

-- sys.taskInit(function()
--     while 1 do
--         sys.wait(5000)
--         log.info("lua", rtos.meminfo())
--         log.info("sys", rtos.meminfo("sys"))
--     end

-- end)

-- 兼容V1001固件的
if http == nil and http2 then
    http = http2
end

--用于html的unescape,等效于python的html.unescape
local gsub, char = string.gsub, string.char
local entityMap  = {["lt"]="<",["gt"]=">",["amp"]="&",["quot"]='"',["apos"]="'"}
local entitySwap = function(orig,n,s)
    return (n=='' and entityMap[s])
        or (n=="#" and tonumber(s)) and string.char(s)
        or (n=="#x" and tonumber(s,16)) and string.char(tonumber(s,16))
        or orig
end
function unescape(str)
    return (gsub( str, '(&(#?x?)([%d%a]+);)', entitySwap ))
end

sys.taskInit(function()
    sys.wait(1000)
    wlan.init()
    wlan.connect("502_Home_Network", "22-59-502")
    log.info("wlan", "wait for IP_READY")
    sys.waitUntil("IP_READY", 30000)
    if wlan.ready() then
        log.info("wlan", "ready !!")
        sys.wait(100)
        -- local url = "http://ip.nutz.cn/json"
        local url = "http://192.168.1.118:5080/web-notepad/msg"
        while true
        do
            local code, headers, body = http.request("GET", url).wait()
            bodyText=string.format("%q",body)
            str1="/textarea"
            a,b=string.find(bodyText,str1,1)
            log.info(unescape(string.sub(bodyText,556,a-2)))
            sys.wait(2000)
        end
    else
        print("wlan NOT ready!!!!")
    end
        
end)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

发表评论