Sunday, January 29, 2023

Getting Started with MQTT using Qt

Qt is a cross-platform framework written in C++ for creating graphical user interfaces as well as cross-platform applications that run on various software and hardware platforms such as Linux, Windows, macOS, Android, or embedded systems with little or no change in the underlying code base while still being a native application with native capabilities and speed. 

MQTT is a lightweight, publish-subscribe, machine-to-machine network protocol for message queue/message queuing service. It is designed for connections with remote locations that have devices with resource constraints or limited network bandwidth.

In this post, I will show you how to get started quickly with MQTT using Qt. Qt already provides the QTMQTT library but unfortunately for the open-source version of Qt, we have to build the QtMQTT source code to generate the library, and this is the main motive behind this post.

If you are interested in watching the video, please watch the below-mentioned video, or else continue reading this post.


The above-mentioned video is for Linux Users and the below-mentioned video is for Windows.

This post is sponsored by PCBWay, with more than a decade in the field of PCB prototype and fabrication, PCBWay is committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests.
As one of the most experienced PCB manufacturers in the World, PCBWay prides itself to be your best business partner as well as a good friend in every aspect of your PCB needs.

Cloning Qt's MQTT Library

If we do a simple google search with the keyword "Qt MQTT Repository" we will land on the following link.
And on this page, we can see various development branches, and now as a first step we have to clone the repository based on the Qt installed on our laptop, in my case the Qt version installed is "6.2.4", hence I will clone the same branch from the above page, and the command to do this is given below.
git clone -b 6.2.4 git://code.qt.io/qt/qtmqtt.git
In the above command "6.2.4" is the Qt version, you have to replace it with the version which is installed on your computer.
The above command will clone the Qt MQTT Source Code on our computer and the next step is to compile the source code to generate the Qt MQTT Library.

Compiling the Qt MQTT Source

Compiling the source files to generate the library is the most important step. Since we will be compiling the Qt MQTT Source files from the command line terminal hence it is desirable to have proper environment variables set while building Qt projects from the command line.
Here we will make a script file on our computer with all the relevant environment variables, and let's name this file "Qt_Environment.sh", and the content of this file is shown below.
export QT_VERSION="6.2.4"

export QT_INSTALL_DIR="/opt/Qt"
export CMAKE_BIN_DIR="${QT_INSTALL_DIR}/Tools/CMake/bin"
export QMAKE_BIN_DIR="${QT_INSTALL_DIR}/${QT_VERSION}/gcc_64/bin"
export CMAKE_PREFIX_PATH="${QT_INSTALL_DIR}/${QT_VERSION}/gcc_64/"
export NINJA_DIR="${QT_INSTALL_DIR}/Tools/Ninja"
export PATH="${PATH}:${CMAKE_BIN_DIR}:${QMAKE_BIN_DIR}:${NINJA_DIR}"

Update "QT_VERSION" and "QT_INSTALL_DIR" as per your computer installation, in my case the Qt is installed inside the "opt" folder and its version is "6.2.4".
Note: Make Sure there are no spaces in the above file else it will not work and gives you a lot of errors.
Now, the next step is to load these environment variables, and this can be done with the help of the below command.
source QT_Environment.sh
This above command will load the environment variables for only one session of the terminal, and if the terminal is closed, we need to execute the same command again. But in case you wanted to avoid this step, you can load this script at power-up, but for me this is fine, so I will not do this.
The next step is to generate the build environment, build the source files and install the library, and all this can be done using the below mentioned commands.
cd qtmqtt
mkdir build
cd build
/opt/Qt/6.2.4/gcc_64/bin/qt-configure-module ..
/opt/Qt/Tools/CMake/bin/cmake --build .
/opt/Qt/Tools/CMake/bin/cmake --install . --verbose
The first command is "cd qtmqtt", here "qtmqtt" is the name of the directory where we cloned the repository, if your directory name is different, please use that, the main idea is to just go inside this directory.
The second command is "mkdir build", the purpose of this command is to create a directory named "build", which contains all the build-related information.
The third command is "cd build", the purpose of this command is to go inside the empty directory build, and from here we will run our main commands.
The fourth command is "/opt/Qt/6.2.4/gcc_64/bin/qt-configure-module ..", this command will change according to your Qt installation path, this command will basically generate the build environment, you can always check in the directory explorer where your "qt-configure-module" is present, and update this command accordingly.
The fifth command is "/opt/Qt/Tools/CMake/bin/cmake --build .", this command will build the project and generate the Qt MQTT library.
And then the sixth and last step is to install the Qt MQTT Library, and this can be done by using the command "/opt/Qt/Tools/CMake/bin/cmake --install . --verbose", this command will install the Qt MQTT library and then it can be accessed directly from the Qt Creator. Sometimes this command can give error, if your Qt is installed in some special location, like in my case it is installed inside the "/opt/" folder, and in this case we have to execute this command as "sudo".
Now, the library is installed the next step is to test the library.

Testing Qt MQTT Project

The repository which we have cloned contains several example projects and we will use one of these projects to test the Qt MQTT's library.
Qt MQTT Example Projects

I will be using the "simple client" project from these example projects. The following is the user interface of this project.
Simple Client Qt MQTT project

As can be seen in the above image, we can specify the Host Name, Port, Topic, and Message, and then there are Connect, Publish and Subscribe buttons.
In my case, I am running an MQTT broker on my local machine, and hence in Host Name I have specified "localhost", and in the port name, I have specified 1883.
The following animation file will show how it works, whenever publish button is pressed, the topic message is received on the terminal which is subscribing to the topic.
Project Demo

That's all from this tutorial, the reason why I created this post is that in the future I am working on a simple project where we can control the devices and get the status of some sensors using ESP32, and all this is done using the "MQTT" protocol, and this all is done using the Android application, and to develop that Android application, I will be using the Qt, hence this is needed to get started.

NOTE:
I did the same thing under Windows, open the Qt Creator and opened the "MQTT" project, and compiled the project for Windows and Android. After this, I used the following command to install the library for Windows and Android, from their respective build folders.
c:\Qt\Tools\CMake_64\bin\cmake.exe --install . --verbose

For Windows, the library is installed in the proper place, but for Android, it gets installed inside the "C" drive and "Program Files" folder, and then I have to copy them manually inside the Android library folders.
I don't know why this happen, I am not an expert on CMake, in case someone knows please let me know in the comment section.

No comments:

Post a Comment