mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-04 12:24:57 +00:00
can send messages to server from cpp
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#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_ is the connection
|
||||||
: 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_);
|
||||||
options.set_clean_session(true);
|
options.set_clean_session(true);
|
||||||
@@ -32,6 +33,16 @@ void MqttClient::subscribe(const std::string& topic, int qos) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MqttClient::publishMessage(const std::string& topic, const std::string& payload) {
|
||||||
|
try {
|
||||||
|
std::cout << "Publishing message: " << payload << std::endl;
|
||||||
|
client_.publish(topic, payload)->wait();
|
||||||
|
} catch (const mqtt::exception& exc) {
|
||||||
|
std::cerr << "Error: " << exc.what() << std::endl;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief Only needed when program doesnt keep itself alive
|
/// @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
|
||||||
@@ -41,7 +52,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
|
//lock the variable, it automaticly unlocks when going out of scope using lock_guard
|
||||||
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();
|
||||||
}
|
}
|
||||||
@@ -58,7 +69,7 @@ void MqttClient::Callback::delivery_complete(mqtt::delivery_token_ptr token) {
|
|||||||
/// @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 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
|
//lock the variable, it automaticly unlocks when going out of scope using lock_guard
|
||||||
std::lock_guard<std::mutex> lock(messageMutex_);
|
std::lock_guard<std::mutex> lock(messageMutex_);
|
||||||
return lastMessage_;
|
return lastMessage_;
|
||||||
}
|
}
|
@@ -13,6 +13,7 @@ public:
|
|||||||
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();
|
std::string getLastMessage();
|
||||||
|
void publishMessage(const std::string& topic, const std::string& payload);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class Callback : public virtual mqtt::callback {
|
class Callback : public virtual mqtt::callback {
|
||||||
|
@@ -24,12 +24,13 @@ void setup(){
|
|||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
setup();
|
setup();
|
||||||
std::thread safety([&]() { robot.robotSafety(&message); });
|
// std::thread safety([&]() { robot.robotSafety(&message); });
|
||||||
while(true){
|
// while(true){
|
||||||
parseMQTT(readMQTT());
|
// parseMQTT(readMQTT());
|
||||||
}
|
// }
|
||||||
safety.join();
|
// safety.join();
|
||||||
return 0;
|
// return 0;
|
||||||
|
client.publishMessage("kobuki/data", "aaaa");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string readMQTT()
|
std::string readMQTT()
|
||||||
|
Reference in New Issue
Block a user