Sample MQTT program - simple send and receive

Dependencies:   C12832 MQTT

Dependents:   MQTT_G_SENSOR

This program and the MQTT libraries it uses are part of the EclipseTM Paho project; specifically the embedded client.

This example and API are working, but are still in progress. Please give us your feedback.

HelloMQTT is an example of using the MQTT API. The MQTT API is portable across network interface stacks. MQTT is designed to be used with TCP/IP, but any transport with similar characteristics should be suitable.

HelloMQTT uses the NetworkInterface APIs in mbed OS 5 to show how this works. The MQTT library contains an MQTTNetwork.h header, which is a wrapper around the mbed networking interface. To switch between connectivity methods (the default is Ethernet) the easy-connect library is provided in this example application. You can change the connectivity method in mbed_app.json.

Adding new connectivity methods to the program is trivial, as long as they implement the mbed OS 5 NetworkStack API.

Files at this revision

API Documentation at this revision

Comitter:
icraggs
Date:
Fri Aug 01 17:02:22 2014 +0000
Parent:
9:5beb8609e9f7
Child:
13:52a9dd92ce9d
Commit message:
Some cleaning up

Changed in this revision

MQTT.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/MQTT.lib	Thu May 22 23:58:34 2014 +0000
+++ b/MQTT.lib	Fri Aug 01 17:02:22 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/mqtt/code/MQTT/#a51dd239b78e
+http://mbed.org/teams/mqtt/code/MQTT/#e3d64f9b986c
--- a/main.cpp	Thu May 22 23:58:34 2014 +0000
+++ b/main.cpp	Fri Aug 01 17:02:22 2014 +0000
@@ -67,6 +67,8 @@
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
     data.MQTTVersion = 3;
     data.clientID.cstring = "mbed-sample";
+    data.username.cstring = "testuser";
+    data.password.cstring = "testpassword";
     if ((rc = client.connect(&data)) != 0)
         lcd.printf("rc from MQTT connect is %d\n", rc);
     
@@ -84,7 +86,7 @@
     message.payload = (void*)buf;
     message.payloadlen = strlen(buf)+1;
     rc = client.publish(topic, &message);
-    while (arrivedcount == 0)
+    while (arrivedcount < 1)
         client.yield(100);
         
     // QoS 1
@@ -92,7 +94,7 @@
     message.qos = MQTT::QOS1;
     message.payloadlen = strlen(buf)+1;
     rc = client.publish(topic, &message);
-    while (arrivedcount == 1)
+    while (arrivedcount < 2)
         client.yield(100);
         
     // QoS 2
@@ -100,8 +102,19 @@
     message.qos = MQTT::QOS2;
     message.payloadlen = strlen(buf)+1;
     rc = client.publish(topic, &message);
-    while (arrivedcount == 2)
+    while (arrivedcount < 3)
         client.yield(100);
+        
+    // n * QoS 2
+    for (int i = 1; i <= 10; ++i)
+    {
+        sprintf(buf, "Hello World!  QoS 2 message number %d from app version %f\n", i, version);
+        message.qos = MQTT::QOS2;
+        message.payloadlen = strlen(buf)+1;
+        rc = client.publish(topic, &message);
+        while (arrivedcount < i + 3)
+            client.yield(100);
+    }
     
     if ((rc = client.unsubscribe(topic)) != 0)
         printf("rc from unsubscribe was %d\n", rc);