nodemcu 贝壳物联远程控制继电器

nodemcu 贝壳物联远程控制继电器

wifi配置


print("set up wifi mode")--shezhimoshi

wifi.setmode(wifi.STATION) 

station_cfg={}
--here SSID and PassWord should be modified according your wireless router
station_cfg.ssid="Zdd-4089139"
station_cfg.pwd="1234567890" 
station_cfg.save=true
wifi.sta.config(station_cfg)

wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...") --dengdailianjie
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("kaiguan.lua")--zuanhuanwenjianzhixing
end
end)

开关灯代码 另改命为kaiguan.lua 换名也行 和上代码对应。

--use sjson
_G.cjson = sjson
--modify DEVICEID INPUTID APIKEY
DEVICEID = "10359"   
APIKEY = "0d68cab66"
INPUTID = "36"
host = host or "www.bigiot.net"
port = port or 8181
LED = 5
LED1 = 6
LED = 7
LED1 = 8
isConnect = false
gpio.mode(LED,gpio.OUTPUT)
gpio.mode(LED1,gpio.OUTPUT)
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
      if r.C == "play" then   
        gpio.write(LED, gpio.HIGH)  
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="0kai"})
        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="0guan"})
        cu:send( stoped.."\n" ) 
      end
    end
    if r.M == "say" then
      if r.C == "play1" then   
        gpio.write(LED1, gpio.HIGH)  
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="1kai"})
        cu:send( played.."\n" )
      end
      if r.C == "stop1" then   
        gpio.write(LED1, gpio.LOW)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="1guan"})
        cu:send( stoped.."\n" ) 
      end
    end
    if r.M == "say" then
      if r.C == "play2" then   
        gpio.write(LED, gpio.HIGH)  
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="2kai"})
        cu:send( played.."\n" )
      end
      if r.C == "stop2" then   
        gpio.write(LED, gpio.LOW)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="2guan"})
        cu:send( stoped.."\n" ) 
      end
    end
    if r.M == "say" then
      if r.C == "play3" then   
        gpio.write(LED1, gpio.HIGH)  
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="3kai"})
        cu:send( played.."\n" )
      end
      if r.C == "stop3" then   
        gpio.write(LED1, gpio.LOW)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="3gaun"})
        cu:send( stoped.."\n" ) 
      end
    end
  end)
  cu:on('disconnection',function(scu)
    cu = nil
    isConnect = false
    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
  if isConnect then
    cu:send(s.."\n")
  end
  tmr.alarm(1, 6000, 1, function()
    if isConnect then
      cu:send(s.."\n")
    end
  end)
end
run()

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值