实现外部一键开关LED问题,大神帮加消抖

作者:梵歌指纹密码锁批发零售 | 更新时间:2019-04-12 | 浏览量:1106

求大神帮忙加个消抖,非常感谢

DEVICEID = "8888"     
APIKEY   = "88818da" 
INPUTID  = "88888"     
host     = host or "www.bigiot.net"  --
port     = port or 8181
LED      = 6
LED1     = 5
LED2     = 2
gpio.mode(LED,gpio.OUTPUT)
gpio.mode(LED1,gpio.OUTPUT)
gpio.mode(LED2,gpio.OUTPUT)
gpio.mode(2,gpio.INT, gpio.PULLUP)
gpio.write(2, gpio.HIGH)
gpio.write(1, gpio.HIGH)
 local function ledTrg()
     local i = gpio.read(6)
     if (i == 1) then
         gpio.write(6, gpio.LOW)
    else
         gpio.write(6, gpio.HIGH)
    end
    end
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.HIGH)
        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.LOW)
        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)
  else
    print("failed to encode!")
  end
  cu:send(s.."\n")
  tmr.alarm(1, 60000, 1, function()
    cu:send(s.."\n")
  end)
end
gpio.trig(2, "low", ledTrg)
run()


评论:共0条

返回顶部