diff --git a/.gitignore b/.gitignore index 84cc5d1..1db0c74 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,8 @@ src/C++/Driver/Makefile src/Python/flask/web/_pycache_ venv build/ -log \ No newline at end of file +log +CMakeFiles/ +Makefile +CMakeCache.txt +cmake_install.cmake diff --git a/src/C++/MQTT/mqttclass/CMakeLists.txt b/src/C++/MQTT/mqttclass/CMakeLists.txt new file mode 100644 index 0000000..1f31293 --- /dev/null +++ b/src/C++/MQTT/mqttclass/CMakeLists.txt @@ -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}) diff --git a/src/C++/MQTT/mqttclass/MqttClient.cpp b/src/C++/MQTT/mqttclass/MqttClient.cpp new file mode 100644 index 0000000..c2b2fc9 --- /dev/null +++ b/src/C++/MQTT/mqttclass/MqttClient.cpp @@ -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; +} \ No newline at end of file diff --git a/src/C++/MQTT/mqttclass/MqttClient.h b/src/C++/MQTT/mqttclass/MqttClient.h new file mode 100644 index 0000000..3f11986 --- /dev/null +++ b/src/C++/MQTT/mqttclass/MqttClient.h @@ -0,0 +1,30 @@ +#ifndef MQTTCLIENT_H +#define MQTTCLIENT_H + +#include +#include +#include + +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 \ No newline at end of file diff --git a/src/C++/MQTT/mqttclass/main.cpp b/src/C++/MQTT/mqttclass/main.cpp new file mode 100644 index 0000000..0d53a61 --- /dev/null +++ b/src/C++/MQTT/mqttclass/main.cpp @@ -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; +} \ No newline at end of file