1publish(3)                 Library Functions Manual                 publish(3)
2
3
4

NAME

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