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

NAME

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