-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (53 loc) · 2.4 KB
/
Copy pathmain.cpp
File metadata and controls
80 lines (53 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
#include <iostream>
#include <thread>
#include "MQTTClient.h"
void test_mqtt_certs(const std::string &host, unsigned int port)
{
MQTTClient mqtt_client(host, port);
mqtt_client.set_auth_method(AUTH_METHOD::CERTS);
if (!mqtt_client.set_credentials("usuario", "usuario"))
std::cout << "Error setting credentials" << std::endl;
if (!mqtt_client.set_cafile("ca.crt"))
std::cout << "Error setting CA key" << std::endl;
if (!mqtt_client.connect())
std::cout << "Error connecting" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
if (!mqtt_client.subscribe("topic"))
std::cout << "Error subscribing to topic" << std::endl;
if (!mqtt_client.publish_message("topic", "Hola mundo"))
std::cout << "Error publishing msg" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
if (!mqtt_client.unsubscribe("topic"))
std::cout << "Error unsubscribing from topic" << std::endl;
if (!mqtt_client.publish_message("topic", "Adios mundo"))
std::cout << "Error publishing msg" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void test_mqtt_psk(const std::string &host, unsigned int port)
{
MQTTClient mqtt_client(host, port);
mqtt_client.set_auth_method(AUTH_METHOD::PSK);
if (!mqtt_client.set_psk("usuario", "70617373776f726473757065726368756e6761"))
std::cout << "Error setting PSK key" << std::endl;
if (!mqtt_client.connect())
std::cout << "Error connecting" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
if (!mqtt_client.subscribe("topic"))
std::cout << "Error subscribing to topic" << std::endl;
if (!mqtt_client.publish_message("topic", "Hola mundo"))
std::cout << "Error publishing msg" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
if (!mqtt_client.unsubscribe("topic"))
std::cout << "Error unsubscribing from topic" << std::endl;
if (!mqtt_client.publish_message("topic", "Adios mundo"))
std::cout << "Error publishing msg" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main(int argc, char **argv)
{
std::cout << "**** Attemping to test MQTT stuff... ****\n\n";
test_mqtt_psk("localhost", 8883);
test_mqtt_certs("localhost", 8883);
std::cout << "Test finished" << std::endl;
return 0;
}