創作者:ridxqqqq | 更新日期:2016-12-14 | 在線時長:3天
【藍狐網絡】利用ESP8266-01模塊來控制插座
--init.lua
print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config("XXXXX","XXXXX") //修改相關路由器信息WIFI的賬號和密碼//
--here SSID and PassWord should be modified according your wireless router
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("kaiguan.lua")
end
end)
DEVICEID = "XXXXX" //設備的ID//
APIKEY = "XXXXX" //設備的APIKEY//
INPUTID = "XXXXX" //設備的接口ID//
host = host or "www.eqytg.org"
port = port or 8181
LED = 4
gpio.mode(LED,gpio.OUTPUT)
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" 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 == "stop" 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
end
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)
輸入node.restart()重啟esp8266,看到本地ip說明連接路由器成功,看到welcome to bigiot 說明連接貝殼物聯服務器成功,看到checkinok說明設備登錄成功。
登錄用戶中心對話設備,遙控設備輸入play、stop驗證,LED燈是否有變化。