ESP8266對接天貓精靈-多路繼電器控制

Jun626發表於2020-10-18

上面是我建立的群聊,歡迎新朋友的加入。

資源和燒錄我就不管了,和https://blog.csdn.net/Jun626/article/details/109150006一模一樣

把那個kaiguan的lua改一下程式碼

DEVICEID = "19539" 
APIKEY   = "17aaa8a16"
INPUTID  = "10945"
host     = host or "www.bigiot.net"
port     = port or 8181
RELAY1     = 0
RELAY2     = 1
RELAY3     = 2
RELAY4     = 3
RELAY5     = 4
RELAY6     = 5
gpio.mode(RELAY1,gpio.OUTPUT)
gpio.mode(RELAY2,gpio.OUTPUT)
gpio.mode(RELAY3,gpio.OUTPUT)
gpio.mode(RELAY4,gpio.OUTPUT)
gpio.mode(RELAY5,gpio.OUTPUT)
gpio.mode(RELAY6,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" and r.S =="D1904" then   
        gpio.write(RELAY1, gpio.HIGH)  
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY1 turn on!"})
        cu:send( played.."\n" )
      end
    if r.C == "play" and r.S =="D1905" then   
          gpio.write(RELAY2, gpio.HIGH)  
          ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY2 turn on!"})
        cu:send( played.."\n" )
      end 
    if r.C == "play" and r.S =="D1906" then   
        gpio.write(RELAY3, gpio.HIGH)  
          ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY3 turn on!"})
        cu:send( played.."\n" )
      end
    if r.C == "play" and r.S =="D1907" then   
         gpio.write(RELAY4, gpio.HIGH)  
         ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY4 turn on!"})
        cu:send( played.."\n" )
      end
    if r.C == "play" and r.S =="D1908" then   
        gpio.write(RELAY5, gpio.HIGH)  
          ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY5 turn on!"})
        cu:send( played.."\n" )
      end
    if r.C == "play" and r.S =="D1909" then   
         gpio.write(RELAY6, gpio.HIGH)  
         ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY6 turn on!"})
        cu:send( played.."\n" )
      end
    if r.C == "stop" and r.S =="D1904" then   
        gpio.write(RELAY1, gpio.LOW)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY1 turn off!"})
        cu:send( stoped.."\n" ) 
      end
    if r.C == "stop" and r.S =="D1905" then   
          gpio.write(RELAY2, gpio.LOW)
          ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY2 turn off!"})
        cu:send( stoped.."\n" ) 
      end
    if r.C == "stop" and r.S =="D1906" then   
        gpio.write(RELAY3, gpio.LOW)
          ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY3 turn off!"})
        cu:send( stoped.."\n" ) 
      end
    if r.C == "stop" and r.S =="D1907" then   
        gpio.write(RELAY4, gpio.LOW)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY4 turn off!"})
        cu:send( stoped.."\n" ) 
      end
    if r.C == "stop" and r.S =="D1908" then   
        gpio.write(RELAY5, gpio.LOW)
          ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY5 turn off!"})
        cu:send( stoped.."\n" ) 
      end
    if r.C == "stop" and r.S =="D1909" then   
        gpio.write(RELAY6, gpio.LOW)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="RELAY6 turn off!"})
        cu:send( stoped.."\n" ) 
      end
    end
  end)
  cu:on('disconnection',function(scu)
    cu = nil
    --停止心跳包傳送定時器,5秒後重試
    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("faiRELAY to encode!")
  end
  cu:send(s.."\n")
  tmr.alarm(1, 60000, 1, function()
    cu:send(s.."\n")
  end)
end
run()

燒錄

步驟來源於https://www.bigiot.net/talk/935.html

開始
 1.  新增裝置 
    2. 點選編輯並將裝置型別設定為萬能遙控器儲存


     3. 新增介面 儲存

  


4.新增子裝置 要新增四個(當然你要控制幾路就新增幾個),注意父裝置的一致   儲存

開啟天貓精靈,更新一下裝置

然後就可以控制了

相關文章