mqtt_client.txt 5.3 KB

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