diff --git a/src/C++/Driver/src/MQTT/MqttClient.cpp b/src/C++/Driver/src/MQTT/MqttClient.cpp index 7923822..d83535b 100644 --- a/src/C++/Driver/src/MQTT/MqttClient.cpp +++ b/src/C++/Driver/src/MQTT/MqttClient.cpp @@ -3,18 +3,18 @@ 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), callback_(*this) { client_.set_callback(callback_); - connOpts_.set_clean_session(true); - connOpts_.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1 + options.set_clean_session(true); + options.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1 if (!username_.empty() && !password_.empty()) { - connOpts_.set_user_name(username_); - connOpts_.set_password(password_); + options.set_user_name(username_); + options.set_password(password_); } } void MqttClient::connect() { try { std::cout << "Connecting to broker..." << std::endl; - client_.connect(connOpts_)->wait(); + client_.connect(options)->wait(); std::cout << "Connected!" << std::endl; } catch (const mqtt::exception& exc) { std::cerr << "Error: " << exc.what() << std::endl; @@ -41,6 +41,7 @@ void MqttClient::run() { } void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) { + //lock the variable, it automaticly unlocks when going out of scope std::lock_guard lock(client_.messageMutex_); client_.lastMessage_ = msg->to_string(); } @@ -55,7 +56,9 @@ void MqttClient::Callback::delivery_complete(mqtt::delivery_token_ptr token) { /// @brief Get the last message received from the MQTT broker /// @return The last message received in a string +//std::string is the datatype of the return value std::string MqttClient::getLastMessage() { + //lock the variable, it automaticly unlocks when going out of scope std::lock_guard lock(messageMutex_); return lastMessage_; } \ No newline at end of file diff --git a/src/C++/Driver/src/MQTT/MqttClient.h b/src/C++/Driver/src/MQTT/MqttClient.h index 0cae258..06eba19 100644 --- a/src/C++/Driver/src/MQTT/MqttClient.h +++ b/src/C++/Driver/src/MQTT/MqttClient.h @@ -27,7 +27,7 @@ private: }; mqtt::async_client client_; - mqtt::connect_options connOpts_; + mqtt::connect_options options; Callback callback_; std::string username_; std::string password_; @@ -35,4 +35,4 @@ private: std::mutex messageMutex_; }; -#endif // MQTTCLIENT_H \ No newline at end of file +#endif //MQTTCLIENT_H \ No newline at end of file diff --git a/src/C++/Driver/src/main.cpp b/src/C++/Driver/src/main.cpp index 704a3eb..901acfe 100644 --- a/src/C++/Driver/src/main.cpp +++ b/src/C++/Driver/src/main.cpp @@ -10,7 +10,8 @@ using namespace std; CKobuki robot; int movement(); std::string readMQTT(); -MqttClient client("mqtt://145.92.224.21:1883", "KobukiRPI", "ishak", "kobuki"); +void parseMQTT(std::string message); +MqttClient client("mqtt://145.92.224.21:1883", "KobukiRPI", "ishak", "kobuki"); //create a client object void setup(){ @@ -18,7 +19,7 @@ void setup(){ robot.startCommunication("/dev/ttyUSB0", true, null_ptr); client.connect(); client.subscribe("home/commands"); - + parseMQTT(readMQTT()); } int main(){ @@ -42,6 +43,22 @@ std::string readMQTT() return message; } +void parseMQTT(std::string message){ + if(message == "up"){ + robot.forward(600); + } + else if(message == "left"){ + robot.Rotate(90); + } + else if(message == "down"){ + robot.Rotate(-90); + } + else{ + std::cout << "Invalid command" << std::endl; + } +} + + int movement() { int text;