66e26bc1
Peter M. Groen
Updated dependencies
|
1
|
# MQTT-CPP
|
e0b1fe64
Peter M. Groen
Fixed script.
|
2
3
4
5
|
Modern, asynchronous and fast C++ client for paho-mqtt ( paho-c ).
**Features:**
* Simple and clean A-synchronous API. ( Connect, publish, subscribe ).
|
66e26bc1
Peter M. Groen
Updated dependencies
|
6
|
* Multi-threaded. Use multiple topics in multiple threads..
|
e0b1fe64
Peter M. Groen
Fixed script.
|
7
8
9
10
|
* Callbacks can be lambdas, class methods, bind expressions, or any [std::function]
* Fully autonomous reconnecting
# Dependencies
|
66e26bc1
Peter M. Groen
Updated dependencies
|
11
12
|
This library has dependencies on libpaho-mqtt3a.so and Boost ( RegEx and Core ).
Changes are there is a version for your platform ( Debian, Fedora, CentOS ).
|
e0b1fe64
Peter M. Groen
Fixed script.
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
Just check your package manager for the correct package-name.
# Tutorial
* Clone this repository :
```
git clone http://gitlab.osdev.nl/open_source/mqtt-cpp.git
```
* Change to the repo and run the submodules script :
```
$ cd mqtt-cpp
$ scripts/setup_submodules -i
```
This will add the cmake directory and versioning.
* Create a build directory and start the build.
```
$ mkdir build
$ cd build
$ cmake ../
|
ee6fece2
Peter M. Groen
Update Readme
|
31
|
$ gmake ( or make, depending on your platform )
|
e0b1fe64
Peter M. Groen
Fixed script.
|
32
|
```
|
44481cf5
Peter M. Groen
Updated the docum...
|
33
34
35
36
37
38
39
40
41
|
## Building the examples and tests
### Tests
Normally only the library is being build. By adding `-DENABLE_TESTING` to the cmake step, the unittests are being build also. Make sure a broker is running on localhost and type `test/mqtt_test` to run the tests.
### Examples
By adding the flag `-DENABLE_EXAMPLES` to the cmake step, the examples are being build together with the library.
In build/bin there are two examples, test_mqtt_pu and test_mqtt_sub. Have a broker running, like mosquitto or flashmq capable of accepting anonymous connections. Start the "sub" part and couple of moments later the "pub" part. If all went well, you should see two screens in sync running.
|
0bf1b2f7
Peter M. Groen
Fixed script.
|
42
43
44
45
46
47
48
49
|
```
Open terminal 1
$ bin/test_mqtt_sub
Open terminal 2
$ bin/test_mqtt_pub
```
|
44481cf5
Peter M. Groen
Updated the docum...
|
50
|
|
26f93877
Peter M. Groen
Added examples.
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
Screen 2 is sending, Screen 1 is receiving.
# Using the library.
Using mqtt-cpp is pretty straight forward. No real magic is happening. Beware of the fact that the library uses the namespace :
```
osdev::components::mqtt
```
## Publishing..
To create a publisher, a simple member needs to be created.
Header :
```
#pragma once
// std
#include <memory>
#include <string>
// osdev::components::mqtt
#include "mqttclient.h"
#include "compat-c++14.h"
class Publisher
{
public:
Publisher(const std::string &client_id);
virtual ~Publisher() {}
void connect( const std::string &hostname, int portnumber = 1883, const std::string &username = std::string(), const std::string &password = std::string() );
void publish( const std::string &message_topic, const std::string &message_payload );
private:
osdev::components::mqtt::MqttClient m_mqtt_client;
};
```
Implementation
```
// osdev::components::mqtt
// #include "token.h"
// mqtt_tests
#include "publisher.h"
Publisher::Publisher(const std::string &client_id)
: m_mqtt_client( client_id )
{
}
void Publisher::connect( const std::string &hostname, int portnumber, const std::string &username, const std::string &password )
{
m_mqtt_client.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ) );
std::cout << "Client state : " << m_mqtt_client.state() << std::endl;
}
void Publisher::publish( const std::string &message_topic, const std::string &message_payload )
{
osdev::components::mqtt::MqttMessage message( message_topic, true, false, message_payload );
std::cout << "[Publisher::publish] - Publising message : " << message_payload << " to topic : " << message_topic << std::endl;
osdev::components::mqtt::Token t_result = m_mqtt_client.publish( message, 0 );
}
```
## Subscribing
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.
subscriber.h
```
#pragma once
// std
#include <string>
// mqtt-cpp
#include "mqttsubscriberbase.h"
class Subscriber : public MqttSubscriberBase
{
public:
Subscriber( const std::string &client_id );
virtual ~Subscriber() {}
protected:
void receive_data( const std::string &message_topic, const std::string &message_payload );
};
```
In the receive_data, the logic will be implemented to handle, decode and store the payload.
```
#include "subscriber.h"
#include <iostream>
Subscriber::Subscriber( const std::string &client_id )
: MqttSubscriberBase( client_id )
{
}
void Subscriber::receive_data( const std::string &message_topic, const std::string &message_payload )
{
std::cout << "[Subscriber::receive_data] - Received message : " << message_payload << " from topic : " << message_topic << std::endl;
}
```
|
ee6fece2
Peter M. Groen
Update Readme
|
162
|
Now it will be very easy to send and receive messages from a broker without the hassle of administration.
|