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.

Revision:
16:28d062c5522b
Parent:
12:086a9314e8a5
Child:
17:0811bdbdd78a
--- a/main.cpp	Fri Aug 01 23:46:08 2014 +0000
+++ b/main.cpp	Mon Oct 06 11:42:25 2014 +0000
@@ -69,7 +69,7 @@
     data.clientID.cstring = "mbed-sample";
     data.username.cstring = "testuser";
     data.password.cstring = "testpassword";
-    if ((rc = client.connect(&data)) != 0)
+    if ((rc = client.connect(data)) != 0)
         lcd.printf("rc from MQTT connect is %d\n", rc);
     
     if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0)
@@ -85,7 +85,7 @@
     message.dup = false;
     message.payload = (void*)buf;
     message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, &message);
+    rc = client.publish(topic, message);
     while (arrivedcount < 1)
         client.yield(100);
         
@@ -93,7 +93,7 @@
     sprintf(buf, "Hello World!  QoS 1 message from app version %f\n", version);
     message.qos = MQTT::QOS1;
     message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, &message);
+    rc = client.publish(topic, message);
     while (arrivedcount < 2)
         client.yield(100);
         
@@ -101,7 +101,7 @@
     sprintf(buf, "Hello World!  QoS 2 message from app version %f\n", version);
     message.qos = MQTT::QOS2;
     message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, &message);
+    rc = client.publish(topic, message);
     while (arrivedcount < 3)
         client.yield(100);
         
@@ -111,7 +111,7 @@
         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);
+        rc = client.publish(topic, message);
         while (arrivedcount < i + 3)
             client.yield(100);
     }