ESP8266对接天猫精灵-多路继电器控制

上面是我的微信和QQ群,欢迎新朋友的加入。

资源和烧录我就不管了,和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.添加子设备 要添加四个(当然你要控制几路就添加几个),注意父设备的一致   保存

打开天猫精灵,更新一下设备

然后就可以控制了

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值