12377 views|24 replies

3386

Posts

0

Resources
The OP
 

MicroPython Hands-on (32) - MQTT for the Internet of Things [Copy link]

 
 

MQTT
(Message Queuing Telemetry Transport) is a "lightweight" communication protocol based on the publish/subscribe model. The protocol is built on the TCP/IP protocol and was released by IBM in 1999. The biggest advantage of MQTT is that it can provide real-time and reliable message services for connecting remote devices with very little code and limited bandwidth. As an instant messaging protocol with low overhead and low bandwidth usage, it is widely used in the Internet of Things, small devices, mobile applications, etc.

MQTT is a client-server based message publishing/subscription transmission protocol. The MQTT protocol is lightweight, simple, open and easy to implement, which makes it widely applicable. In many cases, including restricted environments such as machine-to-machine (M2M) communication and the Internet of Things (IoT). It has been widely used in sensors communicating via satellite links, medical devices that occasionally dial up, smart homes, and some miniaturized devices.

Latest reply

Study the notes of the big guys, it's awesome, especially the hands-on series, full of useful information  Details Published on 2024-6-6 14:20
 
 

3386

Posts

0

Resources
2
 
This post was last edited by eagle8 on 2020-6-18 17:00

MQTT (Message Queue Telemetry Transport)
is a message protocol based on the publish/subscribe paradigm under the ISO standard (ISO/IEC PRF 20922). It works on the TCP/IP protocol family and is a publish/subscribe message protocol designed for remote devices with low hardware performance and poor network conditions. For this purpose, it requires a message middleware.

Andy Stanford-Clark of IBM and Alan Knipper of Cirrus Link wrote the first version of the protocol in 1999. The usability of the protocol depends on the environment in which it is used. IBM submitted the MQTT 3.1 specification to the Structured Information Standards Promotion Organization in 2013, with a charter to ensure that only minor changes can be made to the specification. MQTT-SN is a variant of the main protocol for embedded devices on non-TCP/IP networks, similar to the ZigBee protocol.

Looking at the development of the industry, the "MQ" in "MQTT" comes from IBM's MQ series message queue product line. However, the queue itself is usually not required to be supported as a standard feature. Optional protocols include Advanced Message Queuing Protocol, Text Oriented Messaging Protocol, Internet Engineering Task Force Constrained Application Protocol, Extensible Messaging and Presence Protocol, Data Distribution Service, OPC UA and Web Application Messaging Protocol.

 
 
 

3386

Posts

0

Resources
3
 

Main principles of MQTT protocol

(1) Streamlined, without adding unnecessary functions;
(2) Pub/Sub mode to facilitate message transmission between sensors;
(3) Allow users to dynamically create topics with zero operation and maintenance costs;
(4) Minimize the transmission volume to improve transmission efficiency;
(5) Take into account factors such as low bandwidth, high latency, and unstable networks;
(6) Support continuous session control;
(7) Understand that the client's computing power may be low;
(8) Provide service quality management;
(9) Assume that data is unknowable, do not force the type and format of transmitted data, and maintain flexibility.

 
 
 

3386

Posts

0

Resources
4
 

Main features of MQTT protocol

The MQTT protocol is designed for communication between remote sensors and control devices on low-bandwidth, unreliable networks. It has the following main features:

(1) Use the publish/subscribe messaging model to provide one-to-many message publishing and decouple applications.

This is very similar to XMPP, but the information redundancy of MQTT is far less than that of XMPP, because XMPP uses XML format text to transmit data.

(2) Message transmission with payload content shielded.

(3) Use TCP/IP to provide network connection.

The mainstream MQTT pushes data based on TCP connection, but there is also a UDP-based version called MQTT-SN. Since these two versions are based on different connection methods, their advantages and disadvantages are naturally different.

(4) There are three types of message publishing service quality:

"At most once", message publishing is completely dependent on the underlying TCP/IP network. Message loss or duplication may occur. This level can be used in the following situations: environmental sensor data, it does not matter if a reading record is lost once, because there will be a second send soon. This method is mainly used for ordinary APP push. If your smart device is not connected to the Internet when the message is pushed, it will not be received when it is pushed, and it will not be received when it is connected to the Internet again.

"At least once" ensures that messages arrive, but message duplication may occur.

"Only once" ensures that the message arrives once. This level can be used in some billing systems with strict requirements. In the billing system, duplicate or lost messages will lead to incorrect results. This highest quality message publishing service can also be used for push notifications of instant messaging apps to ensure that users receive it and only receive it once.

(5) Small transmission, low overhead (fixed-length header is 2 bytes), and minimized protocol exchanges to reduce network traffic.

That is why it is said in the introduction that it is very suitable for "communication between sensors and servers and information collection in the field of Internet of Things". It should be noted that the computing power and bandwidth of embedded devices are relatively weak, and it is more suitable to use this protocol to transmit messages.

(6) A mechanism that uses the Last Will and Testament features to notify relevant parties of abnormal client interruptions.

Last Will: This is the last will mechanism, which is used to notify other devices under the same topic that the device sending the last will has been disconnected.

Testament: A will mechanism that functions similarly to Last Will.

 
 
 

3386

Posts

0

Resources
5
 

MQTT publish and subscribe model
Application layer protocol based on TCP protocol;
adopts C/S architecture;
uses subscription/publish mode to decouple the sender and receiver of messages;
provides 3 types of message QoS (Quality of Service): at most once, at least once, and only once;
sending and receiving messages are asynchronous, and the sender does not need to wait for the receiver to respond.

Compared with traditional message queues, MQTT has the following differences:
1. In traditional message queues, you must create a corresponding queue before sending a message; in MQTT, you do not need to pre-create the topic to be published (subscribeable Topic);
2. In traditional message queues, unconsumed messages are always saved in a queue until a consumer consumes them; in MQTT, if a message is published that is not subscribed to by any client, the message will be thrown away directly;
3. In traditional message queues, a message can only be obtained by one client, in MQTT, a message can be obtained by multiple subscribers, and the MQTT protocol does not support specifying a message to be obtained by a single client.

Advantages of MQTT
1. Simple implementation
2. Provides QoS for data transmission
3. Lightweight and occupies low bandwidth
4. Can transmit any type of data
5. Maintainable session
The MQTT protocol can provide communication security for a large number of low-power IoT devices with unreliable working network environments.

 
 
 

3386

Posts

0

Resources
6
 
This post was last edited by eagler8 on 2020-6-18 18:28

MQTT functional components

MQTT is a publish-subscribe based "lightweight" messaging protocol for use on top of the TCP/IP protocol. It is suitable for connections in remote locations that require a "small code footprint" or have limited network bandwidth. It is a protocol that can implement one-to-many communication (people call it publish or subscribe). It consists of 3 functions, namely broker, publisher, and subscriber. The broker plays the role of a server that forwards MQTT communications. In contrast, publishers and subscribers play the role of clients. The publisher is the client responsible for sending messages, while the subscriber is the client responsible for receiving messages. Messages exchanged by MQTT are accompanied by a "topic" address, and each client regards this "topic" as the receiving address and performs the operation of transmitting messages on it. Figuratively speaking, the broker is the mailbox that receives emails.

 
 
 

3386

Posts

0

Resources
7
 

MQTT communication mechanism
The broker is waiting for each client to connect to it. The subscriber connects to the broker and tells the broker the name of the topic he wants to subscribe to. This is called subscription. Then the publisher connects to the broker and sends a message with the topic as the recipient address. This is publishing. As soon as the publisher publishes the topic, the broker will pass the message to the subscriber who has subscribed to the topic. As shown in the figure, if the subscriber subscribes to topic A, the broker will only pass the message to the subscriber if the publisher publishes topic A. The subscriber and the broker are always connected, while the publisher only needs to establish a connection when publishing, but it needs to maintain the connection when publishing several times in a short period of time. Because the broker plays the role of forwarding messages, it is not necessary for each client to know each other's IP address and other network recipient addresses. And because multiple clients can subscribe to the same topic, the publisher and subscriber are in a one-to-many relationship. In the communication between devices and servers, the device is equivalent to the publisher and the server is equivalent to the subscriber.

 
 
 

2

Posts

1

Resources
8
 

very nice

Comments

Thank you for the encouragement, I am still learning.  Details Published on 2020-6-19 11:32
 
 
 

3386

Posts

0

Resources
9
 

Thank you for the encouragement, I am still learning.

 
 
 

3386

Posts

0

Resources
10
 

Table of Contents
MicroPython Hands-on (01) - I bought a K210 chip AI development board after the Spring Festival
https://bbs.eeworld.com.cn/thread-1115786-1-1.html

MicroPython Hands-on (02) - Try to build the IDE environment of K210 development board
https://bbs.eeworld.com.cn/thread-1115831-1-1.html

MicroPython Hands-on (03) - Learn to start and run MicroPython from scratch
https://bbs.eeworld.com.cn/thread-1116184-1-1.html

MicroPython Hands-on (04) - Basic examples of learning MicroPython from scratch
https://bbs.eeworld.com.cn/thread-1116438-1-1.html

MicroPython Hands-on (05) - Learn MicroPython from scratch - LCD screen
https://bbs.eeworld.com.cn/thread-1116577-1-1.html

MicroPython Hands-on (06) - Learn MaixPy from scratch for monocular camera
https://bbs.eeworld.com.cn/thread-1116591-1-1.html

MicroPython Hands-on (07) - Learn Machine Vision with MaixPy from Scratch
https://bbs.eeworld.com.cn/thread-1116617-1-1.html

MicroPython Hands-on (08) - Learn MaixPy from scratch to recognize colors
https://bbs.eeworld.com.cn/thread-1116662-1-1.html

MicroPython Hands-on (09) - Learn MaixPy face recognition from scratch
https://bbs.eeworld.com.cn/thread-1116720-1-1.html

MicroPython Hands-on (10) - Learn MaixPy Neural Network KPU from scratch
https://bbs.eeworld.com.cn/thread-1116925-1-1.html

MicroPython Hands-on (11) - Building the control board IDE environment
https://bbs.eeworld.com.cn/thread-1117964-1-1.html

MicroPython Hands-on (12) - Hello World on the Control Board
https://bbs.eeworld.com.cn/thread-1118180-1-1.html

MicroPython Hands-on (13) - RGB tricolor light of the control board
https://bbs.eeworld.com.cn/thread-1118275-1-1.html

MicroPython Hands-on (14) - OLED screen of the control board
https://bbs.eeworld.com.cn/thread-1118389-1-1.html

MicroPython Hands-on (15) - AB buttons on the control panel
https://bbs.eeworld.com.cn/thread-1118496-1-1.html

MicroPython Hands-on (16) - Image display of the control board
https://bbs.eeworld.com.cn/thread-1118945-1-1.html

MicroPython Hands-on (17) - Touch pin of control board
https://bbs.eeworld.com.cn/thread-1119462-1-1.html

MicroPython Hands-on (18) - Sound and light sensor of the control board
https://bbs.eeworld.com.cn/thread-1119583-1-1.html

MicroPython Hands-on (19) - Buzzer and music on the control board
https://bbs.eeworld.com.cn/thread-1119668-1-1.html

MicroPython Hands-on (20) - Three-axis acceleration of the control board
https://bbs.eeworld.com.cn/thread-1119998-1-1.html

MicroPython Hands-on (21) - Magnetic Field Sensor of Control Board
https://bbs.eeworld.com.cn/thread-1120188-1-1.html

MicroPython Hands-on (22) - Wireless Broadcasting of Control Board
https://bbs.eeworld.com.cn/thread-1120835-1-1.html

MicroPython Hands-on (23) - WiFi and Bluetooth of the control board
https://bbs.eeworld.com.cn/thread-1120934-1-1.html

MicroPython Hands-on (24) - Extension of the control board and control treasure
https://bbs.eeworld.com.cn/thread-1121208-1-1.html

MicroPython Hands-on (25) - Speech Synthesis and Speech Recognition
https://bbs.eeworld.com.cn/thread-1123752-1-1.html

MicroPython Hands-on (26) - OneNET of the Internet of Things
https://bbs.eeworld.com.cn/thread-1125172-1-1.html

MicroPython Hands-on (27) - WeChat applet for the Internet of Things
https://bbs.eeworld.com.cn/thread-1125520-1-1.html

MicroPython Hands-on (28) - Yeelight of the Internet of Things
https://bbs.eeworld.com.cn/thread-1125939-1-1.html

MicroPython Hands-on (29) - Internet of Things SIoT
https://bbs.eeworld.com.cn/thread-1126090-1-1.html

MicroPython Hands-on (30) - Blynk for the Internet of Things

https://bbs.eeworld.com.cn/thread-1126427-1-1.html

MicroPython Hands-on (31) - Easy IoT

https://bbs.eeworld.com.cn/thread-1126903-1-1.html

MicroPython Hands-on (32) - MQTT for the Internet of Things
https://bbs.eeworld.com.cn/thread-1128464-1-1.html

MicroPython Hands-on (33) - Weather Forecast for the Internet of Things
https://bbs.eeworld.com.cn/thread-1128721-2-1.html

MicroPython Hands-on (34) - Comprehensive application of general sensors
https://bbs.eeworld.com.cn/thread-1129058-1-1.html

 
 
 

1

Posts

0

Resources
11
 

Very detailed. What are the physical media of the MQTT protocol?

Comments

Thank you for your encouragement, I don't quite understand this  Details Published on 2021-9-29 12:33
Thank you for your encouragement, I don't quite understand this  Details Published on 2021-9-29 10:45
 
 
 

6069

Posts

4

Resources
12
 

Not bad. But if you want to play with MQTT, a computer is enough to play with it.

Comments

Thank you for the encouragement, teacher  Details Published on 2021-9-29 10:45
 
 
 

3386

Posts

0

Resources
13
 
Flooded Desert published on 2021-9-27 11:42 Very detailed, what are the physical media of the MQTT protocol?

Thank you for your encouragement, I don't quite understand this

 
 
 

3386

Posts

0

Resources
14
 
damiaa posted on 2021-9-27 15:19 Not bad. But if you play MQTT. A computer is enough to play.

Thank you for the encouragement, teacher

 
 
 

6069

Posts

4

Resources
15
 
Flooded Desert published on 2021-9-27 11:42 Very detailed, what are the physical media of the MQTT protocol?

MQTT is based on TCP. It requires a broker (message proxy server), a publisher client, and a subscriber client. The publisher publishes information, and the subscriber receives information.

It can be deployed on a computer or other device with TCP. It can also be deployed on a cloud server.

Generally, brokers are deployed on computers or cloud servers. They can also be deployed on Linux devices, such as Raspberry Pi.

The publisher subscriber can be in the computer or in the device.

 
 
 

1

Posts

0

Resources
16
 
Very valuable to learn, thank you for sharing

Comments

Good morning, thank you for your encouragement.  Details Published on 2022-6-4 05:58
 
 
 

3386

Posts

0

Resources
17
 
Zick2222 posted on 2022-5-31 08:53 Very valuable for learning, thank you for sharing

Good morning, thank you for your encouragement.

 
 
 

25

Posts

3

Resources
18
 

Learned~

Comments

Thanks for the encouragement  Details Published on 2022-8-31 12:22
 
 
 

3386

Posts

0

Resources
19
 

Thanks for the encouragement

 
 
 

686

Posts

0

Resources
20
 

This is a post from last year. I think I've seen it before...

However, UDP is popular again this year. HTTP3 solves the packet loss problem of UDP. I wonder if it can be used on IoT devices.

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list