init.lua代码
print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config("TP-LINK_Yang","family10086")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
print("Config SUCESS ")
dofile("kaiguan.lua")
end
end)
开关.lua代码
DEVICEID = "17827"
APIKEY = "9c1ca9bf3"
INPUTID = "17827"
host = host or "www.bigiot.net"
port = port or 8181
LED = 2 --GPIO4 D2
--pwm.setup(pin,50,70) --128 PWM
--pwm.start(pin)
gpio.mode(LED,gpio.OUTPUT)
local function run()
local cu = net.createConnection(net.TCP)
cu:on("receive", function(cu, c)
print(c)
r = cjson.decode(c)
if r.M == "say" then
if r.C == "play" then
gpio.write(LED, gpio.LOW)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"})
cu:send( played.."\n" )
end
if r.C == "stop" then
gpio.write(LED, gpio.HIGH)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
cu:send( stoped.."\n" )
end
end
end)
cu:on('disconnection',function(scu)
cu = nil
tmr.stop(1)
tmr.alarm(6, 5000, 0, run)
end)
cu:connect(port, host)
ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY})
if ok then
print(s)
print("welcome to iot")
else
print("failed to encode!")
end
------------------------------
tmr.alarm(1, 50000, 1, function()
cu:send(s.."\n")
end)
tmr.alarm(5, 6000, 1, function()
cu:send("{\"M\":\"update\",\"ID\":\"17827\",\"V\":{\"16385\":\"5\"}}\r\n")
print("data recall is sucess")
--cu:send( stoped.."\n" )
end)
end
run()