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:
Wed May 31 10:03:52 2017 +0000
Revision:
5:48b6090e0df1
Parent:
4:a416fef97faa
Enabled sending real device events back

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 5:48b6090e0df1 89 while(1) {
sathipal 0:555ea43ec379 90 if (++count == 100) {
lokeshhk 4:a416fef97faa 91 logMessage("\r\n");
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());
lokeshhk 4:a416fef97faa 97
sathipal 0:555ea43ec379 98 //Message is converted from datapoints into json format and then published
lokeshhk 4:a416fef97faa 99 bool status = client->publishEvent("blink", buf);
lokeshhk 4:a416fef97faa 100
lokeshhk 4:a416fef97faa 101 logMessage("Publish status = %s\r\n",status?"true":"false");
sathipal 2:2fc985a92cda 102
sathipal 2:2fc985a92cda 103 if(status == false) {
lokeshhk 4:a416fef97faa 104 // Check if connection is lost and retry
sathipal 2:2fc985a92cda 105 int re_count = 0;
lokeshhk 4:a416fef97faa 106 while(!client->isConnected()) {
lokeshhk 4:a416fef97faa 107 logMessage("Reconnecting... %d\r\n", re_count++);
lokeshhk 4:a416fef97faa 108 client->reConnect();
sathipal 2:2fc985a92cda 109 wait(5.0);
sathipal 2:2fc985a92cda 110 }
sathipal 2:2fc985a92cda 111 }
sathipal 0:555ea43ec379 112 count = 0;
sathipal 0:555ea43ec379 113 }
sathipal 0:555ea43ec379 114 if (blink_interval == 0)
sathipal 0:555ea43ec379 115 led2 = LED2_OFF;
sathipal 0:555ea43ec379 116 else if (count % blink_interval == 0)
sathipal 0:555ea43ec379 117 led2 = !led2;
lokeshhk 4:a416fef97faa 118
lokeshhk 4:a416fef97faa 119 client->yield(10); // allow the MQTT client to receive messages
lokeshhk 5:48b6090e0df1 120 }
lokeshhk 4:a416fef97faa 121
lokeshhk 4:a416fef97faa 122 if(client->isConnected()){
lokeshhk 4:a416fef97faa 123 logMessage("Disconnecting the client from server...\r\n");
lokeshhk 4:a416fef97faa 124 client->disconnect();
lokeshhk 4:a416fef97faa 125 }
lokeshhk 4:a416fef97faa 126 else
lokeshhk 4:a416fef97faa 127 logMessage("Client already disconnected from server...\r\n");
sathipal 0:555ea43ec379 128 }
lokeshhk 4:a416fef97faa 129 else{
lokeshhk 4:a416fef97faa 130 logMessage("Client could not connect to server...\r\n");
lokeshhk 4:a416fef97faa 131 }
lokeshhk 4:a416fef97faa 132
lokeshhk 4:a416fef97faa 133 delete client;
lokeshhk 4:a416fef97faa 134 logMessage("!!! Done !!!\r\n");
lokeshhk 4:a416fef97faa 135 return 0;
sathipal 0:555ea43ec379 136 }
sathipal 0:555ea43ec379 137
sathipal 0:555ea43ec379 138 void processCommand(IoTF::Command &cmd)
sathipal 0:555ea43ec379 139 {
sathipal 0:555ea43ec379 140 LOG("Command received name: %s, payload: %s\n", cmd.getCommand(), cmd.getPayload());
lokeshhk 4:a416fef97faa 141
sathipal 0:555ea43ec379 142 if (strcmp(cmd.getCommand(), "blink") == 0) {
sathipal 0:555ea43ec379 143 char *payload = cmd.getPayload();
sathipal 0:555ea43ec379 144 char* pos = strchr(payload, '}');
lokeshhk 4:a416fef97faa 145
sathipal 0:555ea43ec379 146 if (pos != NULL) {
sathipal 0:555ea43ec379 147 *pos = '\0';
sathipal 0:555ea43ec379 148 char* ratepos = strstr(payload, "rate");
sathipal 0:555ea43ec379 149 if(ratepos == NULL) {
sathipal 0:555ea43ec379 150 return;
sathipal 0:555ea43ec379 151 }
sathipal 0:555ea43ec379 152 if ((pos = strchr(ratepos, ':')) != NULL)
sathipal 0:555ea43ec379 153 {
lokeshhk 4:a416fef97faa 154 int blink_rate = atoi(pos + 1);
sathipal 0:555ea43ec379 155 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
sathipal 0:555ea43ec379 156 }
sathipal 0:555ea43ec379 157 }
sathipal 0:555ea43ec379 158 } else {
sathipal 0:555ea43ec379 159 WARN("Unsupported command: %s\n", cmd.getCommand());
sathipal 0:555ea43ec379 160 }
lokeshhk 4:a416fef97faa 161 logMessage("blink interval = %d\n", blink_interval);
sathipal 1:e58533b6bc6b 162 wait(1.0);
sathipal 0:555ea43ec379 163 }
sathipal 0:555ea43ec379 164
sathipal 0:555ea43ec379 165 void joystickThread(void const *args) {
sathipal 0:555ea43ec379 166 while (1) {
sathipal 0:555ea43ec379 167 if (Down)
sathipal 0:555ea43ec379 168 joystickPos = "DOWN";
sathipal 0:555ea43ec379 169 else if (Left)
sathipal 0:555ea43ec379 170 joystickPos = "LEFT";
sathipal 0:555ea43ec379 171 else if (Click)
sathipal 0:555ea43ec379 172 joystickPos = "CLICK";
sathipal 0:555ea43ec379 173 else if (Up)
sathipal 0:555ea43ec379 174 joystickPos = "UP";
sathipal 0:555ea43ec379 175 else if (Right)
sathipal 0:555ea43ec379 176 joystickPos = "RIGHT";
sathipal 0:555ea43ec379 177 else
sathipal 0:555ea43ec379 178 joystickPos = "CENTRE";
sathipal 0:555ea43ec379 179 }
lokeshhk 4:a416fef97faa 180 }