赞
踩
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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。