basic-1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This example shows how to publish messages from outside of the Mosquitto network loop.
  3. */
  4. #include <mosquitto.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. /* Callback called when the client receives a CONNACK message from the broker. */
  10. void on_connect(struct mosquitto *mosq, void *obj, int reason_code)
  11. {
  12. /* Print out the connection result. mosquitto_connack_string() produces an
  13. * appropriate string for MQTT v3.x clients, the equivalent for MQTT v5.0
  14. * clients is mosquitto_reason_string().
  15. */
  16. printf("on_connect: %s\n", mosquitto_connack_string(reason_code));
  17. if(reason_code != 0){
  18. /* If the connection fails for any reason, we don't want to keep on
  19. * retrying in this example, so disconnect. Without this, the client
  20. * will attempt to reconnect. */
  21. mosquitto_disconnect(mosq);
  22. }
  23. /* You may wish to set a flag here to indicate to your application that the
  24. * client is now connected. */
  25. }
  26. /* Callback called when the client knows to the best of its abilities that a
  27. * PUBLISH has been successfully sent. For QoS 0 this means the message has
  28. * been completely written to the operating system. For QoS 1 this means we
  29. * have received a PUBACK from the broker. For QoS 2 this means we have
  30. * received a PUBCOMP from the broker. */
  31. void on_publish(struct mosquitto *mosq, void *obj, int mid)
  32. {
  33. printf("Message with mid %d has been published.\n", mid);
  34. }
  35. int get_temperature(void)
  36. {
  37. sleep(1); /* Prevent a storm of messages - this pretend sensor works at 1Hz */
  38. return random()%100;
  39. }
  40. /* This function pretends to read some data from a sensor and publish it.*/
  41. void publish_sensor_data(struct mosquitto *mosq)
  42. {
  43. char payload[20];
  44. int temp;
  45. int rc;
  46. /* Get our pretend data */
  47. temp = get_temperature();
  48. /* Print it to a string for easy human reading - payload format is highly
  49. * application dependent. */
  50. snprintf(payload, sizeof(payload), "%d", temp);
  51. /* Publish the message
  52. * mosq - our client instance
  53. * *mid = NULL - we don't want to know what the message id for this message is
  54. * topic = "example/temperature" - the topic on which this message will be published
  55. * payloadlen = strlen(payload) - the length of our payload in bytes
  56. * payload - the actual payload
  57. * qos = 2 - publish with QoS 2 for this example
  58. * retain = false - do not use the retained message feature for this message
  59. */
  60. rc = mosquitto_publish(mosq, NULL, "example/temperature", strlen(payload), payload, 2, false);
  61. if(rc != MOSQ_ERR_SUCCESS){
  62. fprintf(stderr, "Error publishing: %s\n", mosquitto_strerror(rc));
  63. }
  64. }
  65. int main(int argc, char *argv[])
  66. {
  67. struct mosquitto *mosq;
  68. int rc;
  69. /* Required before calling other mosquitto functions */
  70. mosquitto_lib_init();
  71. /* Create a new client instance.
  72. * id = NULL -> ask the broker to generate a client id for us
  73. * clean session = true -> the broker should remove old sessions when we connect
  74. * obj = NULL -> we aren't passing any of our private data for callbacks
  75. */
  76. mosq = mosquitto_new(NULL, true, NULL);
  77. if(mosq == NULL){
  78. fprintf(stderr, "Error: Out of memory.\n");
  79. return 1;
  80. }
  81. /* Configure callbacks. This should be done before connecting ideally. */
  82. mosquitto_connect_callback_set(mosq, on_connect);
  83. mosquitto_publish_callback_set(mosq, on_publish);
  84. /* Connect to test.mosquitto.org on port 1883, with a keepalive of 60 seconds.
  85. * This call makes the socket connection only, it does not complete the MQTT
  86. * CONNECT/CONNACK flow, you should use mosquitto_loop_start() or
  87. * mosquitto_loop_forever() for processing net traffic. */
  88. rc = mosquitto_connect(mosq, "test.mosquitto.org", 1883, 60);
  89. if(rc != MOSQ_ERR_SUCCESS){
  90. mosquitto_destroy(mosq);
  91. fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
  92. return 1;
  93. }
  94. /* Run the network loop in a background thread, this call returns quickly. */
  95. rc = mosquitto_loop_start(mosq);
  96. if(rc != MOSQ_ERR_SUCCESS){
  97. mosquitto_destroy(mosq);
  98. fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
  99. return 1;
  100. }
  101. /* At this point the client is connected to the network socket, but may not
  102. * have completed CONNECT/CONNACK.
  103. * It is fairly safe to start queuing messages at this point, but if you
  104. * want to be really sure you should wait until after a successful call to
  105. * the connect callback.
  106. * In this case we know it is 1 second before we start publishing.
  107. */
  108. while(1){
  109. publish_sensor_data(mosq);
  110. }
  111. mosquitto_lib_cleanup();
  112. return 0;
  113. }