国内综合精彩aⅤ无码不卡_日本少妇做爰全过程_欧美性爱在线播放免费_午夜?V日韩一区二区_免费a级毛片无码免费播放_成在人线av无码喷水_亚洲精品网站色视频_国产婷婷精品成人_老师撩起裙子让我桶的视频_秋霞影院国产

8路IO接口控制

8路IO接口控制的詳細(xì)介紹

創(chuàng)作者:︻彬▅▆▇◤ | 更新日期:2017-02-04 | 在線時(shí)長(zhǎng):12天
2入6出 控制

PIN 0 板上帶有l(wèi)ed燈,用來(lái)做收信指示。正常為閃動(dòng) 亮燈時(shí)間為發(fā)送到接受到信號(hào)的時(shí)間 失聯(lián)為常亮并延時(shí)后重啟模塊
接口1   1k =開(kāi) 1g=關(guān)
接口2   2k =開(kāi) 2g=關(guān)
.....
接口6   6k =開(kāi) 6g =關(guān)
接口7   cx7 =查詢輸入7電位(1為打開(kāi)0為關(guān)閉)  已短接在接口1
接口8   cx8 =查詢輸入8電位(1為打開(kāi)0為關(guān)閉)  已短接在接口2

代碼為kaiguan.lub修改來(lái)的 剛?cè)腴T,放上來(lái)交流交流

--定義變量
DEVICEID = "1370"
APIKEY = "bXXXXXX5"
INPUTID = "1XXXXX6"
host = host or "www.placeboworld.cn"
port = port or 8181
--存活標(biāo)記
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)
--連接服務(wù)器(TCP連接)
cu = net.createConnection(net.TCP)
cu:connect(port, host)
--接收處理
cu:on("receive", function(cu, c)
  print(c)  
  r = cjson.decode(c)
  --如果存貨標(biāo)記為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
 --收到連接正常,發(fā)送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
    --定時(shí)心跳,防止掉線
    tmr.alarm(1, 10000, 1, function()
        --如果標(biāo)記為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" )
        --發(fā)送后將標(biāo)記置為1
        bLive=1         
    end)
 end
end)