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