QtMQTT is a Qt-based client library that enables MQTT protocol communication with brokers, commonly used in embedded and IoT scenarios. Kylin Linux—a domestic Chinese OS derived from Ubuntu—is widely deployed in secure industrial and edge environments. This guide walks through building, testing, and system-integrating QtMQTT on Kylin Linux (v10 or later).
1. Prerequisites and Libray Setup
QtMQTT requires Qt 5.12+ and the Qt Network module. Ensure development headers and tools are installed:
sudo apt update
sudo apt install qt5-qmake qtbase5-dev libqt5network5-dev build-essential git cmake
Note: Kylin Linux may use kylin-desktop or kylin-server repositories—verify package availability with apt-cache search qt5 if needed.
2. Building QtMQTT from Source
Since QtMQTT was officially integrated into Qt starting with Qt 6.2, and Kylin typically ships with Qt 5.x, we build the standalone Qt 5-compatible version:
git clone https://github.com/qt-mqtt/qt-mqtt.git
cd qt-mqtt
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr ..
make -j$(nproc)
sudo make install
This installs headers to /usr/include/QtMqtt, libraries to /usr/lib/x86_64-linux-gnu/, and configures pkg-config support.
3. Minimal Publisher Example
Create mqtt_publisher.cpp:
#include <QCoreApplication>
#include <QtMqtt/MqttClient>
#include <QTimer>
#include <QDebug>
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
auto client = new QMqttClient(&app);
client->setHostname("localhost");
client->setPort(1883);
QObject::connect(client, &QMqttClient::connected, [&]() {
qDebug() << "Connected to broker";
QTimer::singleShot(1000, [&]() {
client->publish("kylin/sensor/status", "online", 1, true);
});
});
QObject::connect(client, &QMqttClient::disconnected, [&]() {
qDebug() << "Broker connection lost";
QCoreApplication::exit(1);
});
client->connectToHost();
return app.exec();
}
Generate project file mqtt_publisher.pro:
QT += core mqtt
CONFIG += c++17 console
SOURCES += mqtt_publisher.cpp
TARGET = mqtt_publisher
TEMPLATE = app
Build and run:
qmake && make
./mqtt_publisher
4. Systemd Service Deployment
To run the MQTT client as a persistent background service, create /etc/systemd/system/mqtt-publisher.service:
[Unit]
Description=Kylin MQTT Sensor Publisher
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=kylin-user
WorkingDirectory=/opt/mqtt-apps
ExecStart=/opt/mqtt-apps/mqtt_publisher
Restart=on-failure
RestartSec=5
Environment="QT_QPA_PLATFORM=offscreen"
[Install]
WantedBy=multi-user.target
Then enable and start it:
sudo systemctl daemon-reload
sudo systemctl enable mqtt-publisher.service
sudo systemctl start mqtt-publisher.service
Verify status and logs:
systemctl status mqtt-publisher.service
journalctl -u mqtt-publisher.service -f
5. Broker Setup (Optional but Recommended)
For local testing, install Mosquitto:
sudo apt install mosquitto mosquitto-clients
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
Subscribe to verify messages:
mosquitto_sub -t "kylin/sensor/#" -v