Sample MQTT program - simple send and receive

Dependencies:   C12832 MQTT

Fork of HelloMQTT by MQTT

Revision:
23:85b0a1df7d75
Parent:
21:a68bd76740f9
diff -r f5f6c9059eed -r 85b0a1df7d75 main.cpp
--- a/main.cpp	Mon Sep 11 16:43:49 2017 +0200
+++ b/main.cpp	Tue Oct 03 22:56:42 2017 +0000
@@ -43,6 +43,7 @@
 #endif
 
 #define MQTTCLIENT_QOS2 1
+#define MQTT_DEBUG 1
 
 #include "easy-connect.h"
 #include "MQTTNetwork.h"
@@ -77,7 +78,7 @@
 
     MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
 
-    const char* hostname = "m2m.eclipse.org";
+    const char* hostname = "iot.eclipse.org";
     int port = 1883;
     logMessage("Connecting to %s:%d\r\n", hostname, port);
     int rc = mqttNetwork.connect(hostname, port);
@@ -85,8 +86,10 @@
         logMessage("rc from TCP connect is %d\r\n", rc);
 
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    data.MQTTVersion = 3;
-    data.clientID.cstring = "mbed-sample";
+    char clientid[30];
+    sprintf(clientid, "mbed-sample-%d", rand());
+    logMessage("Client id is %s\r\n", clientid);
+    data.clientID.cstring = clientid;
     data.username.cstring = "testuser";
     data.password.cstring = "testpassword";
     if ((rc = client.connect(data)) != 0)