HelloMQTT over TLS.

Dependencies:   MQTT

Fork of HelloMQTT by Osamu Koizumi

HelloMQTT over TLS

This program is a fork of HelloMQTT. Added TLS capability by using TLSSocket library. Tested on K64F.

This sample program connects to iot.eclipse.org:8883 by default. Verifies server identification. Subscribes a certain topic. Then publishes three messages with different QoSs, i.e. QoS0, QoS1, and QoS2.

Warning

Some brokers do not accept QoS2 and/or QoS1 message. For example, AWS IoT Message Broker doesn't accept QoS2. In such broker, this program doesn't work as is. Change QoS level.

Output from console

HelloMQTT: version is 0.70

Opening network interface...
Network interface opened successfully.

Connecting to host iot.eclipse.org:8883 ...
Connection established.

MQTT client is trying to connect the server ...
Client connected.

Client is trying to subscribe a topic "mbed-test".
Client has subscribed a topic "mbed-test".

Client publishes messages ...
Publishing message QoS 0.
QoS 0 message published.
! Message arrived: qos 0, retained 0, dup 0, packetid 6257
! Payload Hello World!  QoS 0 message from app version 0.700000

Publishing message QoS 1.
QoS 1 message published.
! Message arrived: qos 1, retained 0, dup 0, packetid 1
! Payload Hello World!  QoS 1 message from app version 0.700000

Publishing message QoS 2.
QoS 2 message published.
! Message arrived: qos 2, retained 0, dup 0, packetid 2
! Payload Hello World!  QoS 2 message from app version 0.700000

Version 0.70: finish 3 msgs

Known Issues

On K64F, when serial baud rate is changed from 9600 to 115200, program fails.

Revision:
12:086a9314e8a5
Parent:
9:5beb8609e9f7
Child:
16:28d062c5522b
--- 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);