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:
lokeshhk
Date:
Tue May 30 06:59:13 2017 +0000
Revision:
4:a416fef97faa
Parent:
3:e7b2f56c4f3f
Child:
5:48b6090e0df1
Updated to use latest IBMIoTF library as part of mbed-os porting and ssl/tls support

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
lokeshhk 4:a416fef97faa 15 * Lokesh K Haralakatta - Port to support mbed os 5
sathipal 1:e58533b6bc6b 16 *******************************************************************************/
sathipal 0:555ea43ec379 17
lokeshhk 4:a416fef97faa 18 // change this to 1 to output messages to LCD
lokeshhk 4:a416fef97faa 19 #define USE_LCD 0
lokeshhk 4:a416fef97faa 20
lokeshhk 4:a416fef97faa 21 #if USE_LCD
lokeshhk 4:a416fef97faa 22 #include "C12832.h"
lokeshhk 4:a416fef97faa 23 #include "Arial12x12.h"
lokeshhk 4:a416fef97faa 24 #define logMessage lcd.cls();lcd.locate(0,0);lcd.printf
lokeshhk 4:a416fef97faa 25 #define lcdFont lcd.set_font((unsigned char*) Arial12x12);
lokeshhk 4:a416fef97faa 26 #else
lokeshhk 4:a416fef97faa 27 #define logMessage printf
lokeshhk 4:a416fef97faa 28 #define lcdFont
lokeshhk 4:a416fef97faa 29 #endif
lokeshhk 4:a416fef97faa 30
sathipal 0:555ea43ec379 31 #include "stdio.h"
sathipal 0:555ea43ec379 32 #include "mbed.h"
sathipal 0:555ea43ec379 33 #include "LM75B.h"
sathipal 0:555ea43ec379 34 #include "MMA7660.h"
sathipal 0:555ea43ec379 35 #include "DeviceClient.h"
sathipal 0:555ea43ec379 36 #include "Command.h"
sathipal 0:555ea43ec379 37
sathipal 0:555ea43ec379 38 #if defined(TARGET_UBLOX_C027)
sathipal 0:555ea43ec379 39 #warning "Compiling for mbed C027"
sathipal 0:555ea43ec379 40 #include "C027.h"
sathipal 0:555ea43ec379 41 #elif defined(TARGET_LPC1768)
sathipal 0:555ea43ec379 42 #warning "Compiling for mbed LPC1768"
sathipal 0:555ea43ec379 43 #include "LPC1768.h"
sathipal 0:555ea43ec379 44 #elif defined(TARGET_K64F)
sathipal 0:555ea43ec379 45 #warning "Compiling for mbed K64F"
sathipal 0:555ea43ec379 46 #include "K64F.h"
sathipal 0:555ea43ec379 47 #endif
sathipal 0:555ea43ec379 48
sathipal 0:555ea43ec379 49 //LED
sathipal 0:555ea43ec379 50 DigitalOut led1(LED1);
sathipal 0:555ea43ec379 51
sathipal 0:555ea43ec379 52 char *joystickPos;
sathipal 0:555ea43ec379 53 void joystickThread(void const *args);
sathipal 0:555ea43ec379 54 void processCommand(IoTF::Command &cmd);
sathipal 0:555ea43ec379 55
sathipal 0:555ea43ec379 56 int blink_interval = 0;
sathipal 0:555ea43ec379 57 int main()
sathipal 0:555ea43ec379 58 {
lokeshhk 4:a416fef97faa 59 //Set LCD font if USE_LCD == 1
lokeshhk 4:a416fef97faa 60 lcdFont
lokeshhk 4:a416fef97faa 61
lokeshhk 4:a416fef97faa 62 // K64F: turn off the main board LED
lokeshhk 4:a416fef97faa 63 led2 = LED2_OFF;
lokeshhk 4:a416fef97faa 64
sathipal 0:555ea43ec379 65 //Start thread to read data from joystick
sathipal 0:555ea43ec379 66 Thread jThd(joystickThread);
sathipal 0:555ea43ec379 67 joystickPos = "CENTRE";
sathipal 0:555ea43ec379 68
lokeshhk 4:a416fef97faa 69
sathipal 0:555ea43ec379 70 // Set IoT Foundation connection parameters
lokeshhk 4:a416fef97faa 71 char *organization = "quickstart"; // For a registered connection, replace with your org
lokeshhk 4:a416fef97faa 72 char *deviceType = "qsType"; // For a registered connection, replace with your device type
lokeshhk 4:a416fef97faa 73 char *deviceId = "qsDevice"; // For a registered connection, replace with your device id
lokeshhk 4:a416fef97faa 74 char *method = "token"; // Not required to change as IBM IoTF expects only "token" for now
lokeshhk 4:a416fef97faa 75 char *token = "password"; // For a registered connection, replace with your auth-token
lokeshhk 4:a416fef97faa 76
sathipal 0:555ea43ec379 77 // Create DeviceClient
lokeshhk 4:a416fef97faa 78 IoTF::DeviceClient *client = new IoTF::DeviceClient(organization, deviceType, deviceId);
sathipal 0:555ea43ec379 79
lokeshhk 4:a416fef97faa 80 if(client->connect()){
lokeshhk 4:a416fef97faa 81 //Subscribe to device commands with CMD Callback to process the received command
lokeshhk 4:a416fef97faa 82 client->setCommandCallback(processCommand);
lokeshhk 4:a416fef97faa 83
sathipal 0:555ea43ec379 84 // Create buffer to hold the event
sathipal 0:555ea43ec379 85 char buf[250];
sathipal 0:555ea43ec379 86 int count = 0;
lokeshhk 4:a416fef97faa 87 sprintf(buf,"{\"d\":{\"myName\":\"IoT mbed\"}}");
lokeshhk 4:a416fef97faa 88
lokeshhk 4:a416fef97faa 89 //Comment below stmts when sending real device events in following section
lokeshhk 4:a416fef97faa 90 while(1){
lokeshhk 4:a416fef97faa 91 client->publishEvent("blink", buf);
lokeshhk 4:a416fef97faa 92 client->yield(1);
lokeshhk 4:a416fef97faa 93 if(!client->isConnected())
lokeshhk 4:a416fef97faa 94 break;
lokeshhk 4:a416fef97faa 95 }
lokeshhk 4:a416fef97faa 96
lokeshhk 4:a416fef97faa 97 //Uncomment the below commented section to send real device events
lokeshhk 4:a416fef97faa 98 /*while(1) {
sathipal 0:555ea43ec379 99 if (++count == 100) {
lokeshhk 4:a416fef97faa 100 logMessage("\r\n");
sathipal 0:555ea43ec379 101
sathipal 0:555ea43ec379 102 //Construct an event message with desired datapoints
sathipal 0:555ea43ec379 103 sprintf(buf,
sathipal 0:555ea43ec379 104 "{\"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 105 MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
lokeshhk 4:a416fef97faa 106
sathipal 0:555ea43ec379 107 //Message is converted from datapoints into json format and then published
lokeshhk 4:a416fef97faa 108 bool status = client->publishEvent("blink", buf);
lokeshhk 4:a416fef97faa 109
lokeshhk 4:a416fef97faa 110 logMessage("Publish status = %s\r\n",status?"true":"false");
sathipal 2:2fc985a92cda 111
sathipal 2:2fc985a92cda 112 if(status == false) {
lokeshhk 4:a416fef97faa 113 // Check if connection is lost and retry
sathipal 2:2fc985a92cda 114 int re_count = 0;
lokeshhk 4:a416fef97faa 115 while(!client->isConnected()) {
lokeshhk 4:a416fef97faa 116 logMessage("Reconnecting... %d\r\n", re_count++);
lokeshhk 4:a416fef97faa 117 client->reConnect();
sathipal 2:2fc985a92cda 118 wait(5.0);
sathipal 2:2fc985a92cda 119 }
sathipal 2:2fc985a92cda 120 }
sathipal 0:555ea43ec379 121 count = 0;
sathipal 0:555ea43ec379 122 }
sathipal 0:555ea43ec379 123 if (blink_interval == 0)
sathipal 0:555ea43ec379 124 led2 = LED2_OFF;
sathipal 0:555ea43ec379 125 else if (count % blink_interval == 0)
sathipal 0:555ea43ec379 126 led2 = !led2;
lokeshhk 4:a416fef97faa 127
lokeshhk 4:a416fef97faa 128 client->yield(10); // allow the MQTT client to receive messages
lokeshhk 4:a416fef97faa 129 }*/
lokeshhk 4:a416fef97faa 130
lokeshhk 4:a416fef97faa 131 if(client->isConnected()){
lokeshhk 4:a416fef97faa 132 logMessage("Disconnecting the client from server...\r\n");
lokeshhk 4:a416fef97faa 133 client->disconnect();
lokeshhk 4:a416fef97faa 134 }
lokeshhk 4:a416fef97faa 135 else
lokeshhk 4:a416fef97faa 136 logMessage("Client already disconnected from server...\r\n");
sathipal 0:555ea43ec379 137 }
lokeshhk 4:a416fef97faa 138 else{
lokeshhk 4:a416fef97faa 139 logMessage("Client could not connect to server...\r\n");
lokeshhk 4:a416fef97faa 140 }
lokeshhk 4:a416fef97faa 141
lokeshhk 4:a416fef97faa 142 delete client;
lokeshhk 4:a416fef97faa 143 logMessage("!!! Done !!!\r\n");
lokeshhk 4:a416fef97faa 144 return 0;
sathipal 0:555ea43ec379 145 }
sathipal 0:555ea43ec379 146
sathipal 0:555ea43ec379 147 void processCommand(IoTF::Command &cmd)
sathipal 0:555ea43ec379 148 {
sathipal 0:555ea43ec379 149 LOG("Command received name: %s, payload: %s\n", cmd.getCommand(), cmd.getPayload());
lokeshhk 4:a416fef97faa 150
sathipal 0:555ea43ec379 151 if (strcmp(cmd.getCommand(), "blink") == 0) {
sathipal 0:555ea43ec379 152 char *payload = cmd.getPayload();
sathipal 0:555ea43ec379 153 char* pos = strchr(payload, '}');
lokeshhk 4:a416fef97faa 154
sathipal 0:555ea43ec379 155 if (pos != NULL) {
sathipal 0:555ea43ec379 156 *pos = '\0';
sathipal 0:555ea43ec379 157 char* ratepos = strstr(payload, "rate");
sathipal 0:555ea43ec379 158 if(ratepos == NULL) {
sathipal 0:555ea43ec379 159 return;
sathipal 0:555ea43ec379 160 }
sathipal 0:555ea43ec379 161 if ((pos = strchr(ratepos, ':')) != NULL)
sathipal 0:555ea43ec379 162 {
lokeshhk 4:a416fef97faa 163 int blink_rate = atoi(pos + 1);
sathipal 0:555ea43ec379 164 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
sathipal 0:555ea43ec379 165 }
sathipal 0:555ea43ec379 166 }
sathipal 0:555ea43ec379 167 } else {
sathipal 0:555ea43ec379 168 WARN("Unsupported command: %s\n", cmd.getCommand());
sathipal 0:555ea43ec379 169 }
lokeshhk 4:a416fef97faa 170 logMessage("blink interval = %d\n", blink_interval);
sathipal 1:e58533b6bc6b 171 wait(1.0);
sathipal 0:555ea43ec379 172 }
sathipal 0:555ea43ec379 173
sathipal 0:555ea43ec379 174 void joystickThread(void const *args) {
sathipal 0:555ea43ec379 175 while (1) {
sathipal 0:555ea43ec379 176 if (Down)
sathipal 0:555ea43ec379 177 joystickPos = "DOWN";
sathipal 0:555ea43ec379 178 else if (Left)
sathipal 0:555ea43ec379 179 joystickPos = "LEFT";
sathipal 0:555ea43ec379 180 else if (Click)
sathipal 0:555ea43ec379 181 joystickPos = "CLICK";
sathipal 0:555ea43ec379 182 else if (Up)
sathipal 0:555ea43ec379 183 joystickPos = "UP";
sathipal 0:555ea43ec379 184 else if (Right)
sathipal 0:555ea43ec379 185 joystickPos = "RIGHT";
sathipal 0:555ea43ec379 186 else
sathipal 0:555ea43ec379 187 joystickPos = "CENTRE";
sathipal 0:555ea43ec379 188 }
lokeshhk 4:a416fef97faa 189 }