NodeMCU and ESP8266: slow mqtt publish

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ 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. 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) 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.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links