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:
Tue Jan 12 08:13:38 2016 +0000
Revision:
3:e7b2f56c4f3f
Parent:
2:2fc985a92cda
Child:
4:a416fef97faa
keep the sample in quickstart mode by default

Who changed what in which revision?

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