1subscribe(3) Library Functions Manual subscribe(3)
2
3
4
6 subscribe - Subscription example
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "MQTTAsync.h"
12
13 #if !defined(_WIN32)
14 #include <unistd.h>
15 #else
16 #include <windows.h>
17 #endif
18
19 #if defined(_WRS_KERNEL)
20 #include <OsWrapper.h>
21 #endif
22
23 #define ADDRESS "tcp://mqtt.eclipse.org:1883"
24 #define CLIENTID "ExampleClientSub"
25 #define TOPIC "MQTT Examples"
26 #define PAYLOAD "Hello World!"
27 #define QOS 1
28 #define TIMEOUT 10000L
29
30 int disc_finished = 0;
31 int subscribed = 0;
32 int finished = 0;
33
34 void connlost(void *context, char *cause)
35 {
36 MQTTAsync client = (MQTTAsync)context;
37 MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
38 int rc;
39
40 printf("\nConnection lost\n");
41 if (cause)
42 printf(" cause: %s\n", cause);
43
44 printf("Reconnecting\n");
45 conn_opts.keepAliveInterval = 20;
46 conn_opts.cleansession = 1;
47 if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
48 {
49 printf("Failed to start connect, return code %d\n", rc);
50 finished = 1;
51 }
52 }
53
54
55 int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
56 {
57 printf("Message arrived\n");
58 printf(" topic: %s\n", topicName);
59 printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
60 MQTTAsync_freeMessage(&message);
61 MQTTAsync_free(topicName);
62 return 1;
63 }
64
65 void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
66 {
67 printf("Disconnect failed, rc %d\n", response->code);
68 disc_finished = 1;
69 }
70
71 void onDisconnect(void* context, MQTTAsync_successData* response)
72 {
73 printf("Successful disconnection\n");
74 disc_finished = 1;
75 }
76
77 void onSubscribe(void* context, MQTTAsync_successData* response)
78 {
79 printf("Subscribe succeeded\n");
80 subscribed = 1;
81 }
82
83 void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
84 {
85 printf("Subscribe failed, rc %d\n", response->code);
86 finished = 1;
87 }
88
89
90 void onConnectFailure(void* context, MQTTAsync_failureData* response)
91 {
92 printf("Connect failed, rc %d\n", response->code);
93 finished = 1;
94 }
95
96
97 void onConnect(void* context, MQTTAsync_successData* response)
98 {
99 MQTTAsync client = (MQTTAsync)context;
100 MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
101 int rc;
102
103 printf("Successful connection\n");
104
105 printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
106 "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
107 opts.onSuccess = onSubscribe;
108 opts.onFailure = onSubscribeFailure;
109 opts.context = client;
110 if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
111 {
112 printf("Failed to start subscribe, return code %d\n", rc);
113 finished = 1;
114 }
115 }
116
117
118 int main(int argc, char* argv[])
119 {
120 MQTTAsync client;
121 MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
122 MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
123 int rc;
124 int ch;
125
126 if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
127 != MQTTASYNC_SUCCESS)
128 {
129 printf("Failed to create client, return code %d\n", rc);
130 rc = EXIT_FAILURE;
131 goto exit;
132 }
133
134 if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS)
135 {
136 printf("Failed to set callbacks, return code %d\n", rc);
137 rc = EXIT_FAILURE;
138 goto destroy_exit;
139 }
140
141 conn_opts.keepAliveInterval = 20;
142 conn_opts.cleansession = 1;
143 conn_opts.onSuccess = onConnect;
144 conn_opts.onFailure = onConnectFailure;
145 conn_opts.context = client;
146 if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
147 {
148 printf("Failed to start connect, return code %d\n", rc);
149 rc = EXIT_FAILURE;
150 goto destroy_exit;
151 }
152
153 while (!subscribed && !finished)
154 #if defined(_WIN32)
155 Sleep(100);
156 #else
157 usleep(10000L);
158 #endif
159
160 if (finished)
161 goto exit;
162
163 do
164 {
165 ch = getchar();
166 } while (ch!='Q' && ch != 'q');
167
168 disc_opts.onSuccess = onDisconnect;
169 disc_opts.onFailure = onDisconnectFailure;
170 if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
171 {
172 printf("Failed to start disconnect, return code %d\n", rc);
173 rc = EXIT_FAILURE;
174 goto destroy_exit;
175 }
176 while (!disc_finished)
177 {
178 #if defined(_WIN32)
179 Sleep(100);
180 #else
181 usleep(10000L);
182 #endif
183 }
184
185 destroy_exit:
186 MQTTAsync_destroy(&client);
187 exit:
188 return rc;
189 }
190
191
192Paho Asynchronous MQTT C Client LTihburaJrayn 19 2023 subscribe(3)