Sample Client Code that showcases how to use IBMIoTF client library to connect the mbed FRDM-K64F device to the IBM Internet of Things Cloud service in registered mode.

Dependencies:   IBMIoTF

Here are the instructions to use this Client Sample:

- Get the program either in online compiler / local to the system

- Edit main.cpp to update the below pasted lines to contain correct Watson IoT Platform configuration:

char *orgId = "OrganizationID";

char *deviceType = "DeviceType";

char *deviceId = "DeviceID";

char *method = "token";

char *token = "DeviceToken";

- Save the changes and compile to get binary

- Deploy obtained binary onto FRDM K64F board and press reset button to start execution of the program

- Use any of the suitable terminal utility (screen in MAC OS) to see the log statements printed onto console by the Client Sample

- By default, the client sample after establishing secure connection to Watson IoT Platform, publishes 10 device events at the interval of 3 seconds and terminates

- Client sample subscribes to receive the device commands as well, one can try sending device commands using application, we should see a statement being dumped from processCommand callback on the console before termination of sample

Committer:
lokeshhk
Date:
Wed May 31 06:13:55 2017 +0000
Revision:
0:3caca8fc3cd2
SSL Client sample using IBMIoTF Client Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lokeshhk 0:3caca8fc3cd2 1 /*******************************************************************************
lokeshhk 0:3caca8fc3cd2 2 * Copyright (c) 2017 IBM Corp.
lokeshhk 0:3caca8fc3cd2 3 *
lokeshhk 0:3caca8fc3cd2 4 * All rights reserved. This program and the accompanying materials
lokeshhk 0:3caca8fc3cd2 5 * are made available under the terms of the Eclipse Public License v1.0
lokeshhk 0:3caca8fc3cd2 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
lokeshhk 0:3caca8fc3cd2 7 *
lokeshhk 0:3caca8fc3cd2 8 * The Eclipse Public License is available at
lokeshhk 0:3caca8fc3cd2 9 * http://www.eclipse.org/legal/epl-v10.html
lokeshhk 0:3caca8fc3cd2 10 * and the Eclipse Distribution License is available at
lokeshhk 0:3caca8fc3cd2 11 * http://www.eclipse.org/org/documents/edl-v10.php.
lokeshhk 0:3caca8fc3cd2 12 *
lokeshhk 0:3caca8fc3cd2 13 * Contributors:
lokeshhk 0:3caca8fc3cd2 14 * Lokesh K Haralakatta Initial implementation
lokeshhk 0:3caca8fc3cd2 15 *******************************************************************************/
lokeshhk 0:3caca8fc3cd2 16 #include "DeviceClient.h"
lokeshhk 0:3caca8fc3cd2 17 #include "Command.h"
lokeshhk 0:3caca8fc3cd2 18 using namespace IoTF;
lokeshhk 0:3caca8fc3cd2 19
lokeshhk 0:3caca8fc3cd2 20 // Callback Method to process the received command by the device
lokeshhk 0:3caca8fc3cd2 21 void processCommand(IoTF::Command &cmd)
lokeshhk 0:3caca8fc3cd2 22 {
lokeshhk 0:3caca8fc3cd2 23 printf("Command received name: %s, payload: %s\r\n", cmd.getCommand(), cmd.getPayload());
lokeshhk 0:3caca8fc3cd2 24 }
lokeshhk 0:3caca8fc3cd2 25
lokeshhk 0:3caca8fc3cd2 26 int main() {
lokeshhk 0:3caca8fc3cd2 27
lokeshhk 0:3caca8fc3cd2 28 // Set Watson IoT Platform connection parameters
lokeshhk 0:3caca8fc3cd2 29 // Update the below parameters with actual values before build and execution of sample
lokeshhk 0:3caca8fc3cd2 30 char *orgId = "OrganizationID"; // Replace with correct organization ID
lokeshhk 0:3caca8fc3cd2 31 char *deviceType = "DeviceType"; // Replace with correct device type as registered on WIoTP
lokeshhk 0:3caca8fc3cd2 32 char *deviceId = "DeviceID"; // Replace with correct device ID as registered on WIoTP
lokeshhk 0:3caca8fc3cd2 33 char *method = "token"; // Retain this as token for authentication method
lokeshhk 0:3caca8fc3cd2 34 char *token = "DeviceToken"; // Replace with device token as obtained from WIoTP
lokeshhk 0:3caca8fc3cd2 35 int port = 8883; // Secure port can be either 8883 or 443
lokeshhk 0:3caca8fc3cd2 36
lokeshhk 0:3caca8fc3cd2 37 // Create DeviceClient Instance using above connection parameters
lokeshhk 0:3caca8fc3cd2 38 DeviceClient *client = new DeviceClient(orgId,deviceType,deviceId,method,token,port);
lokeshhk 0:3caca8fc3cd2 39
lokeshhk 0:3caca8fc3cd2 40 // Call Device Client connect method to establish secure connection to Watson IoT Platform
lokeshhk 0:3caca8fc3cd2 41 if(client->connect()){
lokeshhk 0:3caca8fc3cd2 42 //Device Client Connected to Watson IoT Platform
lokeshhk 0:3caca8fc3cd2 43 printf("Client Connected to WIoTP\r\n");
lokeshhk 0:3caca8fc3cd2 44
lokeshhk 0:3caca8fc3cd2 45 //Subscribe to device commands with CMD Callback to process the received command
lokeshhk 0:3caca8fc3cd2 46 client->setCommandCallback(processCommand);
lokeshhk 0:3caca8fc3cd2 47
lokeshhk 0:3caca8fc3cd2 48 // Create buffer to hold the event
lokeshhk 0:3caca8fc3cd2 49 char buf[50];
lokeshhk 0:3caca8fc3cd2 50 int count = 0;
lokeshhk 0:3caca8fc3cd2 51 sprintf(buf,"{\"d\":{\"temp\":\"35\"}}");
lokeshhk 0:3caca8fc3cd2 52
lokeshhk 0:3caca8fc3cd2 53 // Keep publishing the 10 device events for every 3 seconds
lokeshhk 0:3caca8fc3cd2 54 while(1){
lokeshhk 0:3caca8fc3cd2 55 client->publishEvent("evt", buf);
lokeshhk 0:3caca8fc3cd2 56 wait(3);
lokeshhk 0:3caca8fc3cd2 57 count++;
lokeshhk 0:3caca8fc3cd2 58 if(count == 10)
lokeshhk 0:3caca8fc3cd2 59 break;
lokeshhk 0:3caca8fc3cd2 60 }
lokeshhk 0:3caca8fc3cd2 61
lokeshhk 0:3caca8fc3cd2 62 // Disconnect Device Client from WIoTP if still in connected state
lokeshhk 0:3caca8fc3cd2 63 if(client->isConnected()){
lokeshhk 0:3caca8fc3cd2 64 printf("Disconnecting the client from server...\r\n");
lokeshhk 0:3caca8fc3cd2 65 client->disconnect();
lokeshhk 0:3caca8fc3cd2 66 }
lokeshhk 0:3caca8fc3cd2 67 else
lokeshhk 0:3caca8fc3cd2 68 printf("Client already disconnected from server...\r\n");
lokeshhk 0:3caca8fc3cd2 69 }
lokeshhk 0:3caca8fc3cd2 70 else
lokeshhk 0:3caca8fc3cd2 71 printf("Client Not Connected to WIoTP\r\n");
lokeshhk 0:3caca8fc3cd2 72
lokeshhk 0:3caca8fc3cd2 73 //Release resources allocated to Device Client
lokeshhk 0:3caca8fc3cd2 74 delete client;
lokeshhk 0:3caca8fc3cd2 75
lokeshhk 0:3caca8fc3cd2 76 printf("!!! Done !!!\r\n");
lokeshhk 0:3caca8fc3cd2 77 return 0;
lokeshhk 0:3caca8fc3cd2 78 }