MQTTAsync_publish.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2018 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 v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "MQTTAsync.h"
  20. #if !defined(WIN32)
  21. #include <unistd.h>
  22. #else
  23. #include <windows.h>
  24. #endif
  25. #if defined(_WRS_KERNEL)
  26. #include <OsWrapper.h>
  27. #endif
  28. #define ADDRESS "tcp://mqtt.eclipse.org:1883"
  29. #define CLIENTID "ExampleClientPub"
  30. #define TOPIC "MQTT Examples"
  31. #define PAYLOAD "Hello World!"
  32. #define QOS 1
  33. #define TIMEOUT 10000L
  34. volatile MQTTAsync_token deliveredtoken;
  35. int finished = 0;
  36. void connlost(void *context, char *cause)
  37. {
  38. MQTTAsync client = (MQTTAsync)context;
  39. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  40. int rc;
  41. printf("\nConnection lost\n");
  42. printf(" cause: %s\n", cause);
  43. printf("Reconnecting\n");
  44. conn_opts.keepAliveInterval = 20;
  45. conn_opts.cleansession = 1;
  46. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  47. {
  48. printf("Failed to start connect, return code %d\n", rc);
  49. finished = 1;
  50. }
  51. }
  52. void onDisconnect(void* context, MQTTAsync_successData* response)
  53. {
  54. printf("Successful disconnection\n");
  55. finished = 1;
  56. }
  57. void onSend(void* context, MQTTAsync_successData* response)
  58. {
  59. MQTTAsync client = (MQTTAsync)context;
  60. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  61. int rc;
  62. printf("Message with token value %d delivery confirmed\n", response->token);
  63. opts.onSuccess = onDisconnect;
  64. opts.context = client;
  65. if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
  66. {
  67. printf("Failed to start sendMessage, return code %d\n", rc);
  68. exit(EXIT_FAILURE);
  69. }
  70. }
  71. void onConnectFailure(void* context, MQTTAsync_failureData* response)
  72. {
  73. printf("Connect failed, rc %d\n", response ? response->code : 0);
  74. finished = 1;
  75. }
  76. void onConnect(void* context, MQTTAsync_successData* response)
  77. {
  78. MQTTAsync client = (MQTTAsync)context;
  79. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  80. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  81. int rc;
  82. printf("Successful connection\n");
  83. opts.onSuccess = onSend;
  84. opts.context = client;
  85. pubmsg.payload = PAYLOAD;
  86. pubmsg.payloadlen = (int)strlen(PAYLOAD);
  87. pubmsg.qos = QOS;
  88. pubmsg.retained = 0;
  89. deliveredtoken = 0;
  90. if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
  91. {
  92. printf("Failed to start sendMessage, return code %d\n", rc);
  93. exit(EXIT_FAILURE);
  94. }
  95. }
  96. int main(int argc, char* argv[])
  97. {
  98. MQTTAsync client;
  99. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  100. int rc;
  101. MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  102. MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);
  103. conn_opts.keepAliveInterval = 20;
  104. conn_opts.cleansession = 1;
  105. conn_opts.onSuccess = onConnect;
  106. conn_opts.onFailure = onConnectFailure;
  107. conn_opts.context = client;
  108. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  109. {
  110. printf("Failed to start connect, return code %d\n", rc);
  111. exit(EXIT_FAILURE);
  112. }
  113. printf("Waiting for publication of %s\n"
  114. "on topic %s for client with ClientID: %s\n",
  115. PAYLOAD, TOPIC, CLIENTID);
  116. while (!finished)
  117. #if defined(WIN32)
  118. Sleep(100);
  119. #else
  120. usleep(10000L);
  121. #endif
  122. MQTTAsync_destroy(&client);
  123. return rc;
  124. }