Using MQTT To Transfer Data From Python To C++ (And C++ To Python)

What Will Be Done

The MQTT clients provided through How To MQTT In C++ and How To MQTT In Python can be used to demonstrate the decoupling of applications provided by using MQTT. Using a messaging protocol that is independent of the programming language allows for programs written in different programming languages to interact with one another. This will be demonstrated in the following text.

Sending Data From Python To C++

To send data from the Python MQTT client to the C++ MQTT client, first, set up the listener. Since we want the C++ client to receive data, we will start the C++ MQTT consumer:

Next, start the Python MQTT data producing program:

Finally, the consumer will process the incoming data as usual. The fact that a Python program is producing the data has no effect on the original program.

Sending Data From C++ To Python

Before starting the C++ data producing client, start a listening client in another terminal:

Optionally, also start the C++ data consuming client:

Next, start the C++ data producer:

Finally, after printing the incoming data, the data-consuming processes will receive the quit signal and return control to the console.