My search for a clear-cut description about getting started with MQTT, C++ and an SSL option was unsuccessful. This contribution aims to facilitate similar efforts of the peer developers.
The article starts by describing the setup of the server, the acquisition, and compilation of the client libraries and concludes with a simple demo application.
I work in a Windows 7 64 bit Visual Studio 2017 environment.
Let’s first tackle the MQTT server. It is ordinarily also called the broker. There are different products around. We went for the EMQ.
When first installed to the C:\Program Files\emqttd folder, the following error message showed up: «Node undefined not responding to pings».
So, I decided to install it to the C root folder in order to circumvent the space in the path.
Start the server and voilà, it shows up under http://localhost:18083.
Because we want to incorporate MQTT into our C++ source code, we need the client libraries. I found, there are basically two variants: Mosquitto and Paho. Because the installation of mosquitto as a broker yielded missing DLL errors, I decided to go for Paho. Paho itself consists of a C and a C++ library. For SLL, openssl is required.
I wanted to spare myself the hassle of building the library from the source files and fixing all issues. Binary versions can be obtained here.
This library was also installed to the C root folder.
Get the Paho C library from Github.
Use CMake in order to generate a Visual Studio solution file. This description helped me get along.
Configure – Generate – Open project and build the solution.
Get he Paho C++ library from Github.
Use CMake again and build the resulting solution.
The demo will utilize the Paho C++ samples found in the src\samples subfolder. In order to show the MQTT publish/subscribe mechanism with SSL and because we want to have a non-blocking approach, I chose the async_subscribe.cpp and the ssl_publish.cpp files as a base for two new Visual Studio solution files.
Update the include directories so that the header files can be found.
Because of the empty project created, do not forget the preprocessor definitions. Alternatively, go for property sheets.
When it comes to the linker, we need to point to the directory for additional libraries.
And we must add them.
//const std::string DFLT_SERVER_ADDRESS{ "ssl://localhost:18885" };
const std::string DFLT_SERVER_ADDRESS{ "ssl://localhost:8883" };
mqtt::connect_options connopts;// ("testuser", "testpassword");
conntok = client.disconnect();
Unfortunately, the following line in the ssl_publish sample returns with exit code 1.
conntok->wait();
With the server certificate missing in this basic setup its validation needs to be turned off for now.
mqtt::ssl_options sslopts;
sslopts.set_trust_store("test-root-ca.crt");
sslopts.set_enable_server_cert_auth(false);
The listeners view shows the two clients on the different ports.
Next, we see the console output of the publisher.
And the output of the subscriber. Because the topic of both samples is the same, the subscriber shows the published messages here.
This should serve as a starting point. Further reading is highly recommended. I found the following blog series helpful.
Further reading:
Schreiben Sie einen Kommentar