한국어
Qt
 

Qt MQTT 를 사용해보기 위해 우분투 데스크탑에 빌드환경을 구축하고 예제를 실행해본다. Qt MQTT는 커머셜 라이센스 및 GPL 로 사용할 수 있으며 커머셜 라이센스를 보유하고 있다면 MaintenanceTool 을 통해 간단하게 설치할 수 있다.

여기서는 오픈소스 라이센스하에서 사용할 것이므로 직접 소스코드를 빌드하여 빌드환경을 구축한다. 기본적으로 Qt가 설치되어 있어야하며 여기서는 이 과정 (Qt 설치)을 생략한다.
 

Qt MQTT 구축

다음은 git저장소를 클론하고 빌드하는 과정을 보여준다.
$ git clone https://code.qt.io/qt/qtmqtt.git
$ cd qtmqtt
$ git checkout 5.12.9
$ ~/Qt/5.12.5/gcc_64/bin/qmake
$ make
$ make install
 
이제 프로젝트 파일(.pro)에 다음 줄을 추가하면 mqtt 모듈을 사용할 수 있다.
QT    += mqtt
 
클래스를 사용하기 위해 관련 (클라이언트)헤더를 포함한다.
#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttMessage>
#include <QtMqtt/QMqttSubscription>
 
클라이언트 객체 생성과 호스트 연결
QMqttClient *client = new QMqttClient();
client->setHostname("Host Name");
client->setPort(1883);
 
클라이언트 객체는 다음과 같은 유용한 시그널을 제공한다.
QMqttClient::stateChanged
QMqttClient::disconnected
QMqttClient::messageReceived
QMqttClient::pingResponseReceived
 
호스트에 연결
if(client->state() == QMqttClient::Disconnected)
    client->connectToHost();
 
토픽 구독
int qos = 0;
QMqttSubscription *subscription = client->subscribe(QMqttTopicFilter("topic"), qos);

 

 

구독이 성공하면 다음 시그널에 연결하여 메세지를 수신할 수 있다.

QMqttSubscription::messageReceived
QMqttSubscription::stateChanged
QMqttSubscription::qosChanged
 
구독을 취소하려면 다음과 같이 할 수 있다.
subscription->unsubscribe();
 
토픽의 메세지를 게시하면 해당 토픽을 구독하는 클라이언트에게 메세지를 전달한다.
client->publish(QMqttTopicName("topic"), "message");

 

pub/sub 예제

이제 간단한 pus/sub예제를 실행 해본다.
브로커는 라즈베리파이4에 mosquitto 를 설치하여 구축한다. 다음의 명령줄로 간단하게 설치할 수 있다.
pi@raspberrypi:~ $ sudo apt install mosquitto
 
다음은 우분투 데스크탑에서 실행되는 간단한 클라이언트 예제를 보여준다.
#include <QCoreApplication>
#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttMessage>
#include <QtMqtt/QMqttSubscription>

#include <QDebug>

void subscribe(QMqttClient *client){
    QString topic("sensors/house/temperature");

    int qos = 0;
    QMqttSubscription *subscription = client->subscribe(QMqttTopicFilter(topic), qos);

    QObject::connect(subscription, &QMqttSubscription::messageReceived, [subscription](const QMqttMessage &msg){
        qDebug() << "topic: " << msg.topic().name() << ", temperature: " << msg.payload().toInt() << "°C";
        subscription->unsubscribe();


        qApp->exit();
    });

    // publish message
    client->publish(topic, "15");
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QMqttClient client;
    client.setHostname("10.10.10.102"); // Raspberry pi4 IP Address.
    client.setPort(1883);

    QObject::connect(&client, &QMqttClient::stateChanged, [&client](){
        if(client.state() == QMqttClient::Connected){
            qDebug() << "Client Connected.";
            subscribe(&client);
        }
    });

    if(client.state() == QMqttClient::Disconnected){
        // connect to broker
        client.connectToHost();
    }

    return a.exec();
}
실행결과:
qtmqtt.png
번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 116959
180 Qt Android 앱에 AdMob 배너달기 file makersweb 2021.12.04 2954
179 HTTPS URL을 연결할 때 SslHandshakeFailedError 오류 makersweb 2022.07.31 3035
178 Qt 6.4에 추가될 Qt Quick 3D Physics file makersweb 2022.08.07 3256
177 성능 고려 및 제안 사항 makersweb 2022.03.07 3308
176 QProcess 보안 권고 리뷰 file makersweb 2022.09.18 3338
175 그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software) j2doll 2020.12.25 3500
174 Binding 타입으로 객체 속성 간 묶기 makersweb 2022.03.04 3521
173 Qt Marketplace 발표 makersweb 2019.12.02 3755
172 Qt Bluetooth Low Energy 개요 makersweb 2022.02.13 3773
171 응용프로그램 자동실행 설정 (on Windows) makersweb 2021.05.08 3801
170 QML에서 Websocket 서버와 통신 file makersweb 2021.09.18 3803
169 Qt 6 에서 프로퍼티 바인딩 makersweb 2021.04.03 3821
168 VirtualKeyboard 스타일 커스터 마이징 makersweb 2022.03.13 3861
167 Qt5Compat 라이브러리를 사용하여 Qt5에서 Qt6로 포팅 [2] makersweb 2020.12.05 3872
166 QML 에서 QR코드 생성 file makersweb 2021.08.20 3936
165 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 3955
164 Qt Safe Renderer 개요 file makersweb 2022.09.08 3976
163 Qt 응용프로그램에서 Lottie Animation사용 file makersweb 2021.05.30 3978
162 Qt 응용프로그램에서 PDF 문서 렌더링 file makersweb 2021.09.23 3991
161 QtQuick 애플리케이션에 Rive 애니메이션 통합 makersweb 2024.03.31 4004