mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
added comments and controls for mqtt
This commit is contained in:
@@ -3,18 +3,18 @@
|
|||||||
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), callback_(*this) {
|
: client_(address, clientId), username_(username), password_(password), callback_(*this) {
|
||||||
client_.set_callback(callback_);
|
client_.set_callback(callback_);
|
||||||
connOpts_.set_clean_session(true);
|
options.set_clean_session(true);
|
||||||
connOpts_.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1
|
options.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1
|
||||||
if (!username_.empty() && !password_.empty()) {
|
if (!username_.empty() && !password_.empty()) {
|
||||||
connOpts_.set_user_name(username_);
|
options.set_user_name(username_);
|
||||||
connOpts_.set_password(password_);
|
options.set_password(password_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttClient::connect() {
|
void MqttClient::connect() {
|
||||||
try {
|
try {
|
||||||
std::cout << "Connecting to broker..." << std::endl;
|
std::cout << "Connecting to broker..." << std::endl;
|
||||||
client_.connect(connOpts_)->wait();
|
client_.connect(options)->wait();
|
||||||
std::cout << "Connected!" << std::endl;
|
std::cout << "Connected!" << std::endl;
|
||||||
} catch (const mqtt::exception& exc) {
|
} catch (const mqtt::exception& exc) {
|
||||||
std::cerr << "Error: " << exc.what() << std::endl;
|
std::cerr << "Error: " << exc.what() << std::endl;
|
||||||
@@ -41,6 +41,7 @@ void MqttClient::run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) {
|
void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) {
|
||||||
|
//lock the variable, it automaticly unlocks when going out of scope
|
||||||
std::lock_guard<std::mutex> lock(client_.messageMutex_);
|
std::lock_guard<std::mutex> lock(client_.messageMutex_);
|
||||||
client_.lastMessage_ = msg->to_string();
|
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
|
/// @brief Get the last message received from the MQTT broker
|
||||||
/// @return The last message received in a string
|
/// @return The last message received in a string
|
||||||
|
//std::string is the datatype of the return value
|
||||||
std::string MqttClient::getLastMessage() {
|
std::string MqttClient::getLastMessage() {
|
||||||
|
//lock the variable, it automaticly unlocks when going out of scope
|
||||||
std::lock_guard<std::mutex> lock(messageMutex_);
|
std::lock_guard<std::mutex> lock(messageMutex_);
|
||||||
return lastMessage_;
|
return lastMessage_;
|
||||||
}
|
}
|
@@ -27,7 +27,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
mqtt::async_client client_;
|
mqtt::async_client client_;
|
||||||
mqtt::connect_options connOpts_;
|
mqtt::connect_options options;
|
||||||
Callback callback_;
|
Callback callback_;
|
||||||
std::string username_;
|
std::string username_;
|
||||||
std::string password_;
|
std::string password_;
|
||||||
|
@@ -10,7 +10,8 @@ using namespace std;
|
|||||||
CKobuki robot;
|
CKobuki robot;
|
||||||
int movement();
|
int movement();
|
||||||
std::string readMQTT();
|
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(){
|
void setup(){
|
||||||
@@ -18,7 +19,7 @@ void setup(){
|
|||||||
robot.startCommunication("/dev/ttyUSB0", true, null_ptr);
|
robot.startCommunication("/dev/ttyUSB0", true, null_ptr);
|
||||||
client.connect();
|
client.connect();
|
||||||
client.subscribe("home/commands");
|
client.subscribe("home/commands");
|
||||||
|
parseMQTT(readMQTT());
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
@@ -42,6 +43,22 @@ std::string readMQTT()
|
|||||||
return message;
|
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 movement()
|
||||||
{
|
{
|
||||||
int text;
|
int text;
|
||||||
|
Reference in New Issue
Block a user