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:
1 2 |
./simpleConsumer |
Next, start the Python MQTT data producing program:
1 2 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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:
1 2 |
python SimpleMqttConsumer.py |
Optionally, also start the C++ data consuming client:
1 2 |
./simpleConsumer |
Next, start the C++ data producer:
1 2 |
./simpleProducer |
Finally, after printing the incoming data,
the data-consuming processes will receive the quit
signal and return control to the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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 |