Sample code that showcases how to use IBMIoTF client library to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service.

Dependencies:   C12832 IBMIoTF LM75B MMA7660

Revision:
4:a416fef97faa
Parent:
3:e7b2f56c4f3f
Child:
5:48b6090e0df1
--- a/src/Main.cpp	Tue Jan 12 08:13:38 2016 +0000
+++ b/src/Main.cpp	Tue May 30 06:59:13 2017 +0000
@@ -12,21 +12,28 @@
  *
  * Contributors:
  *    Sathisumar Palaniappan - initial implementation
+ *    Lokesh K Haralakatta - Port to support mbed os 5
  *******************************************************************************/
 
+ // change this to 1 to output messages to LCD
+ #define USE_LCD 0
+
+#if USE_LCD
+ #include "C12832.h"
+ #include "Arial12x12.h"
+ #define logMessage lcd.cls();lcd.locate(0,0);lcd.printf
+ #define lcdFont lcd.set_font((unsigned char*) Arial12x12);
+#else
+ #define logMessage printf
+ #define lcdFont
+#endif
+
 #include "stdio.h"
 #include "mbed.h"
-#include "rtos.h"
-#include "EthernetInterface.h"
-#include "C12832.h"
 #include "LM75B.h"
 #include "MMA7660.h"
-#include "Arial12x12.h"
 #include "DeviceClient.h"
 #include "Command.h"
-#include "MQTTEthernet.h"
-
-#include "MQTTClient.h"
 
 #if defined(TARGET_UBLOX_C027)
 #warning "Compiling for mbed C027"
@@ -49,78 +56,65 @@
 int blink_interval = 0;
 int main()
 {
-    lcd.cls();
-    lcd.locate(0,0);
-    
-    lcd.set_font((unsigned char*) Arial12x12);  // Set a nice font for the LCD screen
-    led2 = LED2_OFF; // K64F: turn off the main board LED 
-    
+    //Set LCD font if USE_LCD == 1
+    lcdFont
+
+    // K64F: turn off the main board LED
+    led2 = LED2_OFF;
+
     //Start thread to read data from joystick
     Thread jThd(joystickThread);
     joystickPos = "CENTRE";
 
+
     // Set IoT Foundation connection parameters
-    char organization[11] = "quickstart";     // For a registered connection, replace with your org
-    char deviceType[9] = "Sample";        // For a registered connection, replace with your device type
-    char deviceId[15] = "";                  // For a registered connection, replace with your device id
-    char method[6] = "token";               // Not required to change as IBM IoTF expects only "token" for now
-    char token[9] = "password";             // For a registered connection, replace with your auth-token
-    
+    char *organization = "quickstart";     // For a registered connection, replace with your org
+    char *deviceType = "qsType";        // For a registered connection, replace with your device type
+    char *deviceId = "qsDevice";                  // For a registered connection, replace with your device id
+    char *method = "token";               // Not required to change as IBM IoTF expects only "token" for now
+    char *token = "password";             // For a registered connection, replace with your auth-token
+
     // Create DeviceClient
-    IoTF::DeviceClient client(organization, deviceType, deviceId, method, token);
+    IoTF::DeviceClient *client = new IoTF::DeviceClient(organization, deviceType, deviceId);
     
-    // Get the DeviceID(MAC Address) if we are in quickstart mode and device id is not specified
-    if((strcmp(organization, QUICKSTART) == 0) && (strcmp("", deviceId) == 0)) {
-        char tmpBuf[50];
-        client.getDeviceId(tmpBuf, sizeof(tmpBuf));
-        lcd.printf("DeviceID: %s\n", tmpBuf);
-        wait(10.0);
-    }
-    lcd.locate(0,10);
-    lcd.printf("Initialized & connecting..\n");
-    
-    bool status = false;
-    
-    // keep on retrying till the connection is successful
-    while(status == false) {
-        status = client.connect();
-        lcd.cls();
-        lcd.locate(0,0);
-        lcd.printf("connect status: %s\n", status?"true":"false");
-        if(status == false) {
-            lcd.printf("Retrying..\n");
-        }
-    }
-    wait(1.0);
-    client.setCommandCallback(processCommand);
-      
+    if(client->connect()){
+        //Subscribe to device commands with CMD Callback to process the received command
+        client->setCommandCallback(processCommand);
+
     // Create buffer to hold the event
     char buf[250];
     int count = 0;
-    
-    while(1) {
+    sprintf(buf,"{\"d\":{\"myName\":\"IoT mbed\"}}");
+
+    //Comment below stmts when sending real device events in following section
+    while(1){
+        client->publishEvent("blink", buf);
+        client->yield(1);
+        if(!client->isConnected())
+           break;
+    }
+
+    //Uncomment the below commented section to send real device events
+    /*while(1) {
         if (++count == 100) {
-            lcd.cls();
+            logMessage("\r\n");
 
             //Construct an event message with desired datapoints
             sprintf(buf,
             "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
             MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
-     
+
             //Message is converted from datapoints into json format and then published
-            status = client.publishEvent("blink", buf);
-           
-            lcd.locate(0,0);
-            lcd.printf("Publish status = %s",status?"true":"false");
+            bool status = client->publishEvent("blink", buf);
+
+            logMessage("Publish status = %s\r\n",status?"true":"false");
 
             if(status == false) {
-                // Check if connection is lost and retry 
+                // Check if connection is lost and retry
                 int re_count = 0;
-                while(!client.isConnected()) {
-                    lcd.cls();
-                    lcd.locate(0,0);
-                    lcd.printf("Reconnecting... %d", re_count++);
-                    client.reConnect();
+                while(!client->isConnected()) {
+                    logMessage("Reconnecting... %d\r\n", re_count++);
+                    client->reConnect();
                     wait(5.0);
                 }
             }
@@ -130,20 +124,34 @@
             led2 = LED2_OFF;
         else if (count % blink_interval == 0)
             led2 = !led2;
-        
-        client.yield(10);  // allow the MQTT client to receive messages
+
+        client->yield(10);  // allow the MQTT client to receive messages
+    }*/
+
+       if(client->isConnected()){
+           logMessage("Disconnecting the client from server...\r\n");
+           client->disconnect();
+        }
+        else
+           logMessage("Client already disconnected from server...\r\n");
     }
-    client.disconnect();
+    else{
+        logMessage("Client could not connect to server...\r\n");
+    }
+
+    delete client;
+    logMessage("!!! Done !!!\r\n");
+    return 0;
 }
 
 void processCommand(IoTF::Command &cmd)
 {
     LOG("Command received name: %s, payload: %s\n", cmd.getCommand(), cmd.getPayload());
-    
+
     if (strcmp(cmd.getCommand(), "blink") == 0) {
         char *payload = cmd.getPayload();
         char* pos = strchr(payload, '}');
-        
+
         if (pos != NULL) {
             *pos = '\0';
             char* ratepos = strstr(payload, "rate");
@@ -152,16 +160,14 @@
             }
             if ((pos = strchr(ratepos, ':')) != NULL)
             {
-                int blink_rate = atoi(pos + 1);       
+                int blink_rate = atoi(pos + 1);
                 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
             }
         }
     } else {
         WARN("Unsupported command: %s\n", cmd.getCommand());
     }
-    lcd.cls();
-    lcd.locate(0,0);
-    lcd.printf("blink interval = %d\n", blink_interval);
+    logMessage("blink interval = %d\n", blink_interval);
     wait(1.0);
 }
 
@@ -180,4 +186,4 @@
         else
             joystickPos = "CENTRE";
     }
-}
\ No newline at end of file
+}