MQTTClient_subscribe.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "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. 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. int rc;
  59. int ch;
  60. MQTTClient_create(&client, ADDRESS, CLIENTID,
  61. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  62. conn_opts.keepAliveInterval = 20;
  63. conn_opts.cleansession = 1;
  64. MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
  65. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  66. {
  67. printf("Failed to connect, return code %d\n", rc);
  68. exit(EXIT_FAILURE);
  69. }
  70. printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
  71. "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
  72. MQTTClient_subscribe(client, TOPIC, QOS);
  73. do
  74. {
  75. ch = getchar();
  76. } while(ch!='Q' && ch != 'q');
  77. MQTTClient_unsubscribe(client, TOPIC);
  78. MQTTClient_disconnect(client, 10000);
  79. MQTTClient_destroy(&client);
  80. return rc;
  81. }