en / de
AI
Expertisen
Methoden
Dienstleistungen
Referenzen
Jobs & Karriere
Firma
Technologie-Trends TechCast WebCast TechBlog News Events Academy

Getting started with MQTT/C++

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.

MQTT server setup

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.

MQTT server dashboard

Figure 1: MQTT Server dashboard

Acquisition and compilation of the MQTT client libraries

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.

openssl

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.

Paho C library

Get the Paho C library from Github.

Use CMake in order to generate a Visual Studio solution file. This description helped me get along.

CMake for Paho C library

Figure 2: CMake for Paho C library

Configure – Generate – Open project and build the solution.

Paho C++ library

Get he Paho C++ library from Github.

Use CMake again and build the resulting solution.

CMake for Paho C++ library

Figure 3: CMake for Paho C++ library

Demo

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.

Prepare async subscribe

  1. Create a new empty project and add the async_subscribe.cpp file.
  2. Update the project settings as shown below
  3. Copy the following DLL files to the output folder (eg. Debug): libcrypto-1_1.dll, libssl-1_1.dll, paho-mqtt3as.dll
  4. Build the solution

Update the include directories so that the header files can be found.

Additional include directories

Figure 4: Additional include directories

Because of the empty project created, do not forget the preprocessor definitions. Alternatively, go for property sheets.

Preprocessor definitions

Figure 5: Preprocessor definitions

When it comes to the linker, we need to point to the directory for additional libraries.

Additional library directories

Figure 6: Additional library directories

And we must add them.

Additional dependencies

Figure 7: Additional dependencies

Prepare ssl publish

  1. Create a new empty project and add the ssl_publish.cpp file.
  2. Update the project settings, copy the DLL files and build the solution as shown above
  3. Copy the test-root-ca.crt file to the output directory
  4. Update the server port to 8883 for the EMQ broker
  5. Update the connection options to omit the user name and password like here
//const std::string DFLT_SERVER_ADDRESS{ "ssl://localhost:18885" };
const std::string DFLT_SERVER_ADDRESS{ "ssl://localhost:8883" };
mqtt::connect_options connopts;// ("testuser", "testpassword");

Run the demo, first try

  1. Ensure the broker is running (see above)
  2. In order that the publisher’s messages are not lost first start the async_subscribe sample
  3. In the ssl_publish sample set a breakpoint before the connection is cut. Otherwise, the program immediately exits:
conntok = client.disconnect();

Unfortunately, the following line in the ssl_publish sample returns with exit code 1.

conntok->wait();

Run the demo

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.

MQTT server dashboard with two subscribers

Figure 8: MQTT server dashboard with two subscribers

Next, we see the console output of the publisher.

Console output MQTT publisher

Figure 9: Console output MQTT publisher

And the output of the subscriber. Because the topic of both samples is the same, the subscriber shows the published messages here.

Console output MQTT subscriber

Figure 10: Console output MQTT subscriber

This should serve as a starting point. Further reading is highly recommended. I found the following blog series helpful.

Further reading:

Kommentare

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Newsletter - aktuelle Angebote, exklusive Tipps und spannende Neuigkeiten

 Jetzt anmelden

Copyright © 2025 Noser Engineering AG – Alle Rechte vorbehalten.

NACH OBEN
Privacy Policy Cookie Policy
Zur Webcast Übersicht