-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a very basic MQTT client example. Examlple shows how to use and integrate KDMqtt in you KDFoundation CoreApplication. Example connects to broker at test.mosquitto.org, subscribes to mytopic as soon as connected to the broker and publishes a message for mytopic as soon as subscribing to the topic is done. The received message is then printed using spdlog::info. This example also outlines the steps neccessary when deploying an application using KDMqtt on windos (copying DLLs next to the binary).
- Loading branch information
1 parent
c95258b
commit 0ceb208
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
# | ||
|
||
add_subdirectory(gui_window) | ||
add_subdirectory(mqtt_client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This file is part of KDUtils. | ||
# | ||
# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
# Author: Marco Thaller <marco.thaller@kdab.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
# | ||
|
||
project(mqtt_client_example LANGUAGES CXX) | ||
|
||
add_executable( | ||
${PROJECT_NAME} | ||
mqtt_client.cpp | ||
) | ||
|
||
target_link_libraries( | ||
${PROJECT_NAME} | ||
KDMqtt | ||
) | ||
|
||
if (WIN32) | ||
# Deployment: On Windows, copy all DLLs from the mosquitto install directory next to the application binary so that they're found. | ||
if (BUILD_INTEGRATION_MQTT AND MOSQUITTO_RUNTIME_DLLS) | ||
foreach(MOSQUITTO_RUNTIME_DLL ${MOSQUITTO_RUNTIME_DLLS}) | ||
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy_if_different | ||
"${MOSQUITTO_RUNTIME_DLL}" | ||
$<TARGET_FILE_DIR:${PROJECT_NAME}> | ||
) | ||
endforeach() | ||
endif() | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
This file is part of KDUtils. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
Author: Marco Thaller <marco.thaller@kdab.com> | ||
SPDX-License-Identifier: MIT | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
#include <KDFoundation/core_application.h> | ||
#include <KDMqtt/mqtt.h> | ||
|
||
using namespace KDFoundation; | ||
using namespace KDMqtt; | ||
|
||
int main() | ||
{ | ||
const Url url("test.mosquitto.org"); | ||
const std::string topic = "mytopic"; | ||
const std::string payload = "Hello World!"; | ||
|
||
CoreApplication app; | ||
|
||
MqttLib::instance().init(); | ||
MqttClient mqttClient("KDMqttClient", true, true); | ||
|
||
auto onMqttConnectionStateChanged = [&](const MqttClient::ConnectionState &connectionState) { | ||
if (connectionState == MqttClient::ConnectionState::CONNECTED) { | ||
mqttClient.subscribe(topic.c_str()); | ||
} | ||
}; | ||
std::ignore = mqttClient.connectionState.valueChanged().connect(onMqttConnectionStateChanged); | ||
|
||
auto onMqttSubscriptionStateChanged = [&](const MqttClient::SubscriptionState &subscriptionState) { | ||
if (subscriptionState == MqttClient::SubscriptionState::SUBSCRIBED) { | ||
mqttClient.publish(nullptr, topic.c_str(), payload.length(), payload.c_str()); | ||
} | ||
}; | ||
std::ignore = mqttClient.subscriptionState.valueChanged().connect(onMqttSubscriptionStateChanged); | ||
|
||
auto onMqttMessageReceived = [&](const mosquitto_message *message) { | ||
const auto timestamp = std::time(nullptr); | ||
const auto timestring = std::string(std::asctime(std::localtime(×tamp))); | ||
const auto topic = std::string(message->topic); | ||
const auto payload = std::string(static_cast<char *>(message->payload)); | ||
spdlog::info("Received MQTT message. Topic: {}. Payload: {}", topic, payload); | ||
}; | ||
std::ignore = mqttClient.msgReceived.connect(onMqttMessageReceived); | ||
|
||
mqttClient.connect(url); | ||
|
||
app.exec(); | ||
} |