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:
0:555ea43ec379
Child:
1:e58533b6bc6b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Main.cpp	Fri Nov 06 07:39:04 2015 +0000
@@ -0,0 +1,158 @@
+/*******************************************************************************
+* Copyright (c) 2014 IBM Corporation and other Contributors.
+*
+* All rights reserved. This program and the accompanying materials
+* are made available under the terms of the Eclipse Public License v1.0
+* which accompanies this distribution, and is available at
+* http://www.eclipse.org/legal/epl-v10.html
+*
+* Contributors: Sathiskumar Palaniappan
+* IBM - Initial Contribution
+*******************************************************************************/
+
+#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"
+#include "C027.h"
+#elif defined(TARGET_LPC1768)
+#warning "Compiling for mbed LPC1768"
+#include "LPC1768.h"
+#elif defined(TARGET_K64F)
+#warning "Compiling for mbed K64F"
+#include "K64F.h"
+#endif
+
+//LED
+DigitalOut led1(LED1);
+
+char *joystickPos;
+void joystickThread(void const *args);
+void processCommand(IoTF::Command &cmd);
+
+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 
+    
+    //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] = "SampleDT";        // For a registered connection, replace with your type
+    char deviceId[8] = "";                  // For a registered connection, replace with your 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
+    
+    // Create DeviceClient
+    IoTF::DeviceClient client(organization, deviceType, deviceId, method, token);
+    
+    // Get the 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.getMac(tmpBuf, sizeof(tmpBuf));
+        lcd.printf("Got MAC: %s\n", tmpBuf);
+        wait(10.0);
+    }
+    lcd.locate(0,10);
+    lcd.printf("Initialized & connecting..\n");
+    
+    bool status = client.connect();
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("connect status: %s\n", status?"true":"false");
+    wait(1.0);
+    client.setCommandCallback(processCommand);
+      
+    // Create buffer to hold the event
+    char buf[250];
+    int count = 0;
+    
+    while(1) {
+        if (++count == 100) {
+            lcd.cls();
+
+            //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");
+            
+            count = 0;
+        }
+        if (blink_interval == 0)
+            led2 = LED2_OFF;
+        else if (count % blink_interval == 0)
+            led2 = !led2;
+        
+        client.yield(10);  // allow the MQTT client to receive messages
+    }
+    client.disconnect();
+}
+
+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");
+            if(ratepos == NULL) {
+                return;
+            }
+            if ((pos = strchr(ratepos, ':')) != NULL)
+            {
+                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);
+}
+
+void joystickThread(void const *args) {
+    while (1) {
+        if (Down)
+            joystickPos = "DOWN";
+        else if (Left)
+            joystickPos = "LEFT";
+        else if (Click)
+            joystickPos = "CLICK";
+        else if (Up)
+            joystickPos = "UP";
+        else if (Right)
+            joystickPos = "RIGHT";
+        else
+            joystickPos = "CENTRE";
+    }
+}
\ No newline at end of file