A Data Consumer
The following program is an example of a simple data consumer built with the Paho Mosquitto C++ Library.
File
: SimpleMqttConsumer.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// Start by `#include`-ing the Mosquitto MQTT Library and other standard libraries. #include <mqtt/client.h> // Mosquitto client. #include <ostream> // std::cout. // With the library header files included, continue by defining a main function. int main() { // In order to connect the mqtt client to a broker, // Define an Ip address pointing to a broker. In this case, the localhost on port 1883. std::string ip = "localhost:1883"; // Then, define an ID to be used by the client when communicating with the broker. std::string id = "consumer"; // Construct a client using the Ip and Id, specifying usage of MQTT V5. mqtt::client client(ip, id, mqtt::create_options(MQTTVERSION_5)); // Use the connect method of the client to establish a connection to the broker. client.connect(); // In order to receive messages from the broker, specify a topic to subscribe to. client.subscribe("in"); // Begin the client's message processing loop, filling a queue with messages. client.start_consuming(); bool running = true; while (running) { // Construct a message pointer to hold an incoming message. mqtt::const_message_ptr messagePointer; // Try to consume a message, passing messagePointer by reference. // If a message is consumed, the function will return `true`, // allowing control to enter the if-statement body. if (client.try_consume_message(&messagePointer)) { // Construct a string from the message payload. std::string messageString = messagePointer -> get_payload_str(); // Print payload string to console (debugging). std::cout << messageString << std::endl; // Perform processing on the string. // This is where message processing can be passed onto different // functions for parsing. // Here, we break the loop and exit the program if a `quit` is received. if (messageString == "quit") { running = false; } } } return 0; } |
Compiling The Data Consumer
To compile the C++ source code into an executable file, use g++:
1 2 |
g++ SimpleMqttConsumer.cpp -lpaho-mqttpp3 -lpaho-mqtt3as -o simpleConsumer |
Make note of the library links -lpaho-mqttpp3
and -lpaho-mqtt3as
.
The library for the Mosquitto C++ interface is -lpaho-mqttpp3
which in turn requires the Mosquitto C interface-lpaho-mqtt3as
as a dependency.
A Data Producer
The following program is an example of a simple data producer built with the Paho Mosquitto C++ Library.
File
: SimpleMqttProducer.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
// Start by `#include`-ing the Mosquitto MQTT Library and other standard libraries. #include <mqtt/client.h> // Mosquitto client. #include <ostream> // std::cout. #include <chrono> // Time keeping. #include <thread> // Sleep. // With the library header files included, continue by defining a main function. int main() { // In order to connect the mqtt client to a broker, // Define an Ip address pointing to a broker. In this case, the localhost on port 1883. std::string ip = "localhost:1883"; // Then, define an ID to be used by the client when communicating with the broker. std::string id = "publisher"; // Construct a client using the Ip and Id, specifying usage of MQTT V5. mqtt::client client(ip, id, mqtt::create_options(MQTTVERSION_5)); // Use the connect method of the client to establish a connection to the broker. client.connect(); // Initialize an empty message with specified topic. mqtt::message_ptr timeLeftMessagePointer = mqtt::make_message("consumer/in", ""); // Create a loop for counting down from N seconds. for (int i = 10; i > 0; --i) { // Sleep for one second. std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Configure Mqtt message to contain payload specifying time until end. timeLeftMessagePointer->set_payload("Time Left: " + std::to_string(i)); // Publish the Mqtt message using the connected client. client.publish(timeLeftMessagePointer); } // After counting down, configure Mqtt message for sending the quit signal. timeLeftMessagePointer->set_payload("quit"); // Send quit signal to listeners. client.publish(timeLeftMessagePointer); return 0; } |
Compiling The Data Producer
To compile the C++ source code into an executable file, use g++:
1 2 |
g++ SimpleMqttProducer.cpp -lpaho-mqttpp3 -lpaho-mqtt3as -o simpleProducer |
Communicating Between The Clients
With each of the client programs compiled in to executables, they can now be used to communicate between one another.
Start The Consumer
I
n one terminal, execute the simpleConsumer
program to begin listening.
1 2 |
./simpleConsumer |
Start The Producer
With the Consumer started, open another terminal and execute the simpleProducer
program to begin publishing.
1 2 |
./simpleProducer |
The simpleProducer
will begin publishing messages to a topic on the MQTT broker.
These messages are picked up by the simpleConsumer
and printed to the console.
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 |
The final message, quit
, triggers the simpleConsumer
to stop looping and return control to the console.