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

NAME

6       pubsync - Synchronous publication example
7
8       #include <stdio.h>
9       #include <stdlib.h>
10       #include <string.h>
11       #include "MQTTClient.h"
12
13       #define ADDRESS     "tcp://mqtt.eclipse.org:1883"
14       #define CLIENTID    "ExampleClientPub"
15       #define TOPIC       "MQTT Examples"
16       #define PAYLOAD     "Hello World!"
17       #define QOS         1
18       #define TIMEOUT     10000L
19
20       int main(int argc, char* argv[])
21       {
22           MQTTClient client;
23           MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
24           MQTTClient_message pubmsg = MQTTClient_message_initializer;
25           MQTTClient_deliveryToken token;
26           int rc;
27
28           if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
29               MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
30           {
31                printf("Failed to create client, return code %d\n", rc);
32                exit(EXIT_FAILURE);
33           }
34
35           conn_opts.keepAliveInterval = 20;
36           conn_opts.cleansession = 1;
37           if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
38           {
39               printf("Failed to connect, return code %d\n", rc);
40               exit(EXIT_FAILURE);
41           }
42
43           pubmsg.payload = PAYLOAD;
44           pubmsg.payloadlen = (int)strlen(PAYLOAD);
45           pubmsg.qos = QOS;
46           pubmsg.retained = 0;
47           if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
48           {
49                printf("Failed to publish message, return code %d\n", rc);
50                exit(EXIT_FAILURE);
51           }
52
53           printf("Waiting for up to %d seconds for publication of %s\n"
54                   "on topic %s for client with ClientID: %s\n",
55                   (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
56           rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
57           printf("Message with delivery token %d delivered\n", token);
58
59           if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
60               printf("Failed to disconnect, return code %d\n", rc);
61           MQTTClient_destroy(&client);
62           return rc;
63       }
64
65
66Paho MQTT C Client Library Thu Jul 20 2023 00:00:00                 pubsync(3)
Impressum