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

NAME

6       subasync - Asynchronous subscription example
7
8
9       #include <stdio.h>
10       #include <stdlib.h>
11       #include <string.h>
12       #include "MQTTClient.h"
13
14       #define ADDRESS     "tcp://mqtt.eclipse.org:1883"
15       #define CLIENTID    "ExampleClientSub"
16       #define TOPIC       "MQTT Examples"
17       #define PAYLOAD     "Hello World!"
18       #define QOS         1
19       #define TIMEOUT     10000L
20
21       volatile MQTTClient_deliveryToken deliveredtoken;
22
23       void delivered(void *context, MQTTClient_deliveryToken dt)
24       {
25           printf("Message with token value %d delivery confirmed0, dt);
26           deliveredtoken = dt;
27       }
28
29       int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
30       {
31           printf("Message arrived0);
32           printf("     topic: %s0, topicName);
33           printf("   message: %.*s0, message->payloadlen, (char*)message->payload);
34           MQTTClient_freeMessage(&message);
35           MQTTClient_free(topicName);
36           return 1;
37       }
38
39       void connlost(void *context, char *cause)
40       {
41           printf("0onnection lost0);
42           printf("     cause: %s0, cause);
43       }
44
45       int main(int argc, char* argv[])
46       {
47           MQTTClient client;
48           MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
49           int rc;
50
51           if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
52               MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
53           {
54               printf("Failed to create client, return code %d0, rc);
55               rc = EXIT_FAILURE;
56               goto exit;
57           }
58
59           if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
60           {
61               printf("Failed to set callbacks, return code %d0, rc);
62               rc = EXIT_FAILURE;
63               goto destroy_exit;
64           }
65
66           conn_opts.keepAliveInterval = 20;
67           conn_opts.cleansession = 1;
68           if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
69           {
70               printf("Failed to connect, return code %d0, rc);
71               rc = EXIT_FAILURE;
72               goto destroy_exit;
73           }
74
75           printf("Subscribing to topic %s0or client %s using QoS%d0
76                  "Press Q<Enter> to quit0, TOPIC, CLIENTID, QOS);
77           if ((rc = MQTTClient_subscribe(client, TOPIC, QOS)) != MQTTCLIENT_SUCCESS)
78           {
79               printf("Failed to subscribe, return code %d0, rc);
80               rc = EXIT_FAILURE;
81           }
82           else
83           {
84               int ch;
85               do
86               {
87                       ch = getchar();
88               } while (ch!='Q' && ch != 'q');
89
90               if ((rc = MQTTClient_unsubscribe(client, TOPIC)) != MQTTCLIENT_SUCCESS)
91               {
92                       printf("Failed to unsubscribe, return code %d0, rc);
93                       rc = EXIT_FAILURE;
94               }
95           }
96
97           if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
98           {
99               printf("Failed to disconnect, return code %d0, rc);
100               rc = EXIT_FAILURE;
101           }
102       destroy_exit:
103           MQTTClient_destroy(&client);
104       exit:
105           return rc;
106       }
107
108
109Paho MQTT C Client Library      Thu Jul 22 2021                    subasync(3)
Impressum