Commit 26f938772b29c8c42de9c6596b4b4a99c3e2cbb4

Authored by Peter M. Groen
1 parent c6669435

Added examples.

Showing 1 changed file with 112 additions and 1 deletions
README.md
@@ -41,4 +41,115 @@ later the "pub" part. If all went well, you should see two screens in sync runni @@ -41,4 +41,115 @@ later the "pub" part. If all went well, you should see two screens in sync runni
41 Open terminal 2 41 Open terminal 2
42 $ bin/test_mqtt_pub 42 $ bin/test_mqtt_pub
43 ``` 43 ```
44 -Screen 2 is sending, Screen 1 is receiving.  
45 \ No newline at end of file 44 \ No newline at end of file
  45 +Screen 2 is sending, Screen 1 is receiving.
  46 +
  47 +# Using the library.
  48 +Using mqtt-cpp is pretty straight forward. No real magic is happening. Beware of the fact that the library uses the namespace :
  49 +```
  50 + osdev::components::mqtt
  51 +```
  52 +
  53 +## Publishing..
  54 +To create a publisher, a simple member needs to be created.
  55 +Header :
  56 +```
  57 + #pragma once
  58 +
  59 + // std
  60 + #include <memory>
  61 + #include <string>
  62 +
  63 + // osdev::components::mqtt
  64 + #include "mqttclient.h"
  65 + #include "compat-c++14.h"
  66 +
  67 + class Publisher
  68 + {
  69 + public:
  70 + Publisher(const std::string &client_id);
  71 +
  72 + virtual ~Publisher() {}
  73 +
  74 + void connect( const std::string &hostname, int portnumber = 1883, const std::string &username = std::string(), const std::string &password = std::string() );
  75 +
  76 + void publish( const std::string &message_topic, const std::string &message_payload );
  77 +
  78 + private:
  79 + osdev::components::mqtt::MqttClient m_mqtt_client;
  80 + };
  81 +```
  82 +
  83 +Implementation
  84 +```
  85 +
  86 + // osdev::components::mqtt
  87 + // #include "token.h"
  88 +
  89 + // mqtt_tests
  90 + #include "publisher.h"
  91 +
  92 + Publisher::Publisher(const std::string &client_id)
  93 + : m_mqtt_client( client_id )
  94 + {
  95 +
  96 + }
  97 +
  98 + void Publisher::connect( const std::string &hostname, int portnumber, const std::string &username, const std::string &password )
  99 + {
  100 + m_mqtt_client.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ) );
  101 + std::cout << "Client state : " << m_mqtt_client.state() << std::endl;
  102 + }
  103 +
  104 + void Publisher::publish( const std::string &message_topic, const std::string &message_payload )
  105 + {
  106 + osdev::components::mqtt::MqttMessage message( message_topic, true, false, message_payload );
  107 + std::cout << "[Publisher::publish] - Publising message : " << message_payload << " to topic : " << message_topic << std::endl;
  108 + osdev::components::mqtt::Token t_result = m_mqtt_client.publish( message, 0 );
  109 + }
  110 +```
  111 +
  112 +## Subscribing
  113 +TO create a subscriber, a bit more work is involved. A subscription is dependant on a callback in which the data is handled to your liking. It is necessary to inherit from MqttSubscriberBase and override the "receive_data" method.
  114 +
  115 +subscriber.h
  116 +```
  117 +#pragma once
  118 +
  119 +// std
  120 +#include <string>
  121 +
  122 +// mqtt-cpp
  123 +#include "mqttsubscriberbase.h"
  124 +
  125 +class Subscriber : public MqttSubscriberBase
  126 +{
  127 +public:
  128 + Subscriber( const std::string &client_id );
  129 +
  130 + virtual ~Subscriber() {}
  131 +
  132 +protected:
  133 + void receive_data( const std::string &message_topic, const std::string &message_payload );
  134 +
  135 +};
  136 +```
  137 +
  138 +In the receive_data, the logic will be implemented to handle, decode and store the payload.
  139 +```
  140 +#include "subscriber.h"
  141 +
  142 +#include <iostream>
  143 +
  144 +Subscriber::Subscriber( const std::string &client_id )
  145 + : MqttSubscriberBase( client_id )
  146 +{
  147 +
  148 +}
  149 +
  150 +void Subscriber::receive_data( const std::string &message_topic, const std::string &message_payload )
  151 +{
  152 + std::cout << "[Subscriber::receive_data] - Received message : " << message_payload << " from topic : " << message_topic << std::endl;
  153 +}
  154 +```
  155 +
  156 +Now it will be very easy to send and receive messages from a broker without the hassle of administration.