MQTTClient_subscribe.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
  21. #define CLIENTID "ExampleClientSub"
  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. printf("Message arrived\n");
  35. printf(" topic: %s\n", topicName);
  36. printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
  37. MQTTClient_freeMessage(&message);
  38. MQTTClient_free(topicName);
  39. return 1;
  40. }
  41. void connlost(void *context, char *cause)
  42. {
  43. printf("\nConnection lost\n");
  44. if (cause)
  45. printf(" cause: %s\n", cause);
  46. }
  47. int main(int argc, char* argv[])
  48. {
  49. MQTTClient client;
  50. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  51. int rc;
  52. const char* uri = (argc > 1) ? argv[1] : ADDRESS;
  53. printf("Using server at %s\n", uri);
  54. if ((rc = MQTTClient_create(&client, uri, CLIENTID,
  55. MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
  56. {
  57. printf("Failed to create client, return code %d\n", rc);
  58. rc = EXIT_FAILURE;
  59. goto exit;
  60. }
  61. if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
  62. {
  63. printf("Failed to set callbacks, return code %d\n", rc);
  64. rc = EXIT_FAILURE;
  65. goto destroy_exit;
  66. }
  67. conn_opts.keepAliveInterval = 20;
  68. conn_opts.cleansession = 1;
  69. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  70. {
  71. printf("Failed to connect, return code %d\n", rc);
  72. rc = EXIT_FAILURE;
  73. goto destroy_exit;
  74. }
  75. printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
  76. "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
  77. if ((rc = MQTTClient_subscribe(client, TOPIC, QOS)) != MQTTCLIENT_SUCCESS)
  78. {
  79. printf("Failed to subscribe, return code %d\n", rc);
  80. rc = EXIT_FAILURE;
  81. }
  82. else
  83. {
  84. int ch;
  85. do
  86. {
  87. ch = getchar();
  88. } while (ch!='Q' && ch != 'q');
  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. if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
  96. {
  97. printf("Failed to disconnect, return code %d\n", rc);
  98. rc = EXIT_FAILURE;
  99. }
  100. destroy_exit:
  101. MQTTClient_destroy(&client);
  102. exit:
  103. return rc;
  104. }