ESP8266-智能插座

ESP8266-智能插座的详细介绍

创作者: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.bigiot.net"

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灯是否有变化。