作者:1197994757 | 更新時間:2019-07-12 | 瀏覽量:3871
這是一個不錯的想法... ( 你怎么知道的?)
這里我只寫貝殼物聯子設備的使用和“kaiguan.lua"的具體代碼 詳細教程比如使用的軟件啊下載操作啊還有"init.lua" 我就不貼出來了,都一樣的。不會的看參考教程 參考鏈接 http://www.eqytg.org/help/20.html https://blog.csdn.net/qq_33508087/article/details/82925995 http://www.eqytg.org/talk/927.html 萌新看這三篇夠了 老司機直接開始 開始 1. 添加設備2. 點擊編輯并將設備類型設置為萬能遙控器保存
3. 添加接口 保存
4.添加子設備 要添加四個(當然你要控制幾路就添加幾個),注意父設備的一致 保存
![]()
5. OK 接下來打開 "kaiguan.lua" 把里面的代碼全刪了, 把下面的代碼復制上去。
DEVICEID = "12046" APIKEY = "6870b391e" INPUTID = "10945" host = host or "www.eqytg.org" port = port or 8181 LED = 5 LED1 = 6 LED2 = 7 LED3 = 1 gpio.mode(LED,gpio.OUTPUT) gpio.mode(LED1,gpio.OUTPUT) gpio.mode(LED2,gpio.OUTPUT) gpio.mode(LED3,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 =="D258" or r.C == "play1" then gpio.write(LED, gpio.HIGH) ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"}) cu:send( played.."\n" ) end if r.C == "play" and r.S =="D259" or r.C == "play2" then gpio.write(LED1, gpio.HIGH) ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED1 turn on!"}) cu:send( played.."\n" ) end if r.C == "play" and r.S =="D260" or r.C == "play3" then gpio.write(LED2, gpio.HIGH) ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED2 turn on!"}) cu:send( played.."\n" ) end if r.C == "play" and r.S =="D261" or r.C == "play4" then gpio.write(LED3, gpio.HIGH) ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED3 turn on!"}) cu:send( played.."\n" ) end if r.C == "stop" and r.S =="D258" or r.C == "stop1" then gpio.write(LED, gpio.LOW) ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"}) cu:send( stoped.."\n" ) end if r.C == "stop" and r.S =="D259" or r.C == "stop2" then gpio.write(LED1, gpio.LOW) ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED1 turn off!"}) cu:send( stoped.."\n" ) end if r.C == "stop" and r.S =="D260" or r.C == "stop3" then gpio.write(LED2, gpio.LOW) ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED2 turn off!"}) cu:send( stoped.."\n" ) end if r.C == "stop" and r.S =="D261" or r.C == "stop4" then gpio.write(LED3, gpio.LOW) ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED3 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("failed to encode!") end cu:send(s.."\n") tmr.alarm(1, 60000, 1, function() cu:send(s.."\n") end) end run()
6.修改代碼
代碼前三行寫你的 (LED 5 6 7 1 對應 ESP8266的 12 13 14 5 引腳 ,懂的自行修改)
將 “ if r.C == "play" and r.S =="D258" or r.C == "play1" then ”里的258修改為你的子設備ID 我的對應插座一
一樣其余三個對應你的子設備ID修改
if r.C == "stop" and r.S =="D258" or r.C == "stop1" then 這個關的同理 也改好
7.改好 并下載到 ESP8266
8.復位或重啟 去貝殼物聯設備列表刷新看看設備有沒有上線 (要好一會才能刷新出來)
好吧 我這個號被禁止登錄 沒圖了
----------------------------------------------------------------------
上線了試一下 設備對話--發送命令 play1 如果有返回數據就是正常了
9.天貓精靈綁定貝殼物聯 并刷新 看到四個插座后 就可以讓貓精獨立控制ESP 四路繼電器了(可以用萬用表測一下ESP 12 13 14 5 腳的電平)
因為是我的小號 所以不打碼了 好讓你們看清我的思路 這是重點 如果沒有成功就看代碼了解我的思路自己改去
我需要經驗+1 我的子設備不夠用沒法繼續折騰了
看完了回復一下吧
我需要經驗+1