作者:情.若隔至千里 | 更新時間:2019-04-06 | 瀏覽量:3062
以下是部分代碼,官方示例里面的,加粗部分是自己搞出來的。
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)