basic-1.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * This example shows how to write a client that subscribes to a topic and does
  3. * not do anything other than handle the messages that are received.
  4. */
  5. #include <mosquitto.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. /* Callback called when the client receives a CONNACK message from the broker. */
  11. void on_connect(struct mosquitto *mosq, void *obj, int reason_code)
  12. {
  13. int rc;
  14. /* Print out the connection result. mosquitto_connack_string() produces an
  15. * appropriate string for MQTT v3.x clients, the equivalent for MQTT v5.0
  16. * clients is mosquitto_reason_string().
  17. */
  18. printf("on_connect: %s\n", mosquitto_connack_string(reason_code));
  19. if(reason_code != 0){
  20. /* If the connection fails for any reason, we don't want to keep on
  21. * retrying in this example, so disconnect. Without this, the client
  22. * will attempt to reconnect. */
  23. mosquitto_disconnect(mosq);
  24. }
  25. /* Making subscriptions in the on_connect() callback means that if the
  26. * connection drops and is automatically resumed by the client, then the
  27. * subscriptions will be recreated when the client reconnects. */
  28. rc = mosquitto_subscribe(mosq, NULL, "example/temperature", 1);
  29. if(rc != MOSQ_ERR_SUCCESS){
  30. fprintf(stderr, "Error subscribing: %s\n", mosquitto_strerror(rc));
  31. /* We might as well disconnect if we were unable to subscribe */
  32. mosquitto_disconnect(mosq);
  33. }
  34. }
  35. /* Callback called when the broker sends a SUBACK in response to a SUBSCRIBE. */
  36. void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
  37. {
  38. int i;
  39. bool have_subscription = false;
  40. /* In this example we only subscribe to a single topic at once, but a
  41. * SUBSCRIBE can contain many topics at once, so this is one way to check
  42. * them all. */
  43. for(i=0; i<qos_count; i++){
  44. printf("on_subscribe: %d:granted qos = %d\n", i, granted_qos[i]);
  45. if(granted_qos[i] <= 2){
  46. have_subscription = true;
  47. }
  48. }
  49. if(have_subscription == false){
  50. /* The broker rejected all of our subscriptions, we know we only sent
  51. * the one SUBSCRIBE, so there is no point remaining connected. */
  52. fprintf(stderr, "Error: All subscriptions rejected.\n");
  53. mosquitto_disconnect(mosq);
  54. }
  55. }
  56. /* Callback called when the client receives a message. */
  57. void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  58. {
  59. /* This blindly prints the payload, but the payload can be anything so take care. */
  60. printf("%s %d %s\n", msg->topic, msg->qos, (char *)msg->payload);
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64. struct mosquitto *mosq;
  65. int rc;
  66. /* Required before calling other mosquitto functions */
  67. mosquitto_lib_init();
  68. /* Create a new client instance.
  69. * id = NULL -> ask the broker to generate a client id for us
  70. * clean session = true -> the broker should remove old sessions when we connect
  71. * obj = NULL -> we aren't passing any of our private data for callbacks
  72. */
  73. mosq = mosquitto_new(NULL, true, NULL);
  74. if(mosq == NULL){
  75. fprintf(stderr, "Error: Out of memory.\n");
  76. return 1;
  77. }
  78. /* Configure callbacks. This should be done before connecting ideally. */
  79. mosquitto_connect_callback_set(mosq, on_connect);
  80. mosquitto_subscribe_callback_set(mosq, on_subscribe);
  81. mosquitto_message_callback_set(mosq, on_message);
  82. /* Connect to test.mosquitto.org on port 1883, with a keepalive of 60 seconds.
  83. * This call makes the socket connection only, it does not complete the MQTT
  84. * CONNECT/CONNACK flow, you should use mosquitto_loop_start() or
  85. * mosquitto_loop_forever() for processing net traffic. */
  86. rc = mosquitto_connect(mosq, "test.mosquitto.org", 1883, 60);
  87. if(rc != MOSQ_ERR_SUCCESS){
  88. mosquitto_destroy(mosq);
  89. fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
  90. return 1;
  91. }
  92. /* Run the network loop in a blocking call. The only thing we do in this
  93. * example is to print incoming messages, so a blocking call here is fine.
  94. *
  95. * This call will continue forever, carrying automatic reconnections if
  96. * necessary, until the user calls mosquitto_disconnect().
  97. */
  98. mosquitto_loop_forever(mosq, -1, 1);
  99. mosquitto_lib_cleanup();
  100. return 0;
  101. }