-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqtt_pub.cpp
111 lines (97 loc) · 3.08 KB
/
mqtt_pub.cpp
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
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
// MQTT 发布者
#include "config.h"
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MQTTClient.h>
#include <unistd.h>
#ifdef WITH_SSL
#define ADDRESS "ssl://localhost:8883"
#else
#define ADDRESS "tcp://localhost:1883"
#endif
#define CLIENTID "hello_pub"
#define TOPIC "Hello"
#define PAYLOAD "Hello, World!"
#define QOS 1
#define TIMEOUT 10000L
#define USERNAME "hello_pub"
#define PASSWORD "123456"
#define DISCONNECT "hello_pub is died"
int CONNECT = 1;
volatile MQTTClient_deliveryToken deliveredtoken;
void delivered(void *context, MQTTClient_deliveryToken dt)
{
printf("Message with token value %d delivery confirmed\n", dt);
deliveredtoken = dt;
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
int i;
char* payloadptr;
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: ");
payloadptr = (char*)message->payload;
if(strcmp(payloadptr, DISCONNECT) == 0)
{
printf(" \n out!!");
CONNECT = 0;
}
for(i=0; i<message->payloadlen; i++)
{
putchar(*payloadptr++);
}
printf("\n");
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
return 1;
}
void connlost(void *context, char *cause)
{
printf("\nConnection lost\n");
printf(" cause: %s\n", cause);
}
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = USERNAME;
conn_opts.password = PASSWORD;
#ifdef WITH_SSL
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
conn_opts.ssl = &ssl_opts;
conn_opts.ssl->trustStore = "/home/lxl/Develop/myCA/ca.crt";
conn_opts.ssl->keyStore = "/home/lxl/Develop/myCA/client.crt";
conn_opts.ssl->privateKey = "/home/lxl/Develop/myCA/client.key";
conn_opts.ssl->enableServerCertAuth = true; // 双向认证
#endif
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
pubmsg.payload = const_cast<char *>(PAYLOAD);
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
while(CONNECT)
{
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for up to %d seconds for publication of %s\n"
"on topic %s for client with ClientID: %s\n",(int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
usleep(3000000L);
}
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
}