MQTT framework and usage

Table of contents

MQTT framework

1. MQTT Overview

1.1 Visually understand the three roles

1.2 Delivery of messages

2. Experience MQTT on Windows

2.1 Install APP

2.2 Start the server

2.3 Using MQTTX

2.3.1 Establish connection

2.3.2 Subscribing to topics

2.3.3 Publishing topics

2.4 Using mosquitto

2.4.1 Publish news

2.4.2 Subscribe to messages

3. kawaii-mqtt source code analysis

3.1 Use

3.2 kawaii-mqtt internal implementation

3.2.1 Main code

3.2.2 The processing function is recorded in the linked list

3.2.3 Flowchart


MQTT framework

References:

1. MQTT Overview

1.1 Visually understand the three roles

The schematic diagram of the MQTT communication model is as follows:

It is easier to understand using the analogy of the three roles of TV station, reporter, and audience:

  • TV station: It is called a server (broker) in MQTT and has the following functions

    • Accept network connections from clients; // Reporters/viewers connect to the TV station

    • Accept application information released by customers; //Accept information released by reporters

    • Process subscription and unsubscribe requests from the client; // Process subscribe and unsubscribe requests from viewers

    • Forward application messages to subscribed customers // Forward news reported by reporters to viewers

  • Reporters and viewers are both clients. Reporters can also be viewers, and viewers can also be reporters. They have the following functions:

    • Publish information; // publish, reporter

    • Subscribe to the message; // Subscribe, viewer

    • Unsubscribe or delete messages;

    • Disconnect from server

1.2 Delivery of messages

Let’s take daily life as an example and ask a few questions:

  • Audience: I only care about financial news, so I only subscribe to "Financial News" and not "Sports News"

  • Reporter: I am a financial reporter. I can publish "financial news" but not "sports news".

In this process, two concepts are introduced:

  • Topic: Is it financial? Or sports?

  • Message or Playload: specific news information

The specific process is as follows:

  • Viewers call the TV station: connect

  • Viewers subscribe to "financial news" from the TV station: Subscribe to a Topic

  • The reporter called the TV station: connect

  • The reporter released "financial news" to the TV station: a certain Playload of a certain topic in Public

  • The TV station publishes "a certain message" to "audiences who have subscribed to financial news": Public a certain Playload to Subscriber

During the entire process, the direct phone calls between the TV station and reporters, and the TV station and viewers need to save the connection status and confirm from time to time:

  • Reporters should shout "Hello" to the TV station from time to time to ensure that the TV station is still functioning normally.

  • Audiences should shout "Hello" to the TV station from time to time to ensure that the TV station is still functioning properly.

2. Experience MQTT on Windows

2.1 Install APP

Install these 2 apps:

2.2 Start the server

Use the DOS command line to enter the installation directory of mosquitto-2.0.14-install-windows-x64 and execute the command:

cd  "c:\Program Files\mosquitto"
.\mosquitto.exe -v

In the following experiment, whether using MQTTX or mosquitto_pub/mosquitto_sub, keep mosquitto.exe running.

2.3 Using MQTTX

2.3.1 Establish connection

After running MQTTX, proceed as shown below:

2.3.2 Subscribing to topics

After establishing the connection, proceed as shown below:

2.3.3 Publishing topics

Proceed as follows:

2.4 Using mosquitto

2.4.1 Publish news

Parameter Description:

mosquitto_pub 命令参数说明
1. -d  打印debug信息
2. -f  将指定文件的内容作为发送消息的内容
3. -h  指定要连接的域名  默认为localhost
4. -i  指定要给哪个clientId的用户发送消息
5. -I  指定给哪个clientId前缀的用户发送消息
6. -m  消息内容
7. -n  发送一个空(null)消息
8. -p  连接端口号
9. -q  指定QoS的值(0,1,2)
10. -t  指定topic
11. -u  指定broker访问用户
12. -P  指定broker访问密码
13. -V  指定MQTT协议版本
14. --will-payload  指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与--will-topic一起使用
15. --will-qos  Will的QoS值。该参数需要与--will-topic一起使用
16. --will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与--will-topic一起使用
17. --will-topic  用户发送Will消息的topic

Use the DOS command line to enter the installation directory of mosquitto-2.0.14-install-windows-x64 and execute the command:

cd  "c:\Program Files\mosquitto"
.\mosquitto_pub.exe -h 127.0.0.1 -p 1883  -t "100ask"
As shown in the picture:

2.4.2 Subscribe to messages

Parameter Description:

mosquitto_sub 命令参数说明
1. -c  设定‘clean session’为无效状态,这样一直保持订阅状态,即便是已经失去连接,如果再次连接仍旧能够接收的断开期间发送的消息。
2. -d  打印debug信息
3. -h  指定要连接的域名  默认为localhost
4. -i 指定clientId
5. -I 指定clientId前缀
6. -k keepalive 每隔一段时间,发PING消息通知broker,仍处于连接状态。 默认为60秒。
7. -q 指定希望接收到QoS为什么的消息  默认QoS为0
8. -R 不显示陈旧的消息
9. -t 订阅topic
10. -v 打印消息
11. --will-payload  指定一个消息,该消息当客户端与broker意外断开连接时发出。该参数需要与--will-topic一起使用
12. --will-qos  Will的QoS值。该参数需要与--will-topic一起使用
13. --will-retain 指定Will消息被当做一个retain消息(即消息被广播后,该消息被保留起来)。该参数需要与--will-topic一起使用
14. --will-topic  用户发送Will消息的topic

Use the DOS command line to enter the installation directory of mosquitto-2.0.14-install-windows-x64 and execute the command:

3. kawaii-mqtt source code analysis

3.1 Use

A few pieces of code using MQTT:

void my_message_handler_t(void* client, message_data_t* msg)
{
}

int main(void)
{
    int err;
    mqtt_client_t *client = NULL;

    err = mqtt_connect(client);

    err = mqtt_subscribe(client, "100ask-topic", QOS0, my_message_handler_t);
    
    while (1);
}

From the above code, ask 2 questions:

Answer:

3.2 kawaii-mqtt internal implementation

3.2.1 Main code

The internal processing of kawaii-mqtt is processed using the mqtt_yield_thread thread:

The main function is mqtt_yield:

The core function in mqtt_yield is the processing of data packets: mqtt_packet_handle

3.2.2 The processing function is recorded in the linked list

There are 2 linked lists in the mqtt_client structure:

When the MQTT Client sends certain data packets to the Broker, it expects a response (ACK): a timer will be started. If the timer times out, it means no ACK is received:

  • Or resend

  • Either something goes wrong

  • For ACK packets, there is generally no need to provide a processing function

When subscribing to a topic, MQTT Client will send out a SUBCRIBE packet and expect a response packet: SUBACK packet. code show as below:

mqtt_subscribe
    msg_handler = mqtt_msg_handler_create(topic_filter, qos, handler);
    rc = mqtt_ack_list_record(c, SUBACK, packet_id, len, msg_handler);
            /* create a ack handler node */
            ack_handler = mqtt_ack_handler_create(c, type, packet_id, payload_len, handler);
                platform_timer_cutdown(&ack_handler->timer, c->mqtt_cmd_timeout); 
            mqtt_list_add_tail(&ack_handler->list, &c->mqtt_ack_handler_list);

If the SUBACK packet is not received within the specified time, the handler will be deleted from mqtt_ack_handler_list.

If you receive a queue SUBACK packet, you need to do two things:

  • Delete the handler in mqtt_ack_handler_list

  • Put the handler in mqtt_msg_handler_list: this handler will be called when the PUBLISH data packet is received in the future.

3.2.3 Flowchart

Guess you like

Origin blog.csdn.net/m0_74712453/article/details/134772420