当前位置:   article > 正文

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

开关灯代码 另改命为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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/263274?site
推荐阅读
相关标签
  

闽ICP备14008679号