Update MQTT address to use SSL and change broker address

This commit is contained in:
ishak jmilou.ishak
2024-10-22 12:48:27 +02:00
parent 995b3858ab
commit ce2956e504

View File

@@ -1,7 +1,9 @@
#include <iostream>
#include <mqtt/async_client.h>
#include <thread> // Voor std::this_thread::sleep_for
#include <chrono> // Voor std::chrono::seconds
const std::string ADDRESS("tcp://localhost:1883");
const std::string ADDRESS("tcp://ishakpi.ddns.net:1883"); // Aanpassen indien nodig
const std::string CLIENT_ID("raspberry_pi_client");
const std::string TOPIC("home/commands");
@@ -11,6 +13,14 @@ class callback : public virtual mqtt::callback {
<< "' : " << msg->to_string() << std::endl;
// Doe iets met het bericht, bijvoorbeeld een GP.IO-activering
}
void connection_lost(const std::string& cause) override {
std::cerr << "Verbinding verloren. Oorzaak: " << cause << std::endl;
}
void delivery_complete(mqtt::delivery_token_ptr token) override {
std::cout << "Bericht afgeleverd!" << std::endl;
}
};
int main() {
@@ -32,11 +42,13 @@ int main() {
std::cout << "Abonneren op topic: " << TOPIC << std::endl;
client.subscribe(TOPIC, 1)->wait();
// Houd de client draaiende om berichten te blijven ontvangen
while (true) {
// Houd de client draaiende om berichten te blijven ontvangen
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wacht om CPU-gebruik te verminderen
}
} catch (const mqtt::exception &exc) {
std::cerr << exc.what() << std::endl;
std::cerr << "Fout: " << exc.what() << std::endl;
return 1;
}