1

Marcel の NodeMCU カスタム ビルドhttp://frightanic.com/nodemcu-custom-build/で作成されたファームウェアで esp8266 を使用しています 。「dev」ブランチと「master」をテストしました。

ここにある「 Connect to MQTT Broker」コードを少し変更しました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 ブローカーとして使用しており、すべてのトピック#でサブスクライバーを立ち上げました。

結果は次のとおりです。メッセージは正しく到着しますが、サブスクライバーに到着するのは本当に遅いです (それぞれ約 1 秒)...なぜですか?

また、UDP を優先して mqtt アーキテクチャを変更しようとしました。esp8266 は 100 メッセージを高速で送信します。

更新 1#:

私はさらにいくつかの実験を行いました:

  • [android phone + a mqtt publisher] を使用してブローカーとサブスクライバーをテストすると、サブスクライバーはすぐにメッセージを受信します
  • 「デバッグ」を有効にして nodemcu をロードしたところ、興味深い発見がありました: 続きを読む

デバッグログとソースコードを読んで理解したことについて..メッセージをメモリに保存する一種のキューがあり、タイマー(頻度/間隔はわかりません)がキューからメッセージを読み取り、それを介して送信しますmqtt。100通のメッセージを送ろうとするとキューが増えるのですが、同時にメッセージを届けることができません(もしかしたら競合状態?)。

ここで 2 つ目の問題があります。15 を超えるメッセージがエンキューされた後、ファームウェアがクラッシュし、デバイスが再起動します。これは、メモリが使用できなくなったという症状のようです。

4

1 に答える 1

4

あなたが探している答えではないかもしれませんが、はい、NodeMCU MQTT はメッセージに内部キューを使用します。2015 年 3 月末に追加されました。NodeMCU API の非同期性のために追加されました

2 つの呼び出しがm.publish立て続けに行われる場合は、それらが非同期であることを覚えておいてください。2 つ目のメッセージがトリガーされる前に、最初のメッセージが配信されるのに十分な時間がありません。そのキューが導入される前は、ループでパブリッシュした場合、ファームウェアは単純にクラッシュしていました。

コードをさらに簡素化し、いくつかのデバッグ ステートメントを追加しました。

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いれば、出力はそれほど驚くべきことではありません。

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

パブリッシュ中に使用可能なヒープ領域が減少し、キューが空になると再び増加することがわかります。

于 2015-10-30T20:46:17.277 に答える