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" --我的wifi名称
station_cfg.pwd="1234567890" --我的wifi密码
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)
--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
gpio.write(LED, gpio.HIGH)
gpio.write(LED1, gpio.HIGH)
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.LOW)
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.HIGH)
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.LOW)
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.HIGH)
ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="1guan"})
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()
上电重置nodemcu
登录微信进入微信小程序会显示
登录官网显示nodemcu在线
-发送 play 打开第一个继电器 发送stop关闭 发送play1打开第二继电器 发送stop1关闭