How to find MQTT examples
-
Hello. I would like to learn about MQTT. I try to look for some example projects but cannot find any. I use QT 6.5
I tried looking at the Qt maintenance tool but cannot seem to find MQTT component (library).
I appreciate if someone coild clarify to me how can I access the MQTT example projects and try them out myself. Thanks in advance.
-
Hi,
The most simple would be to clone the module sources.
-
Thanks for the response but I was not able to do it succesfully. It is worth mentioning that I am using open source version of QT. Why is it not available as an example project like other examples? It would be so much easier.
Anyways, im trying to figure out how to get it working:
1. I have managed to clone to the whole repository:
I go to examples->mqtt->simpleclient->simpleclient.pro
I tried to build the example project but got many errors:
It does not seem to find mqtt module. Not to sure how to get it to build.
2. I have decided to simplify things and start a brand new project
In my project.pro file, I have added network and mqtt modules:
QT += core gui network mqtt greaterThan(QT_MAJOR_VERSION, 4): QT += widgets INSTALLS += target CONFIG += c++17 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
When I try to build it, it says that the mqtt module is missing:
In the repository that I have downloaded earlierI have found the following folder:
qtmqtt\src\mqtt
I assume this folder contains all the mqtt code and I must import it to my project somehow. I pasted this whole src directory into my project folder:
But I am not sure how to configure my project to include this library. Any ideas?
It would be cool to simplify running MQTT projects as it is not very straightforward for begginners.
I also had a look at:
C:/Qt/6.5.0/mingw_64/include
And could not find mqtt component there. Any way to add it to the include folder of my Qt?
Thanks in advance for any advice!
-
You need to build and install the module before you can use it.
You can do that from Qt Creator (requires an additional step for the install).
-
There is not much more to it than what I wrote:
Open the top level
CMakeLists.txt
of the module with Qt Creator and use the Qt version you want to develop with to build the module. -
Thanks for the help but that does not seem to be as straightforward as you say.
I tried to open and build the top level CMakeLists.txt but there are errors.
I am currently looking into how to solve this cmake issue.
-
@lukutis222 You are apparently on the dev branch, checkout the branch corresponding to your Qt version.
-
Well spotted sir!
After I checked out to required branch with command:
git checkout 6.5.0
I have managed to build it:
16:51:17: Running steps for project QtMqtt... 16:51:17: Starting: "C:\Qt\Tools\CMake_64\bin\cmake.exe" --build C:/Users/Lukas/Desktop/Programming/QT/MQTT/build-qtmqtt-Desktop_Qt_6_5_0_MinGW_64_bit-Debug --target all ninja: no work to do. 16:51:17: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited normally. 16:51:17: Elapsed time: 00:00.
I have now this build folder containing all the files:
I assume the next step after building the module is to install it. I am looking into how this can be done at the moment
Additionally, I tried something else:
TEST#1
I found this stackoverflow thread:
https://stackoverflow.com/questions/66367858/install-mqtt-module-in-open-source-qtIt seems that there is some additional steps missing as this suggestion also does not seem to work:
C:\Users\Lukas>cd C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt>qmake Usage: qmake [mode] [options] [files] QMake has two modes, one mode for generating project files based on some heuristics, and the other for generating makefiles. Normally you shouldn't need to specify a mode, as makefile generation is the default mode for qmake, but you may use this to test qmake on an existing project Mode: -project Put qmake into project file generation mode In this mode qmake interprets [files] as files to be added to the .pro file. By default, all files with known source extensions are added. Note: The created .pro file probably will need to be edited. For example add the QT variable to specify what modules are required. -makefile Put qmake into makefile generation mode (default) In this mode qmake interprets files as project files to be processed, if skipped qmake will try to find a project file in your current working directory Warnings Options: -Wnone Turn off all warnings; specific ones may be re-enabled by later -W options -Wall Turn on all warnings -Wparser Turn on parser warnings -Wlogic Turn on logic warnings (on by default) -Wdeprecated Turn on deprecation warnings (on by default) Options: * You can place any variable assignment in options and it will be * * processed as if it was in [files]. These assignments will be * * processed before [files] by default. * -o file Write output to file -d Increase debug level -t templ Overrides TEMPLATE as templ -tp prefix Overrides TEMPLATE so that prefix is prefixed into the value -help This help -v Version information -early All subsequent variable assignments will be parsed right before default_pre.prf -before All subsequent variable assignments will be parsed right before [files] (the default) -after All subsequent variable assignments will be parsed after [files] -late All subsequent variable assignments will be parsed right after default_post.prf -norecursive Don't do a recursive search -recursive Do a recursive search -set <prop> <value> Set persistent property -unset <prop> Unset persistent property -query <prop> Query persistent property. Show all if <prop> is empty. -qtconf file Use file instead of looking for qt6.conf, then qt.conf -cache file Use file as cache [makefile mode only] -spec spec Use spec as QMAKESPEC [makefile mode only] -nocache Don't use a cache file [makefile mode only] -nodepend Don't generate dependencies [makefile mode only] -nomoc Don't generate moc targets [makefile mode only] -nopwd Don't look for files in pwd [project mode only] C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt>make make: *** No targets specified and no makefile found. Stop. C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt>make install make: *** No rule to make target `install'. Stop. C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt>
TEST#2
I tried to follow this video:
https://www.youtube.com/watch?v=DGB0GfFRqPo&ab_channel=EmbeddedLaboratoryBut also failed:
C:\Users\Lukas>cd C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt\build C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt\build>C:\Qt\Tools\CMake_64\bin\cmake --build . Error: could not load cache C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt\build>C:\Qt\6.5.0\mingw_64\bin\qt-configure-module .. 'C:\Qt\6.5.0\mingw_64\bin\\qt-cmake-private.bat' '..' CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool. CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage -- Configuring incomplete, errors occurred! See also "C:/Users/Lukas/Desktop/Programming/QT/MQTT/qtmqtt/build/CMakeFiles/CMakeOutput.log". CMake Error at C:/Qt/6.5.0/mingw_64/lib/cmake/Qt6/QtProcessConfigureArgs.cmake:994 (message): CMake exited with code 1. C:\Users\Lukas\Desktop\Programming\QT\MQTT\qtmqtt\build>C:\Qt\6.5.0\mingw_64\bin\qt-configure-module .. 'C:\Qt\6.5.0\mingw_64\bin\\qt-cmake-private.bat' '..' CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool. CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage -- Configuring incomplete, errors occurred! See also "C:/Users/Lukas/Desktop/Programming/QT/MQTT/qtmqtt/build/CMakeFiles/CMakeOutput.log". CMake Error at C:/Qt/6.5.0/mingw_64/lib/cmake/Qt6/QtProcessConfigureArgs.cmake:994 (message): CMake exited with code 1.
-
If you want to install from the command line, go to the build folder.
As for the cmake issue, use the ˋqt-cmake ` wrapper found in the bin folder of the 6.5.0 installation,
-
@SGaist
Its a little bit more confusing as there is probably more than 1 method of doing this. It would be probably better to stick to a single method ( the one that we initially started maybe ) as I am a little bit overwhelmed by complexity of building and installing mqtt module.Please lets go a step backwards and continue from where we left initially.
So now that I managed to build the top level project after i checked out to the correct branch and I have the following build folder:build-qtmqtt-Desktop_Qt_6_5_0_MinGW_64_bit-Debug
Would you be able to provide further instructions as I didint manage to find any relevant information on youtube/stackoverflow or qt forums on what would be the next step.
After I figure this all out, il be sure to provide detailed step by step instructions on how to build and install this mqtt module for dummies as it is way too complex as it currently is.
-
Check the bottom right part of this image of this stack overflow post
Just add that step and it should install the module.
-
I go into Projects , then under Build Steps I have tried to "Custom process step"
Then I provide the following command and argument:
I get the following error when trying to build the project:
make: *** No rule to make target `install'. Stop. 15:01:39: The process "C:\Program Files (x86)\GnuWin32\bin\make.exe" exited with code 2. Error while building/deploying project QtMqtt (kit: Desktop Qt 6.5.0 MinGW 64-bit) When executing step "Custom Process Step" 15:01:39: Elapsed time: 00:00.
I also noticed that it complains about working directory:
I tried to replace it with build directory as shown below but still with little luck
Perhaps you know an easy method on how to setup mqtt using command line? I dont mind using command line if that is easier for you.
-
UPDATE
I have been experimenting with Build Steps and figured something out:
I have added another cmake build step and just ticked "install" Target" (See image below):
After building the project, I can confirm that the MQTT module has been installed. If I go to
C:\Qt\6.5.0\mingw_64\modules
I can see mqtt.json module in the folder.
I can now build MQTT examples! Thank you for helping me out!
Just out of curiosity - Do you have any ideas why this process hasnt been automated yet by QT Creator? Why is it not possible to simply run mqtt projects like any other project from the example projects? Why can I not download the MQTT module via the Maintenance tool? Why do we need to go through all the extra steps to get it running? Surely it would be more convenient and simple?
-
@lukutis222 AFAIK, pre-built binaries for that module are available for commercial license holder.
As for Qt Creator, most likely because Qt Creator is way younger than Qt itself and has likely not yet been used as much as other means to develop Qt. Note that having for example the qtbase completely loaded in an IDE is a pretty intensive task which might not even be doable due to the sheer size of the module.
However, additional documentation on that matter would be something good.