MQTT introductory tutorial (a)

Getting introduced MQTT

A brief

MQTT (Message Queuing Telemetry Transport, Message Queuing Telemetry Transport Protocol), is based publish / subscribe "lightweight" communication protocol (publish / subscribe) mode, the agreement built on TCP / IP protocol, by IBM in 1999 on release. MQTT biggest advantage is that the code may be minimal and limited bandwidth for real-time remote devices to provide reliable messaging service. As a low-cost, low bandwidth usage of instant messaging protocol, so there is more widely used in things, small equipment, mobile applications.

MQTT is based on a client - server message publish / subscribe transport protocol. MQTT agreement is lightweight, simple, open and easy to implement, these features make it suitable for a very wide range. In many cases, including constrained environments, such as: machine to machine (M2M) communication and Internet of Things (IoT). Communication by satellite link sensors, occasional dial-up medical equipment, intelligent home, and some of the smaller devices have been widely used.

 

 

Second, the design specifications

Because of things is a very special environment, so MQTT following design principles:

  • (1) streamlined, without the addition of non-essential functions;
  • (2) publish / subscribe (Pub / Sub) mode, to facilitate messaging between the sensors;
  • (3) allows users to dynamically create a theme, zero operation and maintenance costs;
  • (4) to transfer the amount to a minimum in order to improve transmission efficiency;
  • (5) the low bandwidth, high latency, an unstable network and other factors into account;
  • (6) supports continuous session control;
  • (7) the ability to understand the client computing may be low;
  • (8) provide the quality of service management;
  • (9) assuming agnostic data, not force types and format of data transfer, maintaining flexibility.

Third, the main characteristics

MQTT communication protocol operating at the remote sensors and control devices low bandwidth, unreliable network protocol design, which has the following several major characteristics:

  • (1) using the publish / subscribe messaging model, providing many message distribution decouples applications.

    This is similar to XMPP, but the information redundancy MQTT much smaller than the XMPP ,, because XMPP uses XML format to transfer text data.

  • (2) load the contents of message transmission mask.

  • (3) using the TCP / IP provides a network connection.

    Mainstream MQTT is based on TCP connection for data push, but also has a UDP-based version, called MQTT-SN. Because the two versions based on different connections, each with different strengths and weaknesses naturally.

  • (4) There are three Quality of Service news release:

    "At most once", the news release completely dependent on the underlying TCP / IP network. Occur missing or duplicate messages. This level can be used in the following cases, the environmental sensor data, loss of a read record does not matter, because there will be sent a second time soon after. This is a way to push major general of APP, if your smart device is offline when the message push, push past did not receive, will not receive the network again.

    "At least once" to ensure that the message reaches, but the message is repeated may occur.

    "Only once" to ensure that the message reaches once. In some of the more stringent requirements of the billing system, you can use this level. In the billing system, the message is repeated or missing will lead to incorrect results. This news release highest quality service can also be used to push APP instant communications, ensuring that users receive and will receive only once.

  • (5) a small transmission overhead is small (fixed-length header is 2 bytes), a protocol exchange minimized to reduce network traffic.

    This is why it is very suitable for presentation in the "Internet of things in the field of communications, information-collecting sensor with the server," to know that computing power and bandwidth of embedded devices is relatively weak, the use of this protocol to deliver the message again for enough.

  • (6) using the Last Will and Testament characteristics to inform interested parties mechanisms client aborted.

    Last Will: That is testament mechanism for other devices to send the last words of the same subject under the notification device has been disconnected.

    Testament: the mechanism will function similar to the Last Will.

Four, MQTT principle agreement

4.1 MQTT protocol implementation

MQTT achieve agreement requires the client and server communication is completed, in the communication process, there are three protocols MQTT identity: the publisher (Publish), agent (Broker) (server), subscribers (Subscribe). Among them, news publishers and subscribers are clients, messages proxy server, news release may be subscribers simultaneously.

MQTT transmitted into message: topic (Topic) and a load (payload) in two parts:

  • (1) Topic, can be understood as the type of message, after subscriber subscription (Subscribe), you will receive the message content that topic (payload);
  • (2) payload, can be understood as the content of the message, the content refers to specific subscribers want to use.

4.2 application message network transmission and

MQTT builds the underlying network transport: it establishes the connection from client to server, providing an orderly therebetween, and lossless, two-way transmission based byte stream.

When the application data sent over the network MQTT, MQTT will associated with quality of service (QoS) and topic name (Topic) even relevant.

4.3 MQTT client

A use application or device MQTT agreement, it is always to establish a network connection to the server. Clients can:

  • (1) publish information about other clients may subscribe;
  • (2) Subscribe to news release of other clients;
  • Message (3) to unsubscribe or removing applications;
  • (4) is disconnected from the server.

4.4 MQTT server

MQTT server called "Message Agent" (Broker), may be an application or a device. It is located on the news release and subscribers between, it can:

  • (1) receiving a network connection from a client;
  • (2) customer acceptance of the application of information released;
  • (3) Processing subscribe and unsubscribe request from a client;
  • (4) forwarding application messages to subscribed customers.

4.5 MQTT agreement to subscribe, subject, conversation

First, the subscription (Subscription)

Subscribe to topics that contain filters (Topic Filter) and maximum quality of service (QoS). Subscriptions will be associated with a session (Session). A session can contain multiple subscriptions. Each session Each subscription has a different theme filters.

Second, the session (Session)

Each client and server to establish the connection is a session, stateful interaction between the client and the server. A session exists between the network, or across multiple continuous network connection between the client and the server.

Third, the subject name (Topic Name)

Coupled to a label application message, which the subscriber tag matches. Each client server sends the message to subscribe to the tag end of the match.

Fourth, subject filters (Topic Filter)

A wildcard on the subject name filter, use a subscription expression, represents more than one topic to the match.

Fifth, the load (Payload)

The particular message subscriber received content.

4.6 MQTT protocol method

MQTT protocol defines methods (also referred to action), is determined by representation of resources in the operation. This resource data can represent pre-existing or dynamically generated data, depending on the implementation of the server. Generally speaking, it refers to resource files or output on the server. The main methods are:

  • (1) Connect. Waiting to establish a connection to the server.
  • (2) Disconnect. MQTT wait for the client to complete the work, and disconnected from the server TCP / IP session.
  • (3) Subscribe. Waiting to complete the subscription.
  • (4) UnSubscribe. Waiting for the server to cancel one or more clients subscribe to topics.
  • (5) Publish. MQTT client sends a request message, the thread returns to the application program completion of the transmission.

Five, MQTT protocol packet structure

In MQTT protocol, a packet consists MQTT: fixed header (Fixed header), the first variable (Variable header), message body (payload) of three parts. MQTT packet structure is as follows:

  • (1) fixing head (Fixed header). MQTT present in all packets, the packet class identifier represents the packet type and the packet.
  • (2) the variable head (Variable header). MQTT present in the portion of the packet, the packet type determines whether there is a variable header and the specific contents.
  • (3) the message body (Payload). MQTT present in the portion of the packet, a specific content received by the client.

5.1 MQTT Glands

MQTT glands are present in all packets, the following structure:

5.1.1 MQTT packet types

Position: Byte 1 in bits 7-4.

Phase in a 4-bit unsigned value, type, and values ​​described as follows:

5.1.2 flag

Position: Byte 1 in bits 3-0.

Without the use of message type flag, the flag is used as a reserved bit. If the invalid flag is received, the receiver must off a network connection:

(1) DUP: distribute copies of the message. It is used to ensure reliable transmission of the message, if set to 1, is increased in the following MessageId longer, and requires acknowledgment reply to the message to ensure that the transfer is complete, but not for detection of repeatedly transmitted message.

(2) QoS: Quality of Service news release, namely: to ensure that the number of message delivery

Ø00: most once, namely: <= 1

Ø01: at least once, namely: > = 1

Ø10: once,: = 1

Ø11: reserved

(3) RETAIN: Published keeping identities, indicates that the server you want to keep the information push, if there are new subscribers appeared, took the news pushed to it, if it has pushed back the release of current subscribers. 5.1.3 remaining length (Remaining Length)

Address: Byte 2.

The second byte fixed header length variable which stores the total size of the message header and body, but not directly saved. This byte is extended, save mechanism, for storing the length of the first 7 bits, one is used as the identifier. When the last bit is 1, it indicates insufficient length, it is necessary to use two bytes continue to save. For example: to calculate the following size 0

5.2 MQTT variable head

MQTT variable packet contains a header, which is located in between the fixed head and the load. SUMMARY variable header by packet type is different, as is more often the application packet identifier:

Many types of data packet includes a 2-byte packet identification field, these types of packages have: PUBLISH (QoS> 0), PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK.

5.3 Payload message body

Part MQTT position Payload message packet, comprising CONNECT, SUBSCRIBE, SUBACK, UNSUBSCRIBE four types of message:

  • (1) CONNECT, the main content of the message body: Client ClientID, subscribe to the Topic, Message, and user name and password.
  • (2) SUBSCRIBE, message body content is to subscribe to a series of themes and QoS.
  • (3) SUBACK, message body content server to identify and respond to the themes and QoS SUBSCRIBE applied for.
  • (4) UNSUBSCRIBE, message body content to subscribed threads.

 

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/12446680.html