mqtt_client.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. MQTT client for lwIP
  2. Author: Erik Andersson
  3. Details of the MQTT protocol can be found at:
  4. http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
  5. -----------------------------------------------------------------
  6. 1. Initial steps, reserve memory and make connection to server:
  7. 1.1: Provide storage
  8. Static allocation:
  9. mqtt_client_t static_client;
  10. example_do_connect(&static_client);
  11. Dynamic allocation:
  12. mqtt_client_t *client = mqtt_client_new();
  13. if(client != NULL) {
  14. example_do_connect(&client);
  15. }
  16. 1.2: Establish Connection with server
  17. void example_do_connect(mqtt_client_t *client)
  18. {
  19. struct mqtt_connect_client_info_t ci;
  20. err_t err;
  21. /* Setup an empty client info structure */
  22. memset(&ci, 0, sizeof(ci));
  23. /* Minimal amount of information required is client identifier, so set it here */
  24. ci.client_id = "lwip_test";
  25. /* Initiate client and connect to server, if this fails immediately an error code is returned
  26. otherwise mqtt_connection_cb will be called with connection result after attempting
  27. to establish a connection with the server.
  28. For now MQTT version 3.1.1 is always used */
  29. err = mqtt_client_connect(client, ip_addr, MQTT_PORT, mqtt_connection_cb, 0, &ci);
  30. /* For now just print the result code if something goes wrong
  31. if(err != ERR_OK) {
  32. printf("mqtt_connect return %d\n", err);
  33. }
  34. }
  35. Connection to server can also be probed by calling mqtt_client_is_connected(client)
  36. -----------------------------------------------------------------
  37. 2. Implementing the connection status callback
  38. static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
  39. {
  40. err_t err;
  41. if(status == MQTT_CONNECT_ACCEPTED) {
  42. printf("mqtt_connection_cb: Successfully connected\n");
  43. /* Setup callback for incoming publish requests */
  44. mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg);
  45. /* Subscribe to a topic named "subtopic" with QoS level 1, call mqtt_sub_request_cb with result */
  46. err = mqtt_subscribe(client, "subtopic", 1, mqtt_sub_request_cb, arg);
  47. if(err != ERR_OK) {
  48. printf("mqtt_subscribe return: %d\n", err);
  49. }
  50. } else {
  51. printf("mqtt_connection_cb: Disconnected, reason: %d\n", status);
  52. /* Its more nice to be connected, so try to reconnect */
  53. example_do_connect(client);
  54. }
  55. }
  56. static void mqtt_sub_request_cb(void *arg, err_t result)
  57. {
  58. /* Just print the result code here for simplicity,
  59. normal behaviour would be to take some action if subscribe fails like
  60. notifying user, retry subscribe or disconnect from server */
  61. printf("Subscribe result: %d\n", result);
  62. }
  63. -----------------------------------------------------------------
  64. 3. Implementing callbacks for incoming publish and data
  65. /* The idea is to demultiplex topic and create some reference to be used in data callbacks
  66. Example here uses a global variable, better would be to use a member in arg
  67. If RAM and CPU budget allows it, the easiest implementation might be to just take a copy of
  68. the topic string and use it in mqtt_incoming_data_cb
  69. */
  70. static int inpub_id;
  71. static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len)
  72. {
  73. printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len);
  74. /* Decode topic string into a user defined reference */
  75. if(strcmp(topic, "print_payload") == 0) {
  76. inpub_id = 0;
  77. } else if(topic[0] == 'A') {
  78. /* All topics starting with 'A' might be handled at the same way */
  79. inpub_id = 1;
  80. } else {
  81. /* For all other topics */
  82. inpub_id = 2;
  83. }
  84. }
  85. static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
  86. {
  87. printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags);
  88. if(flags & MQTT_DATA_FLAG_LAST) {
  89. /* Last fragment of payload received (or whole part if payload fits receive buffer
  90. See MQTT_VAR_HEADER_BUFFER_LEN) */
  91. /* Call function or do action depending on reference, in this case inpub_id */
  92. if(inpub_id == 0) {
  93. /* Don't trust the publisher, check zero termination */
  94. if(data[len-1] == 0) {
  95. printf("mqtt_incoming_data_cb: %s\n", (const char *)data);
  96. }
  97. } else if(inpub_id == 1) {
  98. /* Call an 'A' function... */
  99. } else {
  100. printf("mqtt_incoming_data_cb: Ignoring payload...\n");
  101. }
  102. } else {
  103. /* Handle fragmented payload, store in buffer, write to file or whatever */
  104. }
  105. }
  106. -----------------------------------------------------------------
  107. 4. Using outgoing publish
  108. void example_publish(mqtt_client_t *client, void *arg)
  109. {
  110. const char *pub_payload= "PubSubHubLubJub";
  111. err_t err;
  112. u8_t qos = 2; /* 0 1 or 2, see MQTT specification */
  113. u8_t retain = 0; /* No don't retain such crappy payload... */
  114. err = mqtt_publish(client, "pub_topic", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
  115. if(err != ERR_OK) {
  116. printf("Publish err: %d\n", err);
  117. }
  118. }
  119. /* Called when publish is complete either with sucess or failure */
  120. static void mqtt_pub_request_cb(void *arg, err_t result)
  121. {
  122. if(result != ERR_OK) {
  123. printf("Publish result: %d\n", result);
  124. }
  125. }
  126. -----------------------------------------------------------------
  127. 5. Disconnecting
  128. Simply call mqtt_disconnect(client)