Getting started with the MQTT protocol: Get started easily and quickly master the core points


Insert image description here

What is MQTT?

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe model-based message transmission protocol, suitable for resource-constrained devices and low-bandwidth, high-latency or unstable network environments. It is popular in IoT applications, enabling efficient communication between sensors, actuators, and other devices.

How MQTT works

To understand how MQTT works, you first need to master the following concepts:MQTT client, MQTT Broker, Publish-Subscribe Mode, Topic , QoS.

MQTT client

Any application or device running MQTT client library is an MQTT client. For example, instant messaging applications that use MQTT are clients, various sensors that use MQTT to report data are clients, and various MQTT testing tools are also clients.

MQTT Broker

MQTT Broker is a key component responsible for processing client requests, including operations such as establishing connections, disconnecting, subscribing and unsubscribing, and is also responsible for forwarding messages. An efficient and powerful MQTT Broker can easily handlemassive connections and million-level message throughput, thereby helping IoT service providers focus on business development and quickly Build reliable MQTT applications.

publish-subscribe model

The difference between the publish-subscribe model and the client-server model is that it separates the client that sends the message (publisher) and the client that receives the message (subscriber)Decoupling. There is no need to establish a direct connection between publishers and subscribers, but the MQTT Broker is responsible for routing and distribution of messages.

The following figure shows the MQTT publish/subscribe process. The temperature sensor connects to the MQTT Broker as a client and publishes temperature data to a specific topic (such as Temperature) through a publish operation. After receiving the message, MQTT Broker will be responsible for forwarding it to the subscriber client subscribed to the corresponding topic (Temperature).
Insert image description here

theme

The MQTT protocol forwards messages based on topics. Topics are distinguished by /, similar to URL paths, for example:

chat/room/1

sensor/10/temperature

sensor/+/temperature

MQTT topics support the following two wildcard characters: + and #.

  • +: Indicates a single-level wildcard, for example, a/+ matches a/x or a/y.
  • #: Indicates multi-level wildcards, for example, a/# matches a/x, a/b/c/d.

Note: Wildcard topics can only be used for subscriptions, not publications.

QoS

MQTT provides three qualities of service (QoS) to ensure message reliability in different network environments.

  • QoS 0: Messages are delivered at most once. If the current client is unavailable, it will lose this message.
  • QoS 1: Message is delivered at least once.
  • QoS 2: Messages are delivered only once.

MQTT workflow

After understanding the basic components of MQTT, let's take a look at its general workflow:

  1. The client uses the TCP/IP protocol to establish a connection with the Broker, and can optionally use TLS/SSL encryption to achieve secure communication. The client provides authentication information and specifies the session type (Clean Session or Persistent Session).
  2. Clients can either publish messages to a specific topic or subscribe to a topic to receive messages. When a client publishes a message, it sends the message to the MQTT Broker; and when the client subscribes to a message, it receives messages related to the subscribed topic.
  3. MQTT Broker receives published messages and forwards these messages to clients subscribed to the corresponding topic. It ensures reliable delivery of messages based on QoS levels and stores messages for disconnected clients based on session type.

Getting started with MQTT: A quick tutorial

Below we will show you how to use MQTT through some simple examples. Before starting, you need to prepare MQTT Broker and MQTT client.

Prepare MQTT Broker

You can build your own MQTT Broker by choosing between a private deployment or a fully managed cloud service. Or you can use a free public broker.

  • Private deployment
    EMQX is the most scalable open source MQTT Broker, suitable for IoT, Industrial IoTConnect with the Internet of Vehicles. You can run the following Docker command to install EMQX.
docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx
  • Fully managed cloud service
    Starting the MQTT service through a fully managed cloud service is the most convenient way. As shown in the image below, EMQX Cloud can be launched in minutes and has operational support in 17 regions across AWS, Google Cloud, and Microsoft Azure.

Insert image description here

Server: broker.emqx.io

TCP Port: 1883

WebSocket Port: 8083

SSL/TLS Port: 8883

Secure WebSocket Port: 8084

Prepare MQTT client

In this article, we will use the MQTT client tool provided by MQTTX that supports browser access. The access address is http://www.emqx.io/online-mqtt-client. MQTTX also provides a desktop client and a command line tool.

MQTTX is a cross-platform MQTT 5.0 desktop client that can run on macOS, Linux, and Windows operating systems. Its user-friendly chat-like interface enables users to easily create multiple MQTT/MQTTS connections and subscribe and publish MQTT messages.

Insert image description here

Currently, various programming languages ​​have mature open source MQTT client libraries. We've curated MQTT client libraries for multiple programming languages ​​in popular MQTT client libraries and SDKs, and provided detailed code examples to help you quickly understand the use of MQTT clients.

Create an MQTT connection

Before communicating using the MQTT protocol, the client needs to create an MQTT connection to connect to the Broker.

Open http://www.emqx.io/online-mqtt-client in the browser, click the New Connection button in the middle of the page, and you will see the following page.

Insert image description here

We enter Simple Demo in Name, and then click the Connect button in the upper right corner to establish an MQTT connection. As shown in the figure below, the connection is successful.

Insert image description here

Subscribe to topics via wildcard

Next, we subscribe to the topic via wildcard in the Simple Demo connection created above, so that we can receive the temperature data sent by all sensors. sensor/+/temperature

As shown in the figure below, click the New Subscription button and enter the topic in the Topic field in the pop-up box. QoS remains at the default value of 0. sensor/+/temperature

Insert image description here

After the subscription is successful, you will see a new record in the middle of the subscription list.

Insert image description here

Publish MQTT messages

Next, we click the + button on the left menu to create two connections, named Sensor 1 and Sensor 2, used to simulate two temperature sensors.

Insert image description here

After the connection is successfully created, you will see three connections, and the online status indicator on the left side of each connection is green.

Insert image description here

SelectSensor 1 to connect, entersensor/1/temperature in the publishing topic at the bottom of the page, enter the following JSON format message in the message box, and then click on the lower right The publish button sends a message.

{
    
    
  "msg": "17.2"
}

Insert image description here

As shown in the figure below, the message was sent successfully.

Insert image description here

Using the same steps, publish the following JSON message in the Sensor 2 connection to the sensor/2/temperature topic.

{
    
    
  "msg": "18.2"
}

You'll see that the Simple Demo connection received two new messages.

Insert image description here

Click on the Simple Demo connection and you will see two messages sent by the two sensors.

Insert image description here

MQTT function demonstration

keep message

When an MQTT client publishes a message to the server, the retain message flag can be set. Persistent messages are stored on the message server and subsequent clients subscribing to the topic can still receive the message.

As shown in the figure below, we check the option in the Sensor 1 connection, and then send two message. Retainretained_message

Insert image description here

Next, we subscribe to the topic in the Simple Demo connection. After the subscription is successful, you will receive the second retained message sent by , which means that the server will only retain the most recent retained message for the topic. retained_messageSensor 1

Insert image description here

Clean Session

MQTT clients usually can only receive messages published by other clients while online. If a client goes offline and then comes back online, it will not receive messages from the offline period.

However, if the client connects with Clean Session set to false and comes online again using the same client ID, the message server will cache a certain number of offline messages for the client and send them to it when it comes back online.

The public MQTT server used in this demonstration is set up to cache offline messages for 5 minutes, with a maximum message count of 1000, and does not save QoS 0 messages.

Below, we create an MQTT 3.1.1 connection and use QoS 1 to demonstrate the use of Clean Session.

In MQTT 5.0, Clean Session is split into Clean Start and Session Expiry Interval.

Create a connection named MQTT V3, set Clean Session to false, and select MQTT version 3.1.1.

Insert image description here

After the connection is successful, subscribe to the clean_session_false topic and set QoS to 1.

Insert image description here

After the subscription is successful, click the disconnect button in the upper right corner to disconnect.

Insert image description here

Then, create a connection named MQTT_V3_Publish with the MQTT version set to 3.1.1. After the connection is successful, publish three messages to the clean_session_false topic.

Insert image description here

Next, select MQTT_V3 to connect, click the Connect button to reconnect to the server, and you will receive three offline messages.

Insert image description here

Will message

When an MQTT client initiates a CONNECT request to the server, it can choose whether to send a will message flag and specify the subject and payload of the will message.

If the MQTT client goes offline abnormally (without sending a DISCONNECT message to the server before disconnecting), the MQTT server publishes a will message.

We create a connection named Last Will to demonstrate this functionality. To see the effect quickly, we set Keep Alive to 5 seconds.

  • Last-Will Topic is set to last_will.
  • Last-Will QoS 设置为 1
  • Last-Will Retain is set to true.
  • Last-Will Payload 设置为 offline

Insert image description here

After the connection is successful, we disconnect the computer network for more than 5 seconds (simulating the client's abnormal disconnection), and then restore the network.

Then start the Simple Demo connection and subscribe to the last_will topic. You will receive a Will message for the Last Will connection setup.

Insert image description here
This article introduces the basic concepts and usage process of MQTT in detail. You can try to use the MQTT protocol according to what you have learned in this article.

Guess you like

Origin blog.csdn.net/Qingai521/article/details/134805962