MQTTClient_publish_async.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2017 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 "MQTTClient.h"
  20. #define ADDRESS "tcp://localhost:1883"
  21. #define CLIENTID "ExampleClientPub"
  22. #define TOPIC "MQTT Examples"
  23. #define PAYLOAD "Hello World!"
  24. #define QOS 1
  25. #define TIMEOUT 10000L
  26. volatile MQTTClient_deliveryToken deliveredtoken;
  27. void delivered(void *context, MQTTClient_deliveryToken dt)
  28. {
  29. printf("Message with token value %d delivery confirmed\n", dt);
  30. deliveredtoken = dt;
  31. }
  32. int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
  33. {
  34. int i;
  35. char* payloadptr;
  36. printf("Message arrived\n");
  37. printf(" topic: %s\n", topicName);
  38. printf(" message: ");
  39. payloadptr = message->payload;
  40. for(i=0; i<message->payloadlen; i++)
  41. {
  42. putchar(*payloadptr++);
  43. }
  44. putchar('\n');
  45. MQTTClient_freeMessage(&message);
  46. MQTTClient_free(topicName);
  47. return 1;
  48. }
  49. void connlost(void *context, char *cause)
  50. {
  51. printf("\nConnection lost\n");
  52. printf(" cause: %s\n", cause);
  53. }
  54. int main(int argc, char* argv[])
  55. {
  56. MQTTClient client;
  57. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  58. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  59. MQTTClient_deliveryToken token;
  60. int rc;
  61. MQTTClient_create(&client, ADDRESS, CLIENTID,
  62. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  63. conn_opts.keepAliveInterval = 20;
  64. conn_opts.cleansession = 1;
  65. MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
  66. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  67. {
  68. printf("Failed to connect, return code %d\n", rc);
  69. exit(EXIT_FAILURE);
  70. }
  71. pubmsg.payload = PAYLOAD;
  72. pubmsg.payloadlen = (int)strlen(PAYLOAD);
  73. pubmsg.qos = QOS;
  74. pubmsg.retained = 0;
  75. deliveredtoken = 0;
  76. MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
  77. printf("Waiting for publication of %s\n"
  78. "on topic %s for client with ClientID: %s\n",
  79. PAYLOAD, TOPIC, CLIENTID);
  80. while(deliveredtoken != token);
  81. MQTTClient_disconnect(client, 10000);
  82. MQTTClient_destroy(&client);
  83. return rc;
  84. }