Skip to content

Commit

Permalink
Add KDMqtt client example
Browse files Browse the repository at this point in the history
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
marcothaller committed Nov 11, 2024
1 parent c95258b commit 0ceb208
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#

add_subdirectory(gui_window)
add_subdirectory(mqtt_client)
34 changes: 34 additions & 0 deletions examples/mqtt_client/CMakeLists.txt
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()
54 changes: 54 additions & 0 deletions examples/mqtt_client/mqtt_client.cpp
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(&timestamp)));
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();
}

0 comments on commit 0ceb208

Please sign in to comment.