怎么用一个模块控制两个LED

作者:liu583501928 | 更新时间:2019-04-02

这是一个不错的想法...

评论:共5条

liu583501928 评论于:2019-04-02 23:48:12
照着kaiguan.lua改的,两个倒是都能控制,不过LED1打开关闭一次,设备就离线了,是我想法不对吗?
--use sjson
_G.cjson = sjson
--modify DEVICEID INPUTID APIKEY
DEVICEID = "10xxx"

APIKEY = "88xxxxxx"

INPUTID = "8xxx"
host = host or "www.bigiot.net"
port = port or 8181
LED = 3
LED1 = 4


isConnect = false
gpio.mode(LED,gpio.OUTPUT)
gpio.mode(LED1,gpio.OUTPUT)
local function run()
local cu = net.createConnection(net.TCP)
cu:on("receive", function(cu, c)
print(c)
isConnect = true
r = cjson.decode(c)
if r.M == "say" then
if r.C == "play" then
gpio.write(LED, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"})
cu:send( played.."\n" )
end

if r.C == "play1" then
gpio.write(LED1, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"})
cu:send( play1ed.."\n" )
end

if r.C == "stop" then
gpio.write(LED, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
cu:send( stoped.."\n" )
end

if r.C == "stop1" then
gpio.write(LED1, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
cu:send( stop1ed.."\n" )
end
end
end)
cu:on('disconnection',function(scu)
cu = nil
isConnect = false
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)
else
print("failed to encode!")
end
if isConnect then
cu:send(s.."\n")
end
tmr.alarm(1, 60000, 1, function()
if isConnect then
cu:send(s.."\n")
end
end)
end
run()
情.若隔至千里 评论于:2019-04-08 20:15:24
LED = 1
LED1 = 2
你试一试,D4好像是串口
zjnbzm 评论于:2019-04-11 16:40:05
改为以下就可
if r.M == "say" then
if r.C == "play" then
gpio.write(LED, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"})
cu:send( played.."\n" )
end

if r.C == "play1" then
gpio.write(LED1, gpio.HIGH)
ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED1 turn on!"})
cu:send( play1ed.."\n" )
end

if r.C == "stop" then
gpio.write(LED, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
cu:send( stoped.."\n" )
end

if r.C == "stop1" then
gpio.write(LED1, gpio.LOW)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED1 turn off!"})
cu:send( stop1ed.."\n" )
end
douzs 评论于:2019-04-28 13:46:53
很厉害了,我的到现在还没头绪呢
面包不好吃 评论于:2019-07-02 06:16:10
我已经成功用一个模块控制 多路
评论支持使用[code][/code]标签添加代码
返回顶部