Created
July 10, 2019 09:57
-
-
Save landersson/29d4f2449bef1ab5e98235a2981ec4bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mqtt/client.h> | |
#include <mqtt/callback.h> | |
#include <string> | |
struct MqttConnection : public mqtt::callback { | |
bool connect(); | |
bool disconnect(); | |
bool transmit(const mqtt::message_ptr msg); | |
// mqtt::callback functions | |
void connected(const mqtt::string &cause) override; | |
void connection_lost(const mqtt::string &cause) override; | |
void message_arrived(mqtt::const_message_ptr msg) override; | |
void delivery_complete(mqtt::delivery_token_ptr tok) override; | |
void create_client(); | |
bool is_connected(); | |
std::shared_ptr<mqtt::client> mqtt_client_; | |
}; | |
bool MqttConnection::connect() { | |
if (!is_connected()) { | |
create_client(); | |
try { | |
mqtt::connect_options options; | |
options.set_automatic_reconnect(true); | |
options.set_mqtt_version(MQTTVERSION_3_1); | |
options.set_clean_session(true); | |
mqtt_client_->connect(options); | |
mqtt_client_->set_timeout(10000); | |
printf("Connect finished\n"); | |
} catch (const mqtt::exception &mqtt_exception) { | |
printf("mqtt_exception while connecting to MQTT Broker! %s\n", | |
mqtt_exception.to_string().c_str()); | |
} catch (const std::exception &exception) { | |
printf("exception while connecting to MQTT Broker! %s\n", exception.what()); | |
} | |
} | |
return is_connected(); | |
} | |
bool MqttConnection::disconnect() { | |
bool result = false; | |
if (is_connected()) { | |
if (mqtt_client_->is_connected()) { | |
mqtt_client_->disconnect(); | |
} | |
mqtt_client_.reset(); | |
result = true; | |
} | |
return result; | |
} | |
bool MqttConnection::transmit(const mqtt::message_ptr msg) { | |
bool success = false; | |
if (is_connected()) { | |
try { | |
auto client_id = mqtt_client_->get_client_id(); | |
printf("MqttConnection::Transmit, client_id=%s\n", client_id.c_str()); | |
mqtt_client_->publish(msg); | |
success = true; | |
} catch (const std::exception &exception) { | |
printf("Failed to publish MQTT message: %s\n", exception.what()); | |
} | |
} | |
return success; | |
} | |
void MqttConnection::connected(const mqtt::string &cause) { | |
printf("Connected to MQTT broker: '%s'\n", cause.c_str()); | |
} | |
void MqttConnection::connection_lost(const mqtt::string &cause) { | |
printf("Lost connection to MQTT broker: '%s'\n", cause.c_str()); | |
} | |
void MqttConnection::message_arrived(mqtt::const_message_ptr msg) { | |
printf("MQTT message arrived!\n"); | |
} | |
void MqttConnection::delivery_complete(mqtt::delivery_token_ptr tok) { | |
printf("MQTT message delivered, msgId:%d returnCode:%d\n", | |
tok->get_message_id(), | |
tok->get_return_code()); | |
} | |
void MqttConnection::create_client() { | |
if (!mqtt_client_) { | |
mqtt_client_ = std::unique_ptr<mqtt::client>( | |
new mqtt::client("127.0.0.1:20000", | |
"TEST", // Client ID | |
0, // Buffered messages | |
nullptr)); // No persistence storage | |
mqtt_client_->set_callback(*this); | |
} | |
} | |
bool MqttConnection::is_connected() { | |
bool result = false; | |
try { | |
if (mqtt_client_) { | |
result = mqtt_client_->is_connected(); | |
} | |
} catch (const mqtt::exception &mqtt_exception) { | |
printf("mqtt_exception while accessing is_connected()! %s\n", mqtt_exception.to_string().c_str()); | |
} catch (const std::exception &exception) { | |
printf("exception while accessing is_connected()! %s\n", exception.what()); | |
} | |
return result; | |
} | |
unsigned char payload[] = { | |
0x0a, 0x1e, 0x0a, 0x0b, 0x54, 0x45, 0x53, 0x54, 0x5f, 0x44, 0x45, 0x56, | |
0x49, 0x43, 0x45, 0x10, 0xb4, 0xf4, 0xbd, 0xb0, 0xe9, 0x85, 0xf0, 0xc1, | |
0x15, 0x20, 0x8a, 0xcb, 0xcb, 0xd8, 0xbd, 0x2d, 0x12, 0x0e, 0x0a, 0x0c, | |
0x47, 0x65, 0x74, 0x49, 0x6e, 0x76, 0x65, 0x6e, 0x74, 0x6f, 0x72, 0x79, | |
0x12, 0x17, 0x0a, 0x15, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x46, | |
0x69, 0x72, 0x6d, 0x77, 0x61, 0x72, 0x65, 0x55, 0x70, 0x64, 0x61, 0x74, | |
0x65 | |
}; | |
int main() | |
{ | |
MqttConnection mqtt_client; | |
mqtt_client.connect(); | |
std::string msg_payload(payload, payload + sizeof(payload) / sizeof(payload[0])); | |
auto mqtt_msg = mqtt::make_message("mo/pb2/initV1/swedspot/KALLE", msg_payload); | |
mqtt_msg->set_qos(1); | |
mqtt_client.transmit(mqtt_msg); | |
mqtt_client.disconnect(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment