MQTTAsync_publish.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2012, 2020 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v2.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  * https://www.eclipse.org/legal/epl-2.0/
10  * and the Eclipse Distribution License is available at
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  * Ian Craggs - initial contribution
15  *******************************************************************************/
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "MQTTAsync.h"
21 
22 #if !defined(_WIN32)
23 #include <unistd.h>
24 #else
25 #include <windows.h>
26 #endif
27 
28 #if defined(_WRS_KERNEL)
29 #include <OsWrapper.h>
30 #endif
31 
32 #define ADDRESS "tcp://mqtt.eclipse.org:1883"
33 #define CLIENTID "ExampleClientPub"
34 #define TOPIC "MQTT Examples"
35 #define PAYLOAD "Hello World!"
36 #define QOS 1
37 #define TIMEOUT 10000L
38 
39 int finished = 0;
40 
41 void connlost(void *context, char *cause)
42 {
43  MQTTAsync client = (MQTTAsync)context;
45  int rc;
46 
47  printf("\nConnection lost\n");
48  printf(" cause: %s\n", cause);
49 
50  printf("Reconnecting\n");
51  conn_opts.keepAliveInterval = 20;
52  conn_opts.cleansession = 1;
53  if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
54  {
55  printf("Failed to start connect, return code %d\n", rc);
56  finished = 1;
57  }
58 }
59 
61 {
62  printf("Disconnect failed\n");
63  finished = 1;
64 }
65 
67 {
68  printf("Successful disconnection\n");
69  finished = 1;
70 }
71 
73 {
74  MQTTAsync client = (MQTTAsync)context;
76  int rc;
77 
78  printf("Message send failed token %d error code %d\n", response->token, response->code);
79  opts.onSuccess = onDisconnect;
81  opts.context = client;
82  if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
83  {
84  printf("Failed to start disconnect, return code %d\n", rc);
85  exit(EXIT_FAILURE);
86  }
87 }
88 
89 void onSend(void* context, MQTTAsync_successData* response)
90 {
91  MQTTAsync client = (MQTTAsync)context;
93  int rc;
94 
95  printf("Message with token value %d delivery confirmed\n", response->token);
96  opts.onSuccess = onDisconnect;
98  opts.context = client;
99  if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
100  {
101  printf("Failed to start disconnect, return code %d\n", rc);
102  exit(EXIT_FAILURE);
103  }
104 }
105 
106 
108 {
109  printf("Connect failed, rc %d\n", response ? response->code : 0);
110  finished = 1;
111 }
112 
113 
115 {
116  MQTTAsync client = (MQTTAsync)context;
119  int rc;
120 
121  printf("Successful connection\n");
122  opts.onSuccess = onSend;
123  opts.onFailure = onSendFailure;
124  opts.context = client;
125  pubmsg.payload = PAYLOAD;
126  pubmsg.payloadlen = (int)strlen(PAYLOAD);
127  pubmsg.qos = QOS;
128  pubmsg.retained = 0;
129  if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
130  {
131  printf("Failed to start sendMessage, return code %d\n", rc);
132  exit(EXIT_FAILURE);
133  }
134 }
135 
136 int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
137 {
138  /* not expecting any messages */
139  return 1;
140 }
141 
142 int main(int argc, char* argv[])
143 {
146  int rc;
147 
149  {
150  printf("Failed to create client object, return code %d\n", rc);
151  exit(EXIT_FAILURE);
152  }
153 
154  if ((rc = MQTTAsync_setCallbacks(client, NULL, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
155  {
156  printf("Failed to set callback, return code %d\n", rc);
157  exit(EXIT_FAILURE);
158  }
159 
160  conn_opts.keepAliveInterval = 20;
161  conn_opts.cleansession = 1;
162  conn_opts.onSuccess = onConnect;
163  conn_opts.onFailure = onConnectFailure;
164  conn_opts.context = client;
165  if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
166  {
167  printf("Failed to start connect, return code %d\n", rc);
168  exit(EXIT_FAILURE);
169  }
170 
171  printf("Waiting for publication of %s\n"
172  "on topic %s for client with ClientID: %s\n",
174  while (!finished)
175  #if defined(_WIN32)
176  Sleep(100);
177  #else
178  usleep(10000L);
179  #endif
180 
181  MQTTAsync_destroy(&client);
182  return rc;
183 }
184 
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1255
int messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *m)
void onSendFailure(void *context, MQTTAsync_failureData *response)
#define CLIENTID
void onDisconnect(void *context, MQTTAsync_successData *response)
#define MQTTAsync_responseOptions_initializer
Definition: MQTTAsync.h:746
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
Definition: MQTTAsync.c:3923
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
Definition: MQTTAsync.c:3062
struct pubsub_opts opts
Definition: paho_c_pub.c:42
void * MQTTAsync
Definition: MQTTAsync.h:239
int finished
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
Definition: MQTTAsync.c:3480
void onDisconnectFailure(void *context, MQTTAsync_failureData *response)
MQTTAsync_connectOptions conn_opts
Definition: paho_c_sub.c:191
#define TOPIC
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
Definition: MQTTAsync.c:737
#define MQTTAsync_disconnectOptions_initializer
Definition: MQTTAsync.h:1422
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:702
MQTTAsync_token token
Definition: MQTTAsync.h:549
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1393
#define MQTTAsync_connectOptions_initializer
Definition: MQTTAsync.h:1335
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:696
MQTTAsync client
Definition: test6.c:276
void MQTTAsync_destroy(MQTTAsync *handle)
Definition: MQTTAsync.c:2554
#define ADDRESS
#define MQTTASYNC_SUCCESS
Definition: MQTTAsync.h:113
int MQTTAsync_sendMessage(MQTTAsync handle, const char *destinationName, const MQTTAsync_message *message, MQTTAsync_responseOptions *response)
Definition: MQTTAsync.c:4328
dictionary context
Definition: test2.py:57
#define MQTTCLIENT_PERSISTENCE_NONE
#define PAYLOAD
void connlost(void *context, char *cause)
MQTTAsync_token token
Definition: MQTTAsync.h:514
void onSend(void *context, MQTTAsync_successData *response)
void onConnect(void *context, MQTTAsync_successData *response)
#define MQTTAsync_message_initializer
Definition: MQTTAsync.h:319
enum MQTTReasonCodes rc
Definition: test10.c:1112
int main(int argc, char *argv[])
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1249
#define QOS
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1387
void onConnectFailure(void *context, MQTTAsync_failureData *response)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09