1pubasync(3) Library Functions Manual pubasync(3)
2
3
4
6 pubasync - Asynchronous publication example
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "MQTTClient.h"
12
13 #if !defined(_WIN32)
14 #include <unistd.h>
15 #else
16 #include <windows.h>
17 #endif
18
19 #define ADDRESS "tcp://mqtt.eclipse.org:1883"
20 #define CLIENTID "ExampleClientPub"
21 #define TOPIC "MQTT Examples"
22 #define PAYLOAD "Hello World!"
23 #define QOS 1
24 #define TIMEOUT 10000L
25
26 MQTTClient_deliveryToken deliveredtoken;
27
28 void delivered(void *context, MQTTClient_deliveryToken dt)
29 {
30 printf("Message with token value %d delivery confirmed\n", dt);
31 deliveredtoken = dt;
32 }
33
34 int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
35 {
36 printf("Message arrived\n");
37 printf(" topic: %s\n", topicName);
38 printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
39 MQTTClient_freeMessage(&message);
40 MQTTClient_free(topicName);
41 return 1;
42 }
43
44 void connlost(void *context, char *cause)
45 {
46 printf("\nConnection lost\n");
47 printf(" cause: %s\n", cause);
48 }
49
50 int main(int argc, char* argv[])
51 {
52 MQTTClient client;
53 MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
54 MQTTClient_message pubmsg = MQTTClient_message_initializer;
55 MQTTClient_deliveryToken token;
56 int rc;
57
58 if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
59 MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
60 {
61 printf("Failed to create client, return code %d\n", rc);
62 rc = EXIT_FAILURE;
63 goto exit;
64 }
65
66 if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
67 {
68 printf("Failed to set callbacks, return code %d\n", rc);
69 rc = EXIT_FAILURE;
70 goto destroy_exit;
71 }
72
73 conn_opts.keepAliveInterval = 20;
74 conn_opts.cleansession = 1;
75 if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
76 {
77 printf("Failed to connect, return code %d\n", rc);
78 rc = EXIT_FAILURE;
79 goto destroy_exit;
80 }
81
82 pubmsg.payload = PAYLOAD;
83 pubmsg.payloadlen = (int)strlen(PAYLOAD);
84 pubmsg.qos = QOS;
85 pubmsg.retained = 0;
86 deliveredtoken = 0;
87 if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
88 {
89 printf("Failed to publish message, return code %d\n", rc);
90 rc = EXIT_FAILURE;
91 }
92 else
93 {
94 printf("Waiting for publication of %s\n"
95 "on topic %s for client with ClientID: %s\n",
96 PAYLOAD, TOPIC, CLIENTID);
97 while (deliveredtoken != token)
98 {
99 #if defined(_WIN32)
100 Sleep(100);
101 #else
102 usleep(10000L);
103 #endif
104 }
105 }
106
107 if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
108 {
109 printf("Failed to disconnect, return code %d\n", rc);
110 rc = EXIT_FAILURE;
111 }
112
113 destroy_exit:
114 MQTTClient_destroy(&client);
115
116 exit:
117 return rc;
118 }
119
120
121Paho MQTT C Client Library Thu Jul 20 2023 00:00:00 pubasync(3)