【问题标题】:Why NodeMCU crashes with no error after boot with this code?为什么使用此代码启动后 NodeMCU 崩溃且没有错误?
【发布时间】:2016-03-26 08:40:46
【问题描述】:

我的 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

为什么每次重启后都会崩溃?

我该如何解决这个问题?

我有 NodeMCU 0.9.5 build 20150318,由 Lua 5.1.4 提供支持。

另一个 lua 脚本运行正常。

【问题讨论】:

  • NodeMCU 0.9.5 真的很旧,尝试使用来自nodemcu-build.com 的最新固件版本。您不能指望我们在没有进一步提示的情况下立即在那个长代码示例中发现错误。我确定您在崩溃时会收到一些错误消息?尝试逐步消除代码,直到找到导致错误的几行。
  • 问题已解决,但感谢您指出我有旧版本的nodemcu。
  • 不要使用 0.95。不要在on('receive') 中使用client:close()。它需要进入on('sent') cb。

标签: lua esp8266 nodemcu


【解决方案1】:

我在 client:on("receive",function(client,request). 它不应该在这里。

【讨论】:

    【解决方案2】:
    1. 我没有看到对 wifi.sta.connect() 的调用
    2. 字符串是不可变的,因此每次重新分配 buf 时,都会创建一个新的。是的,如果您不先用完,GC 会清理它。
    3. 您不能连续调用 client:send() 和 client:close()... 好吧,可以,但效果不佳。我还担心在一个电话中发送所有这些。
    4. 尽管它们不接受回调,但 wifi 配置和连接例程确实需要异步运行,使用计时器调用它们,然后在继续之前检查状态。

    第一个项目是一个显示停止器,其他项目可能会导致彻底失败或可能是间歇性的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2019-01-14
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 2012-08-04
      • 2017-06-24
      相关资源
      最近更新 更多