Lua nodemcu(esp8266)的mqtt上的Keepalive计时器没有响应

Lua nodemcu(esp8266)的mqtt上的Keepalive计时器没有响应,lua,mqtt,keep-alive,esp8266,nodemcu,Lua,Mqtt,Keep Alive,Esp8266,Nodemcu,亲爱的, 我试图在基于nodemcu的esp8266上使用mqtt。我当前正在使用自定义生成() 使用的模块:adc、终端用户设置、文件、gpio、http、mqtt、网络、节点、ow、pwm、tmr、uart、wifi 版本:由SDK 1.5.1(e67da894)上的Lua 5.1.4支持 因此,在程序的初始化时,我调用connect_to_mqtt_broker(),它工作正常,我可以订阅和发布主题 问题是keepalive计时器不正确。让我用一个例子来解释这一点。我设置KEEP_ALI

亲爱的, 我试图在基于nodemcu的esp8266上使用mqtt。我当前正在使用自定义生成()

使用的模块:adc、终端用户设置、文件、gpio、http、mqtt、网络、节点、ow、pwm、tmr、uart、wifi

版本:由SDK 1.5.1(e67da894)上的Lua 5.1.4支持


因此,在程序的初始化时,我调用connect_to_mqtt_broker(),它工作正常,我可以订阅和发布主题

问题是keepalive计时器不正确。让我用一个例子来解释这一点。我设置KEEP_ALIVEu TMR=120s,在esp8266成功连接到mqtt代理后,我禁用了路由器上的wifi并开始计算秒数。根据KEEP_ALIVE_TMR离线事件:

m:on("offline", function(conn) 
        print("["..tmr.time().."(s)] - Mqtt client gone offline")

end)
应该在我禁用WiFi的那一刻起120秒后触发,但不知什么原因,这不会发生。通常情况下,事件会在10-15分钟后发生。 我正在努力理解这次延误的原因,但没有成功。
你知道为什么会发生这种奇怪的事情吗?

当设置了
autoreconnect
标志时,我也遇到了同样的问题。此标志中断代理之间连接的脱机运行事件

请在不设置自动重新连接(默认值为0)的情况下进行尝试:

m:connect(BROKER, PORT, 0, function(client) 

当设置
autoreconnect
标志时,我也遇到了同样的问题。此标志中断代理之间连接的脱机运行事件

请在不设置自动重新连接(默认值为0)的情况下进行尝试:

m:connect(BROKER, PORT, 0, function(client) 

如果您通过打开/关闭mqtt代理来进行测试,那么它是有效的。但不是通过切换wifi连接,而是nodemcu的mqtt库问题

我相信在wifi断开连接时不存在此类mqtt脱机/断开连接事件。这里是addconnection的看门狗的解决方法

tmr.alarm(1, 3000, 1, function()
  if wifi.sta.getip() == nil then
   --mark as mqtt restart needed
   restart = true
 else
   -- wifi reconnect detected then restart mqtt connections
   if restart == true then
     --reset flag, clean object, init for a new one
     restart = false
     m = nil
     mqtt_init()
     connect()
   end
 end
end)

如果您通过打开/关闭mqtt代理来进行测试,那么它可以正常工作。但不是通过切换wifi连接,而是nodemcu的mqtt库问题

我相信在wifi断开连接时不存在此类mqtt脱机/断开连接事件。这里是addconnection的看门狗的解决方法

tmr.alarm(1, 3000, 1, function()
  if wifi.sta.getip() == nil then
   --mark as mqtt restart needed
   restart = true
 else
   -- wifi reconnect detected then restart mqtt connections
   if restart == true then
     --reset flag, clean object, init for a new one
     restart = false
     m = nil
     mqtt_init()
     connect()
   end
 end
end)

试过下面的答案吗?试过下面的答案吗?