创作者:︻彬▅▆▇◤ | 更新日期:2017-02-04 | 在线时长:12天
2入6出 控制
PIN 0 板上带有led灯,用来做收信指示。正常为闪动 亮灯时间为发送到接受到信号的时间 失联为常亮并延时后重启模块
接口1 1k =开 1g=关
接口2 2k =开 2g=关
.....
接口6 6k =开 6g =关
接口7 cx7 =查询输入7电位(1为打开0为关闭) 已短接在接口1
接口8 cx8 =查询输入8电位(1为打开0为关闭) 已短接在接口2
代码为kaiguan.lub修改来的 刚入门,放上来交流交流
--定义变量
DEVICEID = "1370"
APIKEY = "bXXXXXX5"
INPUTID = "1XXXXX6"
host = host or "www.bigiot.net"
port = port or 8181
--存活标记
bLive=0
--初始化GPIO列表
OUTList={1,2,3,4,5,6,0}
for i,v in ipairs(OUTList) do
print("GPIO:"..v)
gpio.mode(tonumber(v),gpio.OUTPUT)
print("GPIO:"..v.."OUTPUT")
end
gpio.mode(7,gpio.INPUT)
gpio.mode(8,gpio.INPUT)
gpio.write(0,gpio.LOW)
gpio.write(1,gpio.LOW)
gpio.write(2,gpio.LOW)
gpio.write(3,gpio.LOW)
gpio.write(4,gpio.LOW)
gpio.write(5,gpio.LOW)
gpio.write(6,gpio.LOW)
gpio.write(7,gpio.LOW)
gpio.write(8,gpio.LOW)
--连接服务器(TCP连接)
cu = net.createConnection(net.TCP)
cu:connect(port, host)
--接收处理
cu:on("receive", function(cu, c)
print(c)
r = cjson.decode(c)
--如果存货标记为1,置为0
if bLive==1 then
if r.M=="isOL" then
gpio.write(0, gpio.HIGH)
bLive=0
end
end
--如果是say代表命令
if r.M == "say" then
gpio.write(0, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="Y0 turn off!"})
cu:send( stoped.."\n" )
if r.C == "1k" then
gpio.write(1, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="Y1 turn on!"})
cu:send( played.."\n" )
end
if r.C == "1g" then
gpio.write(1, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="Y1 turn off!"})
cu:send( stoped.."\n" )
end
if r.C == "2k" then
gpio.write(2, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="Y2 turn on!"})
cu:send( played.."\n" )
end
if r.C == "2g" then
gpio.write(2, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="Y2 turn off!"})
cu:send( stoped.."\n" )
end
if r.C == "3k" then
gpio.write(3, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="Y3 turn on!"})
cu:send( played.."\n" )
end
if r.C == "3g" then
gpio.write(3, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="Y3 turn off!"})
cu:send( stoped.."\n" )
end
if r.C == "4k" then
gpio.write(LED4, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="Y4 turn on!"})
cu:send( played.."\n" )
end
if r.C == "4g" then
gpio.write(4, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="Y4 turn off!"})
cu:send( stoped.."\n" )
end
if r.C == "5k" then
gpio.write(5, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="Y5 turn on!"})
cu:send( played.."\n" )
end
if r.C == "5g" then
gpio.write(5, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="Y5 turn off!"})
cu:send( stoped.."\n" )
end
if r.C == "6k" then
gpio.write(6, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="Y6 turn on!"})
cu:send( played.."\n" )
end
if r.C == "6g" then
gpio.write(6, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="Y6 turn off!"})
cu:send( stoped.."\n" )
end
if r.C == "cx7" then
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C=gpio.read(7)})
cu:send( stoped.."\n" )
end
if r.C == "cx8" then
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C=gpio.read(8)})
cu:send( stoped.."\n" )
end
end
--收到连接正常,发送checkin
if r.M == "WELCOME TO BIGIOT" then
ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY})
if ok then
print(s)
else
print("failed to encode!")
end
cu:send( s.."\n" )
bLive=0
--定时心跳,防止掉线
tmr.alarm(1, 10000, 1, function()
--如果标记为1,表示未收到上次的心跳返回,重启
if bLive==1 then
node.restart()
end
ok, ticket = pcall(cjson.encode, {M="isOL",ID="D"..DEVICEID})
gpio.write(0, gpio.LOW)
print(ticket)
cu:send(ticket.."\n" )
--发送后将标记置为1
bLive=1
end)
end
end)