mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
Merge branch 'main' of ssh://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79
This commit is contained in:
@@ -1,56 +1,64 @@
|
||||
#include <iostream>
|
||||
#include <mqtt/async_client.h>
|
||||
#include <thread> // Voor std::this_thread::sleep_for
|
||||
#include <chrono> // Voor std::chrono::seconds
|
||||
#include <thread> // For std::this_thread::sleep_for
|
||||
#include <chrono> // For std::chrono::seconds
|
||||
|
||||
const std::string ADDRESS("mqtt://localhost:1883"); // Aanpassen indien nodig
|
||||
// Define the address of the MQTT broker, the client ID, and the topic to subscribe to.
|
||||
const std::string ADDRESS("mqtt://localhost:1883"); // Broker address (Raspberry Pi)
|
||||
const std::string CLIENT_ID("raspberry_pi_client");
|
||||
const std::string TOPIC("home/commands");
|
||||
|
||||
// Define a callback class that handles incoming messages and connection events.
|
||||
class callback : public virtual mqtt::callback {
|
||||
// Called when a message arrives on a subscribed topic.
|
||||
void message_arrived(mqtt::const_message_ptr msg) override {
|
||||
std::cout << "Ontvangen bericht: '" << msg->get_topic()
|
||||
<< "' : " << msg->to_string() << std::endl;
|
||||
// Doe iets met het bericht, bijvoorbeeld een GP.IO-activering
|
||||
std::cout << "Received message: '" << msg->get_topic()<< "' : " << msg->to_string() << std::endl;
|
||||
}
|
||||
|
||||
// Called when the connection to the broker is lost.
|
||||
void connection_lost(const std::string& cause) override {
|
||||
std::cerr << "Verbinding verloren. Oorzaak: " << cause << std::endl;
|
||||
std::cerr << "Connection lost. Reason: " << cause << std::endl;
|
||||
}
|
||||
|
||||
// Called when a message delivery is complete.
|
||||
void delivery_complete(mqtt::delivery_token_ptr token) override {
|
||||
std::cout << "Bericht afgeleverd!" << std::endl;
|
||||
std::cout << "Message delivered!" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
// Create an MQTT async client and set up the callback class.
|
||||
mqtt::async_client client(ADDRESS, CLIENT_ID);
|
||||
callback cb;
|
||||
client.set_callback(cb);
|
||||
|
||||
// Set up the connection options (such as username and password).
|
||||
mqtt::connect_options connOpts;
|
||||
connOpts.set_clean_session(true);
|
||||
connOpts.set_user_name("ishak");
|
||||
connOpts.set_password("kobuki");
|
||||
connOpts.set_mqtt_version(MQTTVERSION_3_1_1); // Voor MQTT 3.1.1
|
||||
connOpts.set_mqtt_version(MQTTVERSION_3_1_1);
|
||||
|
||||
try {
|
||||
std::cout << "Verbinden met broker..." << std::endl;
|
||||
client.connect(connOpts)->wait();
|
||||
std::cout << "Verbonden!" << std::endl;
|
||||
// Try to connect to the broker and wait until successful.
|
||||
std::cout << "Connecting to broker..." << std::endl;
|
||||
client.connect(connOpts)->wait(); // Connect with the provided options
|
||||
std::cout << "Connected!" << std::endl;
|
||||
|
||||
std::cout << "Abonneren op topic: " << TOPIC << std::endl;
|
||||
client.subscribe(TOPIC, 1)->wait();
|
||||
// Subscribe to the specified topic and wait for confirmation.
|
||||
std::cout << "Subscribing to topic: " << TOPIC << std::endl;
|
||||
client.subscribe(TOPIC, 1)->wait(); // Subscribe with QoS level 1
|
||||
|
||||
// Houd de client draaiende om berichten te blijven ontvangen
|
||||
// Keep the program running to continue receiving messages from the broker.
|
||||
while (true) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wacht om CPU-gebruik te verminderen
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1)); // Sleep to reduce CPU usage
|
||||
}
|
||||
|
||||
} catch (const mqtt::exception &exc) {
|
||||
std::cerr << "Fout: " << exc.what() << std::endl;
|
||||
// Catch any MQTT exceptions and display the error message.
|
||||
std::cerr << "Error: " << exc.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0; // Return 0 to indicate successful execution
|
||||
}
|
||||
|
Reference in New Issue
Block a user