attempt to make mqtt class

This commit is contained in:
2024-10-23 16:17:19 +02:00
parent 30933b0ca7
commit dc77c104e6
5 changed files with 114 additions and 1 deletions

6
.gitignore vendored
View File

@@ -19,4 +19,8 @@ src/C++/Driver/Makefile
src/Python/flask/web/_pycache_
venv
build/
log
log
CMakeFiles/
Makefile
CMakeCache.txt
cmake_install.cmake

View File

@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 23)
# Find the Paho MQTT C++ library
find_library(PAHO_MQTTPP_LIBRARY paho-mqttpp3 PATHS /usr/local/lib)
find_library(PAHO_MQTT_LIBRARY paho-mqtt3a PATHS /usr/local/lib)
# Include the headers
include_directories(/usr/local/include)
# Add the executable
add_executable(my_program main.cpp)
# Link the libraries
target_link_libraries(my_program ${PAHO_MQTTPP_LIBRARY} ${PAHO_MQTT_LIBRARY})

View File

@@ -0,0 +1,55 @@
#include "MqttClient.h"
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_.set_callback(callback_);
connOpts_.set_clean_session(true);
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
connOpts_.set_user_name(username_);
connOpts_.set_password(password_);
}
}
void MqttClient::connect() {
try {
std::cout << "Connecting to broker..." << std::endl;
client_.connect(connOpts_)->wait();
std::cout << "Connected!" << std::endl;
} catch (const mqtt::exception& exc) {
std::cerr << "Error: " << exc.what() << std::endl;
throw;
}
}
void MqttClient::subscribe(const std::string& topic, int qos) {
try {
std::cout << "Subscribing to topic: " << topic << std::endl;
client_.subscribe(topic, qos)->wait();
} catch (const mqtt::exception& exc) {
std::cerr << "Error: " << exc.what() << std::endl;
throw;
}
}
/// @brief only needed if program doesnt loop itself
void MqttClient::run() {
// Keep the client running to receive messages
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait to reduce CPU usage
}
}
void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) {
std::cout << "Received message: '" << msg->get_topic()
<< "' : " << msg->to_string() << std::endl;
// Do something with the message, e.g., trigger a GPIO action
}
void MqttClient::Callback::connection_lost(const std::string& cause) {
std::cerr << "Connection lost. Cause: " << cause << std::endl;
}
void MqttClient::Callback::delivery_complete(mqtt::delivery_token_ptr token) {
std::cout << "Message delivered!" << std::endl;
}

View File

@@ -0,0 +1,30 @@
#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H
#include <iostream>
#include <thread>
#include <mqtt/async_client.h>
class MqttClient {
public:
MqttClient(const std::string& address, const std::string& clientId, const std::string& username = "", const std::string& password = "");
void connect();
void subscribe(const std::string& topic, int qos = 1);
void run();
private:
class Callback : public virtual mqtt::callback {
public:
void message_arrived(mqtt::const_message_ptr msg) override; //overide the main functions in the original class
void connection_lost(const std::string& cause) override;
void delivery_complete(mqtt::delivery_token_ptr token) override;
};
mqtt::async_client client_;
mqtt::connect_options connOpts_;
Callback callback_;
std::string username_;
std::string password_;
};
#endif // MQTTCLIENT_H

View File

@@ -0,0 +1,9 @@
#include "MqttClient.h"
int main(){
MqttClient client("mqtt://localhost:1883", "raspberry_pi_client", "ishak", "kobuki");
client.connect();
client.subscribe("home/commands");
return 0;
}