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:

./simpleConsumer

Next, start the Python MQTT data producing program:

python SimpleMqttProducer.py

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.

Time Left: 10
Time Left: 9
Time Left: 8
Time Left: 7
Time Left: 6
Time Left: 5
Time Left: 4
Time Left: 3
Time Left: 2
Time Left: 1
quit

Sending Data From C++ To Python

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

python SimpleMqttConsumer.py

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

./simpleConsumer

Next, start the C++ data producer:

./simpleProducer

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

Time Left: 10
Time Left: 10
Time Left: 9
Time Left: 9
Time Left: 8
Time Left: 8
Time Left: 7
Time Left: 7
Time Left: 6
Time Left: 6
Time Left: 5
Time Left: 5
Time Left: 4
Time Left: 4
Time Left: 3
Time Left: 3
Time Left: 2
Time Left: 2
Time Left: 1
quit
Time Left: 1
quit