8266+天猫精灵窗帘开度控制心得,有需要的可以看看

作者:情.若隔至千里 | 更新时间:2019-04-06 | 浏览量:3154

以下是部分代码,官方示例里面的,加粗部分是自己搞出来的。

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
       
        s=r.C                                                       --把接收到天猫精灵的数据"C"后面的字符串赋值给s
        date = "%d%d"                                      --匹配需要提出开度的数据格式
        hh=string.sub(s, string.find(s, date))   --取出字符串s里面的数字,也就是我们最终需要的开度,比如25%,30%,取出来的数据还是string型,并非int型

        
      if hh=="25" then  
        gpio.write(LED, gpio.LOW)  
        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.HIGH)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
        cu:send( stoped.."\n" ) 
      end
       
    end
  end)


评论:共2条

douzs 评论于:2019-04-27 12:33:34
感谢,正需要
YHZ2012 评论于:2020-03-26 15:58:22
b = cjson.decode(r.C)
hh=b.v
返回顶部