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

Committer:
sathipal
Date:
Fri Nov 06 07:39:04 2015 +0000
Revision:
0:555ea43ec379
Child:
1:e58533b6bc6b
Sample code that shows how to use IBMIoTF library to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sathipal 0:555ea43ec379 1 /*******************************************************************************
sathipal 0:555ea43ec379 2 * Copyright (c) 2014 IBM Corporation and other Contributors.
sathipal 0:555ea43ec379 3 *
sathipal 0:555ea43ec379 4 * All rights reserved. This program and the accompanying materials
sathipal 0:555ea43ec379 5 * are made available under the terms of the Eclipse Public License v1.0
sathipal 0:555ea43ec379 6 * which accompanies this distribution, and is available at
sathipal 0:555ea43ec379 7 * http://www.eclipse.org/legal/epl-v10.html
sathipal 0:555ea43ec379 8 *
sathipal 0:555ea43ec379 9 * Contributors: Sathiskumar Palaniappan
sathipal 0:555ea43ec379 10 * IBM - Initial Contribution
sathipal 0:555ea43ec379 11 *******************************************************************************/
sathipal 0:555ea43ec379 12
sathipal 0:555ea43ec379 13 #include "stdio.h"
sathipal 0:555ea43ec379 14 #include "mbed.h"
sathipal 0:555ea43ec379 15 #include "rtos.h"
sathipal 0:555ea43ec379 16 #include "EthernetInterface.h"
sathipal 0:555ea43ec379 17 #include "C12832.h"
sathipal 0:555ea43ec379 18 #include "LM75B.h"
sathipal 0:555ea43ec379 19 #include "MMA7660.h"
sathipal 0:555ea43ec379 20 #include "Arial12x12.h"
sathipal 0:555ea43ec379 21 #include "DeviceClient.h"
sathipal 0:555ea43ec379 22 #include "Command.h"
sathipal 0:555ea43ec379 23 #include "MQTTEthernet.h"
sathipal 0:555ea43ec379 24
sathipal 0:555ea43ec379 25 #include "MQTTClient.h"
sathipal 0:555ea43ec379 26
sathipal 0:555ea43ec379 27 #if defined(TARGET_UBLOX_C027)
sathipal 0:555ea43ec379 28 #warning "Compiling for mbed C027"
sathipal 0:555ea43ec379 29 #include "C027.h"
sathipal 0:555ea43ec379 30 #elif defined(TARGET_LPC1768)
sathipal 0:555ea43ec379 31 #warning "Compiling for mbed LPC1768"
sathipal 0:555ea43ec379 32 #include "LPC1768.h"
sathipal 0:555ea43ec379 33 #elif defined(TARGET_K64F)
sathipal 0:555ea43ec379 34 #warning "Compiling for mbed K64F"
sathipal 0:555ea43ec379 35 #include "K64F.h"
sathipal 0:555ea43ec379 36 #endif
sathipal 0:555ea43ec379 37
sathipal 0:555ea43ec379 38 //LED
sathipal 0:555ea43ec379 39 DigitalOut led1(LED1);
sathipal 0:555ea43ec379 40
sathipal 0:555ea43ec379 41 char *joystickPos;
sathipal 0:555ea43ec379 42 void joystickThread(void const *args);
sathipal 0:555ea43ec379 43 void processCommand(IoTF::Command &cmd);
sathipal 0:555ea43ec379 44
sathipal 0:555ea43ec379 45 int blink_interval = 0;
sathipal 0:555ea43ec379 46 int main()
sathipal 0:555ea43ec379 47 {
sathipal 0:555ea43ec379 48 lcd.cls();
sathipal 0:555ea43ec379 49 lcd.locate(0,0);
sathipal 0:555ea43ec379 50
sathipal 0:555ea43ec379 51 lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen
sathipal 0:555ea43ec379 52 led2 = LED2_OFF; // K64F: turn off the main board LED
sathipal 0:555ea43ec379 53
sathipal 0:555ea43ec379 54 //Start thread to read data from joystick
sathipal 0:555ea43ec379 55 Thread jThd(joystickThread);
sathipal 0:555ea43ec379 56 joystickPos = "CENTRE";
sathipal 0:555ea43ec379 57
sathipal 0:555ea43ec379 58 // Set IoT Foundation connection parameters
sathipal 0:555ea43ec379 59 char organization[11] = "quickstart"; // For a registered connection, replace with your org
sathipal 0:555ea43ec379 60 char deviceType[9] = "SampleDT"; // For a registered connection, replace with your type
sathipal 0:555ea43ec379 61 char deviceId[8] = ""; // For a registered connection, replace with your id
sathipal 0:555ea43ec379 62 char method[6] = "token"; // Not required to change as IBM IoTF expects only token for now
sathipal 0:555ea43ec379 63 char token[9] = "password"; // For a registered connection, replace with your auth-token
sathipal 0:555ea43ec379 64
sathipal 0:555ea43ec379 65 // Create DeviceClient
sathipal 0:555ea43ec379 66 IoTF::DeviceClient client(organization, deviceType, deviceId, method, token);
sathipal 0:555ea43ec379 67
sathipal 0:555ea43ec379 68 // Get the MAC address if we are in quickstart mode and device id is not specified
sathipal 0:555ea43ec379 69 if((strcmp(organization, QUICKSTART) == 0) && (strcmp("", deviceId) == 0)) {
sathipal 0:555ea43ec379 70 char tmpBuf[50];
sathipal 0:555ea43ec379 71 client.getMac(tmpBuf, sizeof(tmpBuf));
sathipal 0:555ea43ec379 72 lcd.printf("Got MAC: %s\n", tmpBuf);
sathipal 0:555ea43ec379 73 wait(10.0);
sathipal 0:555ea43ec379 74 }
sathipal 0:555ea43ec379 75 lcd.locate(0,10);
sathipal 0:555ea43ec379 76 lcd.printf("Initialized & connecting..\n");
sathipal 0:555ea43ec379 77
sathipal 0:555ea43ec379 78 bool status = client.connect();
sathipal 0:555ea43ec379 79 lcd.cls();
sathipal 0:555ea43ec379 80 lcd.locate(0,0);
sathipal 0:555ea43ec379 81 lcd.printf("connect status: %s\n", status?"true":"false");
sathipal 0:555ea43ec379 82 wait(1.0);
sathipal 0:555ea43ec379 83 client.setCommandCallback(processCommand);
sathipal 0:555ea43ec379 84
sathipal 0:555ea43ec379 85 // Create buffer to hold the event
sathipal 0:555ea43ec379 86 char buf[250];
sathipal 0:555ea43ec379 87 int count = 0;
sathipal 0:555ea43ec379 88
sathipal 0:555ea43ec379 89 while(1) {
sathipal 0:555ea43ec379 90 if (++count == 100) {
sathipal 0:555ea43ec379 91 lcd.cls();
sathipal 0:555ea43ec379 92
sathipal 0:555ea43ec379 93 //Construct an event message with desired datapoints
sathipal 0:555ea43ec379 94 sprintf(buf,
sathipal 0:555ea43ec379 95 "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
sathipal 0:555ea43ec379 96 MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
sathipal 0:555ea43ec379 97
sathipal 0:555ea43ec379 98 //Message is converted from datapoints into json format and then published
sathipal 0:555ea43ec379 99 status = client.publishEvent("blink", buf);
sathipal 0:555ea43ec379 100 lcd.locate(0,0);
sathipal 0:555ea43ec379 101 lcd.printf("Publish status = %s",status?"true":"false");
sathipal 0:555ea43ec379 102
sathipal 0:555ea43ec379 103 count = 0;
sathipal 0:555ea43ec379 104 }
sathipal 0:555ea43ec379 105 if (blink_interval == 0)
sathipal 0:555ea43ec379 106 led2 = LED2_OFF;
sathipal 0:555ea43ec379 107 else if (count % blink_interval == 0)
sathipal 0:555ea43ec379 108 led2 = !led2;
sathipal 0:555ea43ec379 109
sathipal 0:555ea43ec379 110 client.yield(10); // allow the MQTT client to receive messages
sathipal 0:555ea43ec379 111 }
sathipal 0:555ea43ec379 112 client.disconnect();
sathipal 0:555ea43ec379 113 }
sathipal 0:555ea43ec379 114
sathipal 0:555ea43ec379 115 void processCommand(IoTF::Command &cmd)
sathipal 0:555ea43ec379 116 {
sathipal 0:555ea43ec379 117 LOG("Command received name: %s, payload: %s\n", cmd.getCommand(), cmd.getPayload());
sathipal 0:555ea43ec379 118
sathipal 0:555ea43ec379 119 if (strcmp(cmd.getCommand(), "blink") == 0) {
sathipal 0:555ea43ec379 120 char *payload = cmd.getPayload();
sathipal 0:555ea43ec379 121 char* pos = strchr(payload, '}');
sathipal 0:555ea43ec379 122
sathipal 0:555ea43ec379 123 if (pos != NULL) {
sathipal 0:555ea43ec379 124 *pos = '\0';
sathipal 0:555ea43ec379 125 char* ratepos = strstr(payload, "rate");
sathipal 0:555ea43ec379 126 if(ratepos == NULL) {
sathipal 0:555ea43ec379 127 return;
sathipal 0:555ea43ec379 128 }
sathipal 0:555ea43ec379 129 if ((pos = strchr(ratepos, ':')) != NULL)
sathipal 0:555ea43ec379 130 {
sathipal 0:555ea43ec379 131 int blink_rate = atoi(pos + 1);
sathipal 0:555ea43ec379 132 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
sathipal 0:555ea43ec379 133 }
sathipal 0:555ea43ec379 134 }
sathipal 0:555ea43ec379 135 } else {
sathipal 0:555ea43ec379 136 WARN("Unsupported command: %s\n", cmd.getCommand());
sathipal 0:555ea43ec379 137 }
sathipal 0:555ea43ec379 138 lcd.cls();
sathipal 0:555ea43ec379 139 lcd.locate(0,0);
sathipal 0:555ea43ec379 140 lcd.printf("blink interval = %d\n", blink_interval);
sathipal 0:555ea43ec379 141 }
sathipal 0:555ea43ec379 142
sathipal 0:555ea43ec379 143 void joystickThread(void const *args) {
sathipal 0:555ea43ec379 144 while (1) {
sathipal 0:555ea43ec379 145 if (Down)
sathipal 0:555ea43ec379 146 joystickPos = "DOWN";
sathipal 0:555ea43ec379 147 else if (Left)
sathipal 0:555ea43ec379 148 joystickPos = "LEFT";
sathipal 0:555ea43ec379 149 else if (Click)
sathipal 0:555ea43ec379 150 joystickPos = "CLICK";
sathipal 0:555ea43ec379 151 else if (Up)
sathipal 0:555ea43ec379 152 joystickPos = "UP";
sathipal 0:555ea43ec379 153 else if (Right)
sathipal 0:555ea43ec379 154 joystickPos = "RIGHT";
sathipal 0:555ea43ec379 155 else
sathipal 0:555ea43ec379 156 joystickPos = "CENTRE";
sathipal 0:555ea43ec379 157 }
sathipal 0:555ea43ec379 158 }