Simple usage example of MQTTS library

Dependencies:   EthernetInterface MQTTS NTPClient SDFileSystem mbed-rtos mbed wolfSSL

Fork of HelloMQTT by MQTT

MQTT is light weight protocol for M2M, IoT. MQTTS adds TLS(SSL) security layer into the MQTT. This program shows simple usage example of MQTTS library.

Connect information has to be stored in SD file "connectInfo.txt", with following lines in it.

  • Host Name
  • User Name
  • Password
  • Client ID
  • Topic

The program asks following information on the terminal

  • Port Number to be connected. If the port is >8000 it assumes MQTTS.
  • Message to be published
  • Certificate file name, if MQTTS. Input file name on SD card. Simply Return key force it to no server verification. The program set up the realtime clock by NTP for certificate verification.

This program was tested with FRDM-K64F, against Sango MQTT server by Shiguredo. https://sango.shiguredo.jp/

References:

Revision:
8:a3e3113054a1
Parent:
7:3de634f2d40c
Child:
9:5beb8609e9f7
--- a/main.cpp	Sun May 11 19:17:05 2014 +0000
+++ b/main.cpp	Tue May 20 15:08:05 2014 +0000
@@ -24,17 +24,16 @@
  
  */
 
-#include "mbed.h"
-#include "EthernetInterfaceIPStack.h"
 
 #include "C12832.h"
 C12832 lcd(p5, p7, p6, p8, p11);
 
-#include "FP.cpp"
+#include "MQTTEthernet.h"
 #include "MQTTClient.h"
 
 int arrivedcount = 0;
 
+
 void messageArrived(MQTT::Message* message)
 {
     lcd.cls();
@@ -46,17 +45,16 @@
 }
 
 
-
 int main(int argc, char* argv[])
 {   
-    IPStack ipstack = IPStack();
-    float version = 0.43;
+    MQTTEthernet ipstack = MQTTEthernet();
+    float version = 0.46;
     char* topic = "mbed-sample";
     
     lcd.printf("Version is %f\n", version);
     printf("Version is %f\n", version);
               
-    MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
+    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
     
     char* hostname = "m2m.eclipse.org";
     int port = 1883;
@@ -67,15 +65,12 @@
  
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
     data.MQTTVersion = 3;
-    data.clientID.cstring = "mbed-icraggs";
-    rc = client.connect(&data);
-    if (rc != 0)
+    data.clientID.cstring = "mbed-sample";
+    if ((rc = client.connect(&data)) != 0)
         lcd.printf("rc from MQTT connect is %d\n", rc);
     
-    rc = client.subscribe(topic, MQTT::QOS1, messageArrived);   
-    if (rc != 0) {
-        printf("rc from MQTT subscribe is %d\n", rc);
-    }
+    if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0)
+        lcd.printf("rc from MQTT subscribe is %d\n", rc);
 
     MQTT::Message message;
 
@@ -107,21 +102,17 @@
     while (arrivedcount == 2)
         client.yield(100);
     
-    rc = client.unsubscribe(topic);
-    if (rc != 0) {
+    if ((rc = client.unsubscribe(topic)) != 0)
         printf("rc from unsubscribe was %d\n", rc);
-    }
     
-    rc = client.disconnect();
-    if (rc != 0) {
+    if ((rc = client.disconnect()) != 0)
         printf("rc from disconnect was %d\n", rc);
-    }
     
     ipstack.disconnect();
     
     lcd.cls();
     lcd.locate(0,3);
-    lcd.printf("Finish: %d msgs\n", arrivedcount);
+    lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
     printf("Finishing with %d messages received\n", arrivedcount);
     
     return 0;