获取天气
enduser_setup.start( function() print("Connected to wifi as:" .. wifi.sta.getip()) end, function(err, str) print("enduser_setup: Err #" .. err .. ": " .. str) end ); local mytimer = tmr.create() mytimer:register(5000, 0, function() dofile("MQTT.lua"); end) mytimer:start(); led1 = 4 stat=1 temp="" city="" wea="" lasttime ="" host = "api.thinkpage.cn" --天气网站 gpio.mode(led1, gpio.OUTPUT) tmr.alarm(2, 1500, tmr.ALARM_AUTO,function() if stat==1 then gpio.write(led1, gpio.LOW); stat=0 elseif stat==0 then gpio.write(led1, gpio.HIGH); stat=1 end end )--设置led1=D4 1.5秒循环闪烁 tmr.alarm(3, 10000, tmr.ALARM_AUTO,function() --每10秒到网站获取数据 conn=net.createConnection(net.TCP, 0) conn:connect(80,"api.thinkpage.cn") conn:on("receive", function(conn, payload) --print(payload) if payload~=nil then i=string.find(payload,"name") j=string.find(payload,"country") end print("CITY:"..string.sub(payload,i+7,j-4).."\r\n")--输出城市信息 print("WEATHER:"..string.sub(payload,string.find(payload,"text")+7,string.find(payload,"code")-4).."\r\n")--输出当前天气 print("TEMP:"..string.sub(payload,string.find(payload,"temperature")+14,string.find(payload,"last_update")-5).."\r\n")--输出当前温度 end ) conn:on("connection", function(conn, payload) conn:send("GET /v3/weather/now.json?key=lkvdhpaemyl8vgme&location=xian&language=en".." HTTP/1.1\r\n".."Host: ".."api.thinkpage.cn".. "\r\n".."Connection: close\r\n\r\n") end) end)
能在此遇见是我们的缘分,我愿意帮助你,相信你一定可以取得成功!
扫码关注微信公众号 “小樊Study”获取更多
QQ群
淘宝店铺