|
发表于 2015-12-19 12:07:48
|
显示全部楼层
NodeMCU和Yeelink之前都有例子,但是因为久远部分参变量已经改变,直接调试不通,经过TCP调试工具验证,修正代码如下,通过,共享给大家,希望少走弯路,参考前人贴
http://www.geek-workshop.com/thread-12689-1-1.html
修改以下部分:
init.lua
if true then --change to if true
print("set up wifi mode")
wifi.setmode(wifi.STATION)
--please config ssid and password according to settings of your wireless router.
wifi.sta.config("wifi-id","wifi-key") --这里设置你的WIFI名字和密码
wifi.sta.connect()
cnt = 0
tmr.alarm(1, 1000, 1, function()
if (wifi.sta.getip() == nil) and (cnt < 20) then
print("IP unavaiable, Waiting...")
cnt = cnt + 1
else
tmr.stop(1)
if (cnt < 20) then print("Config done, IP is "..wifi.sta.getip())
--dofile("yeelink_POST3.lua") --这里设置自动执行的程序,去掉减号就可以运行了。建议调试时候屏蔽单独运行
else
print("No Wifi Connected.")
end
end
end)
else
print("\n")
print("lease edit 'init.lua' first:")
print("Step 1: Modify wifi.sta.config() function in line 5 according settings of your wireless router.")
print("Step 2: Change the 'if false' statement in line 1 to 'if true'.")
end
yeelinkpost.lua
tmr.alarm(0, 30000, 1, function()
conn=net.createConnection(net.TCP, 0)
connn("connection", function(conn) topost() end)
connn("disconnection", function(conn, pl) print("disconnection") conn:close() end)
connn("receive", function(conn, pl) conn:close() print(pl) end )
conn:connect(80,"42.96.164.52")--这个不同于参考贴,目前测试通过2015年12月19日
function topost()
st="{\r\n \"timestamp\":\"2015-12-17T23:46:14\",\r\n \"value\":1\r\n}"
conn:send("OST /v1.0/device/123456/sensor/654321/datapoints HTTP/1.1\r\n"--修改为自己的设备和传感器ID
.."Host: api.yeelink.net\r\n"
.."Accept: */*\r\n"
.."U-ApiKey:d330d8afc49d2863\r\n" --这里的APIkey改成你自己的
.."Content-Length:"..string.len(st).."\r\n\r\n"--the length of json is important
..st.."\r\n")
end
end) |
|