1publish(3) Library Functions Manual publish(3)
2
3
4
6 publish - Publication example
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "MQTTAsync.h"
13
14 #if !defined(_WIN32)
15 #include <unistd.h>
16 #else
17 #include <windows.h>
18 #endif
19
20 #if defined(_WRS_KERNEL)
21 #include <OsWrapper.h>
22 #endif
23
24 #define ADDRESS "tcp://mqtt.eclipse.org:1883"
25 #define CLIENTID "ExampleClientPub"
26 #define TOPIC "MQTT Examples"
27 #define PAYLOAD "Hello World!"
28 #define QOS 1
29 #define TIMEOUT 10000L
30
31 int finished = 0;
32
33 void connlost(void *context, char *cause)
34 {
35 MQTTAsync client = (MQTTAsync)context;
36 MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
37 int rc;
38
39 printf("0onnection lost0);
40 printf(" cause: %s0, cause);
41
42 printf("Reconnecting0);
43 conn_opts.keepAliveInterval = 20;
44 conn_opts.cleansession = 1;
45 if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
46 {
47 printf("Failed to start connect, return code %d0, rc);
48 finished = 1;
49 }
50 }
51
52 void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
53 {
54 printf("Disconnect failed0);
55 finished = 1;
56 }
57
58 void onDisconnect(void* context, MQTTAsync_successData* response)
59 {
60 printf("Successful disconnection0);
61 finished = 1;
62 }
63
64 void onSendFailure(void* context, MQTTAsync_failureData* response)
65 {
66 MQTTAsync client = (MQTTAsync)context;
67 MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
68 int rc;
69
70 printf("Message send failed token %d error code %d0, response->token, response->code);
71 opts.onSuccess = onDisconnect;
72 opts.onFailure = onDisconnectFailure;
73 opts.context = client;
74 if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
75 {
76 printf("Failed to start disconnect, return code %d0, rc);
77 exit(EXIT_FAILURE);
78 }
79 }
80
81 void onSend(void* context, MQTTAsync_successData* response)
82 {
83 MQTTAsync client = (MQTTAsync)context;
84 MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
85 int rc;
86
87 printf("Message with token value %d delivery confirmed0, response->token);
88 opts.onSuccess = onDisconnect;
89 opts.onFailure = onDisconnectFailure;
90 opts.context = client;
91 if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
92 {
93 printf("Failed to start disconnect, return code %d0, rc);
94 exit(EXIT_FAILURE);
95 }
96 }
97
98
99 void onConnectFailure(void* context, MQTTAsync_failureData* response)
100 {
101 printf("Connect failed, rc %d0, response ? response->code : 0);
102 finished = 1;
103 }
104
105
106 void onConnect(void* context, MQTTAsync_successData* response)
107 {
108 MQTTAsync client = (MQTTAsync)context;
109 MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
110 MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
111 int rc;
112
113 printf("Successful connection0);
114 opts.onSuccess = onSend;
115 opts.onFailure = onSendFailure;
116 opts.context = client;
117 pubmsg.payload = PAYLOAD;
118 pubmsg.payloadlen = (int)strlen(PAYLOAD);
119 pubmsg.qos = QOS;
120 pubmsg.retained = 0;
121 if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
122 {
123 printf("Failed to start sendMessage, return code %d0, rc);
124 exit(EXIT_FAILURE);
125 }
126 }
127
128 int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
129 {
130 // not expecting any messages
131 return 1;
132 }
133
134 int main(int argc, char* argv[])
135 {
136 MQTTAsync client;
137 MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
138 int rc;
139
140 if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS)
141 {
142 printf("Failed to create client object, return code %d0, rc);
143 exit(EXIT_FAILURE);
144 }
145
146 if ((rc = MQTTAsync_setCallbacks(client, NULL, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
147 {
148 printf("Failed to set callback, return code %d0, rc);
149 exit(EXIT_FAILURE);
150 }
151
152 conn_opts.keepAliveInterval = 20;
153 conn_opts.cleansession = 1;
154 conn_opts.onSuccess = onConnect;
155 conn_opts.onFailure = onConnectFailure;
156 conn_opts.context = client;
157 if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
158 {
159 printf("Failed to start connect, return code %d0, rc);
160 exit(EXIT_FAILURE);
161 }
162
163 printf("Waiting for publication of %s0
164 "on topic %s for client with ClientID: %s0,
165 PAYLOAD, TOPIC, CLIENTID);
166 while (!finished)
167 #if defined(_WIN32)
168 Sleep(100);
169 #else
170 usleep(10000L);
171 #endif
172
173 MQTTAsync_destroy(&client);
174 return rc;
175 }
176
177
178Paho Asynchronous MQTT C Client LSiabtraMrayy 29 2021 publish(3)