我的 ESP8266 不断重启。
这是我的 init.lua:
cfg={}
cfg.ssid="Sensor"
cfg.auth=AUTH_OPEN
wifi.ap.config(cfg)
wifi.setmode(wifi.STATION)
wifi.sta.getap(function(t)
available_aps = ""
if t then
for k,v in pairs(t) do
ap = string.format("%-10s",k)
ap = trim(ap)
available_aps = available_aps .. "<option value='".. ap .."'>".. ap .."</option>"
end
setup_server(available_aps)
end
end)
function setup_server(aps)
wifi.setmode(wifi.SOFTAP)
srv=net.createServer(net.TCP)
srv:listen(80,function(client)
client:on("receive",function(client,request)
wifi.sta.getap(function(t)
available_aps = ""
if t then
for k,v in pairs(t) do
ap = string.format("%-10s",k)
ap = trim(ap)
available_aps = available_aps .. "<option value='".. ap .."'>".. ap .."</option>"
end
end
end)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = "<html><body>"
buf = buf .. "<h3>Config</h3><br>"
buf = buf .. "<form method='get' action='http://" .. wifi.ap.getip() .."'>"
buf = buf .. "Select access point: <select name='ap'>" .. available_aps .. "</select><br>"
buf = buf .. "Enter wifi password: <input type='password' name='psw'></input><br>"
buf = buf .. "Server-IP: <input name='ipTCP' value='192.168.178.1'></input><br>"
buf = buf .. "<br><button type='submit'>Save</button>"
buf = buf .. "</form></body></html>"
local _on,_off = "",""
if(_GET.pin == "ON1")then
buf = buf.."NICE";
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.LOW);
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
end
为什么每次重启后都会崩溃?
我该如何解决这个问题?
我有由 Lua 5.1.4 提供支持的 NodeMCU 0.9.5 build 20150318。
另一个 lua 脚本运行正常。