带有 NodeMCU 的 ESP8266 仅响应 3 次

ESP8266 with NodeMCU responds only 3 times

我有一个简单的 Lua 脚本可以创建一个服务器来侦听消息。当消息为 'led1' 或 'led2' 时,脚本会翻转两个 GPIO 的输出。问题是该脚本只能工作 2 或 3 次。我会带着实际的剧本回来。

编辑 1:服务器脚本如下(删除了 GPIO 部分以简化脚本):

wifi.setmode(1)
wifi.sta.config("my_router","12345678")
tmr.delay(3000000)
print(wifi.sta.getip())

s=net.createServer(net.TCP)
s:listen(433,function(conn)
    conn:on("receive",function(conn,payload)
        print(payload)
    conn:send("received")
    end)
    conn:on("sent",function(conn) conn:close() end)
end)

结果如下(如果我向服务器发送'led1'超过3次):

>192.168.0.117 255.255.255.0 192.168.0.1
>led1
>led1
>led1

在此之后,客户说 'connection timed out' 但 ESP8266 仍然工作(至少串行线)

3 秒的 tmr.delay 会搞砸 wifi 堆栈。使用 tmr.alarm 并将主要处理挂起。这个例子对我来说很好用:

do
  local srv = net.createServer(net.TCP)
    srv:listen(8888, function(sk)
      sk:on("receive", function(sk, rec)
      print("Received ", rec)
      sk:send("echo "..rec, sk.close)
    end)
  end)
  function close() srv:close() end
end

发送的最后一个参数只是关闭套接字的完成回调。您还需要关闭 srv 以释放对 Lua 注册表中侦听器函数的引用。

确保您使用来自 nodeMCU 云构建器的当前 dev 构建

这是对特里回答的补充。

正如他所说,主要问题是您的 tmr.delay(3000000)。它接受 micro 秒而不是毫秒是有原因的。这是 API documentation 的摘录:

This is in general a bad idea, because nothing else gets to run, and the networking stack (and other things) can fall over as a result. The only time tmr.delay() may be appropriate to use is if dealing with a peripheral device which needs a (very) brief delay between commands, or similar. Use with caution!

你首先添加延迟的原因是因为你想等到 WiFi 堆栈完全初始化(即分配 IP)。你想要做的是设置一个 tmr.alarm "loops" 以 1s 为间隔,直到 WiFi 准备就绪。这是来自 https://cknodemcu.wordpress.com 的缩写示例:

--init.lua
function startup()
    -- do stuff here
end

print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,PASSWORD)
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function() 
    if wifi.sta.getip() == nil then 
        print("IP unavaiable, Waiting...") 
    else 
        tmr.stop(1)
        print("Config done, IP is "..wifi.sta.getip())
        print("You have 5 seconds to abort Startup")
        print("Waiting...")
        tmr.alarm(0, 5000, 0, startup)
    end 
 end)

与其检查 wifi.sta.getip(),不如检查 wifi.sta.status() == 5 like I do in an old Gist

此外,请查看 net module API documentation