remove debug prints in mqtt class

This commit is contained in:
2024-10-24 12:45:32 +02:00
parent 6546dcbdd6
commit c2b4a5418b
3 changed files with 35 additions and 17 deletions

View File

@@ -1,11 +1,11 @@
#include "MqttClient.h" #include "MqttClient.h"
MqttClient::MqttClient(const std::string& address, const std::string& clientId, const std::string& username, const std::string& password) MqttClient::MqttClient(const std::string& address, const std::string& clientId, const std::string& username, const std::string& password)
: client_(address, clientId), username_(username), password_(password) { : client_(address, clientId), username_(username), password_(password), callback_(*this) {
client_.set_callback(callback_); client_.set_callback(callback_);
connOpts_.set_clean_session(true); connOpts_.set_clean_session(true);
connOpts_.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1 connOpts_.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1
if (!username_.empty() && !password_.empty()) { //if username and password are not empty also set those otherwise log in anonymously if (!username_.empty() && !password_.empty()) {
connOpts_.set_user_name(username_); connOpts_.set_user_name(username_);
connOpts_.set_password(password_); connOpts_.set_password(password_);
} }
@@ -32,7 +32,7 @@ void MqttClient::subscribe(const std::string& topic, int qos) {
} }
} }
/// @brief only needed if program doesnt loop itself /// @brief Only needed when program doesnt keep itself alive
void MqttClient::run() { void MqttClient::run() {
// Keep the client running to receive messages // Keep the client running to receive messages
while (true) { while (true) {
@@ -41,9 +41,8 @@ void MqttClient::run() {
} }
void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) { void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) {
std::cout << "Received message: '" << msg->get_topic() std::lock_guard<std::mutex> lock(client_.messageMutex_);
<< "' : " << msg->to_string() << std::endl; client_.lastMessage_ = msg->to_string();
// Do something with the message, e.g., trigger a GPIO action
} }
void MqttClient::Callback::connection_lost(const std::string& cause) { void MqttClient::Callback::connection_lost(const std::string& cause) {
@@ -53,3 +52,10 @@ void MqttClient::Callback::connection_lost(const std::string& cause) {
void MqttClient::Callback::delivery_complete(mqtt::delivery_token_ptr token) { void MqttClient::Callback::delivery_complete(mqtt::delivery_token_ptr token) {
std::cout << "Message delivered!" << std::endl; std::cout << "Message delivered!" << std::endl;
} }
/// @brief Get the last message received from the MQTT broker
/// @return The last message received in a string
std::string MqttClient::getLastMessage() {
std::lock_guard<std::mutex> lock(messageMutex_);
return lastMessage_;
}

View File

@@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <mutex>
#include <mqtt/async_client.h> #include <mqtt/async_client.h>
class MqttClient { class MqttClient {
@@ -11,13 +12,18 @@ public:
void connect(); void connect();
void subscribe(const std::string& topic, int qos = 1); void subscribe(const std::string& topic, int qos = 1);
void run(); void run();
std::string getLastMessage();
private: private:
class Callback : public virtual mqtt::callback { class Callback : public virtual mqtt::callback {
public: public:
void message_arrived(mqtt::const_message_ptr msg) override; //overide the main functions in the original class Callback(MqttClient& client) : client_(client) {}
void message_arrived(mqtt::const_message_ptr msg) override;
void connection_lost(const std::string& cause) override; void connection_lost(const std::string& cause) override;
void delivery_complete(mqtt::delivery_token_ptr token) override; void delivery_complete(mqtt::delivery_token_ptr token) override;
private:
MqttClient& client_;
}; };
mqtt::async_client client_; mqtt::async_client client_;
@@ -25,6 +31,8 @@ private:
Callback callback_; Callback callback_;
std::string username_; std::string username_;
std::string password_; std::string password_;
std::string lastMessage_;
std::mutex messageMutex_;
}; };
#endif // MQTTCLIENT_H #endif // MQTTCLIENT_H

View File

@@ -9,12 +9,12 @@
using namespace std; using namespace std;
CKobuki robot; CKobuki robot;
int movement(); int movement();
int checkCenterCliff(); std::string ReadMQTT()
void setup(){ void setup(){
unsigned char *null_ptr(0); unsigned char *null_ptr(0);
robot.startCommunication("/dev/ttyUSB0", true, null_ptr); robot.startCommunication("/dev/ttyUSB0", true, null_ptr);
MqttClient client("mqtt://localhost:1883", "raspberry_pi_client", "ishak", "kobuki"); MqttClient client("mqtt://localhost:1883", "KobukiRPI", "ishak", "kobuki");
client.connect(); client.connect();
client.subscribe("home/commands"); client.subscribe("home/commands");
@@ -23,18 +23,22 @@ void setup(){
int main() int main()
{ {
// thread mv(movement); readMQTT();
// mv.join(); //only exit once thread one is done running
checkCenterCliff();
return 0; return 0;
} }
int checkCenterCliff() std::string ReadMQTT()
{ {
while (true) { while (true) {
std::cout << robot.parser.data.CliffSensorCenter << endl; std::string message = client.getLastMessage();
if (!message.empty()) {
std::cout << "MQTT Message: " << message << std::endl;
}
// Add a small delay to avoid busy-waiting
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return message;
} }
} }