This example program uses HiveMQ Broker (http://www.mqtt-dashboard.com/index.html) to both publish andsubscribe to topics.

Dependencies:   WNCInterface mbed-rtos mbed

See the README for details on this example program. NOTE: When started, the program can take up to 40 seconds before it will respond. This delay is the time required for the WNC Data Module to connect with the network.

Revision:
2:cedbc9de0521
Parent:
1:368874dc3385
Child:
3:9413314f8017
--- a/main.cpp	Tue Sep 27 20:22:30 2016 +0000
+++ b/main.cpp	Fri Oct 07 01:17:37 2016 +0000
@@ -88,7 +88,7 @@
 // MQTT message arrived callback function
 void messageArrived(MQTT::MessageData& md) {
     MQTT::Message &message = md.message;
-    printf("Receiving MQTT message:  %.*s\r\n", message.payloadlen, (char*)message.payload);
+    pc.printf("Receiving MQTT message:  %.*s\r\n", message.payloadlen, (char*)message.payload);
     
     if (message.payloadlen == 3) {
         if (strncmp((char*)message.payload, "red", 3) == 0)
@@ -130,11 +130,11 @@
     char* hostname = BROKER;
     int port = PORT;
 
-    printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n");
-    printf("\r\nConnected to local network...\r\n");    
-    printf("IP address is %s\r\n", eth.getIPAddress());
-    printf("MAC address is %s\r\n", eth.getMACAddress());
-    printf("Gateway address is %s\r\n", eth.getGateway());
+    pc.printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n");
+    pc.printf("\r\nConnected to local network...\r\n");    
+    pc.printf("IP address is %s\r\n", eth.getIPAddress());
+    pc.printf("MAC address is %s\r\n", eth.getMACAddress());
+    pc.printf("Gateway address is %s\r\n", eth.getGateway());
 
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
     int tries;
@@ -145,16 +145,16 @@
         tmr.start();
         rc = 1;
         while( rc && tries < 3) {
-            printf("\r\n\r\nAttempting TCP connect to %s:%d:  ", hostname, port);
+            pc.printf("\r\n\r\nAttempting TCP connect to %s:%d:  ", hostname, port);
             rc = ipstack.connect(hostname, port);
             if( rc ) {
-                printf("Failed!!\r\n");
+                pc.printf("Failed!!\r\n");
                 while( tmr.read_ms() < 5000 ) ;
                 tries++;
                 tmr.reset();
             }
             else {
-                printf("Success!\r\n");
+                pc.printf("Success!\r\n");
                 rc = 0;
             }
         }
@@ -168,33 +168,33 @@
         rc = 1;
         tmr.reset(); 
         while( !client.isConnected() && rc && tries < 3) {
-            printf("Attempting (%d) MQTT connect to %s:%d: ", tries, hostname, port);
+            pc.printf("Attempting (%d) MQTT connect to %s:%d: ", tries, hostname, port);
             rc = client.connect(data);
             if( rc ) {
-                printf("Connection Failed!\r\n");
+                pc.printf("Connection Failed!\r\n");
                 while( tmr.read_ms() < 5000 );
                 tmr.reset();
                 tries++;
             }
             else
-                printf("Connected!\r\n");
+                pc.printf("Connected!\r\n");
         }
 
         // subscribe to MQTT topic
         tmr.reset();
         rc = 1;
         while( rc && client.isConnected() && tries < 3) {
-            printf("We are %s, Subscribing to MQTT topic %s (%d): ", client.isConnected()?"connected":"NOT CONNECTED",topic,tries);
+            pc.printf("We are %s, Subscribing to MQTT topic %s (%d): ", client.isConnected()?"connected":"NOT CONNECTED",topic,tries);
             rc = client.subscribe(topic, MQTT::QOS0, messageArrived);
             if( rc ) {
-                printf("Subscribe request failed!\r\n");
+                pc.printf("Subscribe request failed!\r\n");
                 while( tmr.read_ms() < 5000 );
                 tries++;
                 tmr.reset();
             }
             else {
                 good=1;
-                printf("Subscribe successful!\r\n");
+                pc.printf("Subscribe successful!\r\n");
             }
         }
     }        
@@ -219,14 +219,14 @@
                     sprintf(buf, "sw3");
                     break;
             }
-            printf("Publishing MQTT message: %s (%d)\r\n", (char*)message.payload,
+            pc.printf("Publishing MQTT message: %s (%d)\r\n", (char*)message.payload,
                        message.payloadlen);
             rc = client.publish(topic, message);
             if( rc ) {
-                printf("Publish request failed! (%d)\r\n",rc);
+                pc.printf("Publish request failed! (%d)\r\n",rc);
             }
             else {
-                printf("Publish successful!\r\n");            
+                pc.printf("Publish successful!\r\n");            
             client.yield(100);
             }
         }