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.
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