MQTTClient_publish_async.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2023 IBM Corp., Ian Craggs
  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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "MQTTClient.h"
  20. #if !defined(_WIN32)
  21. #include <unistd.h>
  22. #else
  23. #include <windows.h>
  24. #endif
  25. #define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
  26. #define CLIENTID "ExampleClientPub"
  27. #define TOPIC "MQTT Examples"
  28. #define PAYLOAD "Hello World!"
  29. #define QOS 1
  30. #define TIMEOUT 10000L
  31. MQTTClient_deliveryToken deliveredtoken;
  32. void delivered(void *context, MQTTClient_deliveryToken dt)
  33. {
  34. printf("Message with token value %d delivery confirmed\n", dt);
  35. deliveredtoken = dt;
  36. }
  37. int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
  38. {
  39. printf("Message arrived\n");
  40. printf(" topic: %s\n", topicName);
  41. printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
  42. MQTTClient_freeMessage(&message);
  43. MQTTClient_free(topicName);
  44. return 1;
  45. }
  46. void connlost(void *context, char *cause)
  47. {
  48. printf("\nConnection lost\n");
  49. if (cause)
  50. printf(" cause: %s\n", cause);
  51. }
  52. int main(int argc, char* argv[])
  53. {
  54. MQTTClient client;
  55. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  56. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  57. MQTTClient_deliveryToken token;
  58. int rc;
  59. const char* uri = (argc > 1) ? argv[1] : ADDRESS;
  60. printf("Using server at %s\n", uri);
  61. if ((rc = MQTTClient_create(&client, uri, CLIENTID,
  62. MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
  63. {
  64. printf("Failed to create client, return code %d\n", rc);
  65. rc = EXIT_FAILURE;
  66. goto exit;
  67. }
  68. if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
  69. {
  70. printf("Failed to set callbacks, return code %d\n", rc);
  71. rc = EXIT_FAILURE;
  72. goto destroy_exit;
  73. }
  74. conn_opts.keepAliveInterval = 20;
  75. conn_opts.cleansession = 1;
  76. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  77. {
  78. printf("Failed to connect, return code %d\n", rc);
  79. rc = EXIT_FAILURE;
  80. goto destroy_exit;
  81. }
  82. pubmsg.payload = PAYLOAD;
  83. pubmsg.payloadlen = (int)strlen(PAYLOAD);
  84. pubmsg.qos = QOS;
  85. pubmsg.retained = 0;
  86. deliveredtoken = 0;
  87. if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
  88. {
  89. printf("Failed to publish message, return code %d\n", rc);
  90. rc = EXIT_FAILURE;
  91. }
  92. else
  93. {
  94. printf("Waiting for publication of %s\n"
  95. "on topic %s for client with ClientID: %s\n",
  96. PAYLOAD, TOPIC, CLIENTID);
  97. while (deliveredtoken != token)
  98. {
  99. #if defined(_WIN32)
  100. Sleep(100);
  101. #else
  102. usleep(10000L);
  103. #endif
  104. }
  105. }
  106. if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
  107. {
  108. printf("Failed to disconnect, return code %d\n", rc);
  109. rc = EXIT_FAILURE;
  110. }
  111. destroy_exit:
  112. MQTTClient_destroy(&client);
  113. exit:
  114. return rc;
  115. }