NodeMCU和ESP8266:缓慢的mqtt发布 [英] NodeMCU and ESP8266: slow mqtt publish

查看:354
本文介绍了NodeMCU和ESP8266:缓慢的mqtt发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将esp8266与Marcel的NodeMCU自定义版本生成的固件配合使用 http://frightanic.com /nodemcu-custom-build/ 我测试了"dev"分支和"master".

I'm using esp8266 with the firmware produced with Marcel's NodeMCU custom builds http://frightanic.com/nodemcu-custom-build/ I tested the "dev" branch and the "master".

我对此处找到的"连接到MQTT Broker "代码进行了一些修改, https://github.com/nodemcu/nodemcu-firmware

I changed a little bit the "Connect to MQTT Broker" code found here https://github.com/nodemcu/nodemcu-firmware

-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")

m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)

-- m:connect( host, port, secure, auto_reconnect, function(client) )
-- for secure: m:connect("192.168.11.118", 1880, 1, 0)
-- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
m:connect("192.168.11.118", 1880, 0, 0, function(conn) print("connected") end)

-- publish a message with data = hello, QoS = 0, retain = 0
local i = 1
while i < 10 do
  m:publish("/topic","hello",0,0, function(conn) print("sent") end)
  i = i + 1
end

m:close();  

我正在使用mosquitto作为mqtt经纪人,并且已经针对所有主题#启动了订阅者.

I'm using mosquitto as a mqtt broker and I have launched a subscriber on all topic #.

结果是:消息正确到达,但到达订阅者的速度确实很慢(每条消息大约1秒钟)...为什么?

The result is: the messages arrives correctly but they are really slow to arrive on the subscriber (around 1 second each)... why?

我还尝试更改mqtt体系结构以支持UDP..esp8266快速发送100条消息.

I tried also to change the mqtt architecture in favor of UDP.. the esp8266 send the 100 messages fast.

更新1#:

我还做了一些实验:

  • 测试经纪人和 具有[android手机+ mqtt发布者]的订阅者,该订阅者 立即接收消息
  • 我加载了启用了调试"功能的nodemcu 我做了一个有趣的发现:请继续阅读
  • Testing the broker and the subscriber with an [android phone + a mqtt publisher], the subscriber receive messages immediately
  • I loaded a nodemcu with "debug" enabled and I have done an interesting discovery: read on

对于我所了解的阅读调试日志和源代码. 有一种将消息保存在内存中的队列,计时器(我不知道频率/间隔)从队列中读取消息,然后通过mqtt发送消息. 如果您尝试发送100条消息,则队列会增加,但无法同时传递消息(也许存在争用条件?).

For what I have understood reading debug log and source code.. There is a sort of queue that saves the messages in memory and a timer (I don't know the frequency/interval) reads a message from the queue and it sends it through mqtt. If you try to send 100 messages, the queue increases, but it is not able to deliver messages at the same time (maybe there is a race condition? ).

这里还有第二个问题,当它排入15条以上的消息之后,固件崩溃并且设备重新启动:这似乎是内存不再可用的症状.

There is a second problem here, after it has enqueued more than 15 messages, the firmware crash and the device reboots: it seems a symptom of memory no more available.

推荐答案

它可能不是您要找的答案,但是可以,NodeMCU MQTT使用内部队列来处理消息.在2015年3月底添加了 .由于NodeMCU API的异步性质.

It may not be the answer you're looking for but yes, NodeMCU MQTT uses an internal queue for messages. It was added at the end of March 2015. It was added due to the asynchronous nature of the NodeMCU API.

如果快速连续有两个呼叫m.publish,请记住它们是异步的,没有足够的时间在触发第二个消息之前传递第一个消息.如果您是循环发布的,那么在引入该队列之前,固件只会崩溃.

If you have two calls to m.publish in quick succession, remember they're asynchronous, there isn't enough time for the 1st message to be delivered before the 2nd is triggered. Before the introduction of that queue the firmware would simply have crashed if you had published in a loop.

我进一步简化了代码,并添加了一些调试语句:

I simplified your code even more and added some debugging statements:

m = mqtt.Client("clientid", 120, "user", "password")

m:connect("m20.cloudmqtt.com", port, 0, function(conn) 
    print("MQTT connected")
    for i=1,10 do
      print("MQTT publishing...")
      m:publish("/topic", "hello", 0, 0, function(conn) 
        print("MQTT message sent")
        print("  heap is " .. node.heap() .. " bytes")
      end)
      print("  heap is " .. node.heap() .. " bytes in loop " .. i)
    end
end)

知道对m.publish的调用是异步的,因此输出不会太令人惊讶:

Knowing that the calls to m.publish are asynchronous the output shouldn't be too surprising:

MQTT connected
MQTT publishing...
  heap is 37784 bytes in loop 1
MQTT publishing...
  heap is 37640 bytes in loop 2
MQTT publishing...
  heap is 37520 bytes in loop 3
MQTT publishing...
  heap is 37448 bytes in loop 4
MQTT publishing...
  heap is 37344 bytes in loop 5
MQTT publishing...
  heap is 37264 bytes in loop 6
MQTT publishing...
  heap is 37192 bytes in loop 7
MQTT publishing...
  heap is 37120 bytes in loop 8
MQTT publishing...
  heap is 37048 bytes in loop 9
MQTT publishing...
  heap is 36976 bytes in loop 10
sent
  heap is 38704 bytes
sent
  heap is 38792 bytes
sent
  heap is 38856 bytes
sent
  heap is 38928 bytes
sent
  heap is 39032 bytes
sent
  heap is 39112 bytes
sent
  heap is 39184 bytes
sent
  heap is 39256 bytes
sent
  heap is 39328 bytes
sent
  heap is 39400 bytes

您看到发布时可用的堆空间正在减少,并且随着队列被清空,可用堆空间再次增加.

You see that the available heap space is decreasing while publishing and increasing again as the queue is emptied.

这篇关于NodeMCU和ESP8266:缓慢的mqtt发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆