Follow,Star Public Account to not miss exciting content
Source: Embedded Miscellaneous
Although my practical development experience with Linux is still limited, I am aware that there are several methods for inter-process communication: pipes, message queues, shared memory, sockets, etc.
In a certain project, MQTT was used as the method for inter-process communication, which felt quite novel, and it seems that there are not many examples online of using MQTT for this purpose. In these two notes, we will learn about this method together.
Introduction to MQTT
❝
The following introduction is from the book “[Wildfire] Practical Guide to LwIP Application Development”
❞
The full name of the MQTT protocol
is Message Queuing Telemetry Transport, which translates to Message Queue Telemetry Transport Protocol
. It is a commonly used application layer protocol
in the Internet of Things, operating at the application layer of TCP/IP, relying on the TCP protocol
, thus it has very high reliability
. It is a lightweight protocol based on the TCP
1. MQTT Communication Model
The MQTT protocol is based on the client-server model
, and there are mainly three identities in the protocol: Publisher
, Broker
, and Subscriber
. Moreover, a message publisher can also be a subscriber.
Both the publisher and subscriber of MQTT messages are clients, while the server acts merely as an intermediary, forwarding messages published by the publisher to all subscribers of that topic.
Functions of the MQTT client:
- Publish messages to other related clients.
- Subscribe to topics to receive related application messages.
- Unsubscribe from topics to stop receiving application messages.
- Terminate connection from the server.
The MQTT server is commonly referred to as the Broker (message broker). Its functions include:
-
Accepting network connection requests from clients.
-
Accepting application messages published by clients.
-
Handling subscription and unsubscription requests from clients.
-
Forwarding application messages to subscribed clients that meet the criteria (including the publisher itself).
2. MQTT Messages
Messages sent by MQTT consist of: topic + content
. Clients can subscribe to any topic, and if other clients publish a topic that matches the subscribed topic, the gateway will send it to the client.
“What is a topic?”
The MQTT server adds a label for each connected client (subscriber), which matches all subscriptions in the server. The server will forward messages to each client that matches the label. This label is called a topic.
“Quality of Service:”
MQTT provides three levels of Quality of Service (QoS) for developers to choose from based on different scenarios:
-
QoS0: At most once delivery, where the receiver does not send a response after the message is sent, and the sender does not resend the message.
-
QoS1: At least once delivery (the message must be delivered at least once, but may be delivered multiple times). The PUBLISH message of QoS 1 contains a message identifier in its variable header, requiring a PUBACK message for acknowledgment.
-
QoS2: This is the highest level of service quality, where message loss and duplication are unacceptable. However, using this level incurs additional overhead, and it is commonly used in payments, as payments must be successful exactly once; it cannot be that the payment is not made or made multiple times.
Using Mosquitto
1. Introduction to Mosquitto
Mosquitto is an open-source MQTT message broker (server) software that provides a lightweight, publish/subscribe message-pushing model, making short message communication between devices simple, such as widely used low-power sensors, mobile devices like phones, embedded computers, microcontrollers, etc.
Mosquitto repository address:
❝
https://gitee.com/zhengnianli/mosquitto
❞
or
❝
https://github.com/eclipse/mosquitto
❞
2. Mosquitto Practice
Download the Mosquitto source code from the above links, and you will get:
In the Mosquitto directory, enter the following commands to compile:
mkdir build
cd build
cmake ../
make
If cmake is not installed, you need to install it yourself. You can refer to previous articles: “Interviewer: How to Compile C Programs in Linux?”
If you encounter the Could NOT find OpenSSL
issue when executing the cmake ../
command:
You can install OpenSSL with the following command (on Ubuntu):
❝
sudo apt-get install libssl-dev
❞
After executing the make command, we can see that some executable files will be generated in the client
and src
folders under the build directory:
We will focus on the three executable files: mosquitto_pub
, mosquitto_sub
, and mosquitto
. Among them, mosquitto
is the server software, mosquitto_pub
is the publisher client, and mosquitto_sub
is the subscriber client.
Next, let’s do a simple test:
Here, the source file corresponding to mosquitto_pub
is mosquitto/client/pub_client.c
, and the source file corresponding to mosquitto_sub
is mosquitto/client/sub_client.c
. Interested friends can read and learn on their own.
This example is essentially communication between two processes, with the prerequisite being a local broker server. If you need to apply it to inter-process communication in our embedded Linux, you need to use a cross-compiler to compile a mosquitto
server that can run on our ARM board.
Recommended Reading:
Step-by-Step Guide to Generating Encrypted Firmware with STM32Trust
The Role and Significance of the do{} while(0U) Macro Definition in HAL Library
How FreeRTOS Reduces RAM Usage and Increases Execution Speed
Follow the WeChat public account “strongerHuang”, reply “1024” to see more content, reply “Join Group” to join the technical exchange group according to the rules.
Long press to go to the public account included in the image to follow