• Home
  • Resources
    • User Guides
    • Concepts Articles
    • API References
    • Client Libraries & SDKs
    • Product Updates
  • FAQs
    • General
    • IoTConnect
      • Device Rules
      • Devices
      • Settings
      • Device Commands
      • Device Templates
      • Device Firmware
      • Device Onboarding
    • Security
  • Contact Us
  • Home
  • Resources
    • User Guides
    • Concepts Articles
    • API References
    • Client Libraries & SDKs
    • Product Updates
  • FAQs
    • General
    • IoTConnect
      • Device Rules
      • Devices
      • Settings
      • Device Commands
      • Device Templates
      • Device Firmware
      • Device Onboarding
    • Security
  • Contact Us

Client Libraries & SDKs

home/Documentation/Client Libraries & SDKs
  • Device SDK Reference
    • Overview
    • Device Authentication Options
    • Data Flow
    • Public Methods
      • Device Connection
      • Send Telemetry
      • Get Attributes
      • Send acknowledgment
      • Update Twin
    • SDKs (Message version 1.0)
      • DOT NET (C#)
      • Node.js
      • Java
      • Python
      • iOS
      • Android
      • Embedded
      • C language
      • Build Your Own SDK
      • Node-RED
        • IoTConnect Node-red 1.0.9
        • IoTConnect Node-red 1.0.8
      • Node-RED Subflow
        • Single Device
        • Multiple Device
    • SDKs (Message version 2.1)
      • Python
      • DOT NET (C#)
      • C language
        • IoTConnect Generic C SDK
          • Linux Build Instructions
          • Windows Build Instructions
      • Python 1.1
    • Code Samples
      • Node.js
        • TPM 3.1
        • Standard SDK 3.0
        • Standard
        • TPM
        • Standard SDK 3.0.1
        • Standard 2.0
      • Java
        • Standard
        • Standard 2.0
        • Standard 3.0
      • Python
        • Standard 2.0
        • TPM 2.1
        • Standard 3.0.1
        • Standard
        • TPM
          • TPM 3.0.1
        • TPM 3.0
        • Standard 3.0.2
        • Standard 3.0.3
        • Standard 3.0.4
        • Python SDK
      • iOS
        • ios1.0.1
      • C Language
        • nRF9160 DK
        • STM32L4
        • ESP32
        • Standard 3.0
        • Standard 2.0
        • Standard 3.1
      • Android
        • Standard 3.0
        • Standard 2.0.6
        • Standard 2.0.0
      • DOT NET (C#)
        • Standard
        • Standard 2.0
        • Standard 3.0
    • Device Message 1.0
      • D2C (Device to Cloud)
        • IoTConnect SDK
        • Custom SDK
      • C2D (Cloud to Device)
        • IoTConnect SDK
        • Custom SDK
      • Troubleshooting & Response codes
      • Know Your Essentials
    • Device Message 2.1
    • AWS Device Message 2.1
    • SDK Deep-diving
Download PDF

Device Connection

In this article
  • Javascript
  • Java
  • Python
  • Dotnet C#
  • C Language
  • iOS
  • Andorid

Each SDK will have a method/constructor to create an instance of SDK which is to discover the device’s identity and get it connected to IoTConnect’s Azure IoTHub.

This page will walk thru you to help to understand the public methods provided by IoTConnect’s SDK and their usages

  • Get your Environment(ENV) Details
  • Get your CPID

Javascript

var iotConnectSDK = 
new sdk(cpid, uniqueId, callbackMessage, twinCallbackMessage, env, sdkOptions);

CPID – Get your CPID
UNIQUEID  – Your Device ID.
CALLBACKMESSAGE  – Callback function. You can receive all the messages and commands.
TWINCALLBACKMESSAGE  – It is a callback function. You can receive twin messages.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details
SDKOPTIONS – Optional pram, this use when select X.509 Authentication method for the device template.

var twinCallbackMessage = function twinCallbackMessage(data){
    console.log(data); 
}
var callbackMessage = function callbackMessage(data){
    if(data.cmdType == '0x01') { // Device command
    else if(data.cmdType == '0x02') { // Firmware OTA command
    } 
} 

var sdkOptions = {
        "certificate" : { 
                "SSLKeyPath"    : "<< file path >>/key.pem",
                "SSLCertPath"   : "<< file path >>/cert.pem",
                "SSLCaPath"     : "<< file path >>/ca.cert.pem"
        }
}

Java SDK

//Create iotconnect sdk instance
IoTConnectSDK ioTConnectSDK = new IoTConnectSDKImpl
        (cpId, uniqueId, env, callerClassObject, callerClassObject);

CPID – Get your CPID
UNIQUEID – Your Device ID.
CALLERCLASSOBJECT – It is a callback to receive all the messages and commands.
ENV –Optional param (Default value is PROD),Get your Environment(ENV) Details

//Callback class
class CBImplementor implements SDKCallback, TwinCallback {
	//Implement a method to receive the command from Cloud 
	@Override
	public void sdkCallbackMethod(CloudResponse cloudResponse) {
		if(cloudResponse!=null) {
		System.out.println("ACK : " + cloudResponse.getAck());
		System.out.println("ACK ID : " + cloudResponse.getAckId());
		System.out.println("Command : " + cloudResponse.getCommand());
		System.out.println("UniqueId : " + cloudResponse.getUniqueId());
		System.out.println("Value : " + cloudResponse.getValue());
		}      
	}
	@Override
	public void twinCallBackMethod(Map<String, String> arg0) {
		if(arg0.size() > 0){
			System.out.println(arg0.toString());
			System.out.println("Twin Property Updated");
		}
	}
}

Python

SDK With Symantec And X.509 Auth

with IoTConnectSDK(cpid, uniqueId, 
            callbackMessage, callbackTwinMessage, env) as sdk:

CPID – Get your CPID
UNIQUEID  – Your Device ID.
CALLBACKMESSAGE  – Callback function. You can receive all the messages and commands.
CALLBACKTWINMESSAGE – It is a callback function. You can receive twin messages.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

def callbackTwinMessage(msg):
def callbackMessage(msg):

SDK With TPM Auth

with IoTConnectSDK(cpId, uniqueId,scopeId, 
             callbackMessage, callbackTwinMessage, env) as sdk:

CPID – Get your CPID
UNIQUEID  – Your Device ID.
SCOPEID – It is given by our team or contact our team to get the details of ScopeId.
CALLBACKMESSAGE  – Callback function. You can receive all the messages and commands.
CALLBACKTWINMESSAGE – It is a callback function. You can receive twin messages.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

def callbackTwinMessage(msg):
def callbackMessage(msg):

DOT NET (C#)

using IOTConnectSDK;
string cpId = "", uniqueId = "";
SDKClient client = new SDKClient(cpId, uniqueId, 
                  FirmwareDelegate, TwinUpdateCallBack,env);

CPID – Get your CPID
UNIQUEID  – Your Device ID.
FIRMWAREDELEGATE – It is a callback function. You can receive all the messages and commands.
TWINUPDATECALLBACK – It is a callback function. You can receive twin messages.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

public static Task FirmwareDelegate(string message)
{
    Console.WriteLine("Command Received.");
    Console.WriteLine(message);
    return Task.CompletedTask;
}
public static Task TwinUpdateCallBack(Dictionary<string, object> twins)
{
    Console.WriteLine("Twin Update received.");
    foreach (var twin in twins)
    {
        Console.WriteLine($"{twin.Key}:{twin.Value}");
    }
    return Task.CompletedTask;
}

C Language

NRF9160 Board

IoTConnect_init(IOTCONNECT_DEVICE_CP_ID, IOTCONNECT_DEVICE_UNIQUE_ID,
           IOTCONNECT_DEVICE_ENV, Device_CallBack, Twin_CallBack);

IOTCONNECT_DEVICE_CP_ID – you get from IoT hub while creating templates.
IOTCONNECT_DEVICE_UNIQUE_ID  – you get from IoT hub while creating templates.
Device_CallBack – It is a callback function. You can receive all the messages and commands.
Twin_CallBack It is a callback function. You can receive twin messages.
IOTCONNECT_DEVICE_ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

void Device_CallBack(char *topic, char *payload) {      
    
    cJSON *Ack_Json;
    int Status = 0,mt=0;
    char *cmd_ackID, *Cmd_value, *Ack_Json_Data;
    printk("\n Cmd_msg >>  %s",payload);   
     
    cJSON *root = cJSON_Parse(payload);
    cmd_ackID = (cJSON_GetObjectItem(root, "ackId"))->valuestring;
    Cmd_value = (cJSON_GetObjectItem(root, "cmdType"))->valuestring;
    if( !strcmp(Cmd_value,"0x01") ){Status = 6; mt = 5;}
    else if( !strcmp(Cmd_value,"0x02") ) {Status = 7; mt = 11;}
    else { };
    Ack_Json = cJSON_CreateObject();
    if (Ack_Json == NULL){
        printk("\nUnable to allocate Ack_Json Object in Device_CallBack");
        return ;    
    }
    cJSON_AddStringToObject(Ack_Json, "ackId",cmd_ackID);
    cJSON_AddStringToObject(Ack_Json, "msg","");
    cJSON_AddStringToObject(Ack_Json, "childId","");
    cJSON_AddNumberToObject(Ack_Json, "st", Status);

    Ack_Json_Data = cJSON_PrintUnformatted(Ack_Json);
    
    // Sending ACk of command with Json(String),
    // msg Type(int) and Current Time(String)  
    SendAck(Ack_Json_Data, Get_Time(), mt);
    cJSON_Delete(Ack_Json);
 }
	
char *key = "twin01", *value = NULL;
void Twin_CallBack(char *topic, char *payload) {      
    printk("\n Twin_msg >>  %s",payload);       
    
    if(! strncmp(topic,"$iothub/twin/PATCH/properties/",30)){   
        cJSON *root = cJSON_Parse(payload);        
        cJSON *P = cJSON_GetObjectItemCaseSensitive(root, "desired");
        value = (cJSON_GetObjectItem(P, key))->valuestring;
        UpdateTwin(key,value);
        }
    
    else{
    
    }
}

ESP8266/ESP32 Board

client.InitializationSDK(cpId, uniqueId, CallBack, TwinCallBack, ENV); 

CPID – Get your CPID
UNIQUEID  – Your Device ID.
CALLBACK – It is a callback function. You can receive all the messages and commands.
TWINCALLBACK – It is a callback function. You can receive twin messages.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

void CallBack(String &topic, String &payload) {        
    String cmd_ackID="";   String Cmd_value="";
    int Status = 0,mt=0;
    Serial.println("Cmd_msg >>  " + payload);    
    StaticJsonDocument<512> in_cmd;   
    deserializeJson(in_cmd, payload);
    cmd_ackID = in_cmd["ackId"].as();  
    Cmd_value = in_cmd["cmdType"].as();
    if(Cmd_value == "0x01" ){Status = 6; mt = 5;}
    else {Status = 7; mt = 11;}     
    StaticJsonDocument<400> Ack_Json; 
        Ack_Json["ackId"] = cmd_ackID;                                 
        Ack_Json["st"] = Status;
        Ack_Json["msg"] = "";
        Ack_Json["childId"] = "";
    String Ack_Json_Data;
    serializeJson(Ack_Json, Ack_Json_Data);
    
    // Sending ACk of command with Json(String), 
    // msg Type(int) and Current Time(String)  
    client.SendAck(Ack_Json_Data, GetTime(), mt);
    in_cmd.clear();    Ack_Json.clear();  
 }

void TwinCallBack(String &topic, String &payload) {          
    Serial.println("Twin_msg >>  " +topic + " " + payload);       
    //TO update the twinproperty                              
     client.UpdateTwin(key,value);
}

iOS

Objective-C

#import <IoTConnect/IoTConnectSDK.h>
IoTConnectSDK *objectIoTSDK;
NSString *uniqueId=@"sdk001";
NSString *cpId=@"abcde1";
NSString *strEnv=@"<<ENV>>”;
objectIoTSDK =[[IoTConnectSDK alloc] init: cpId : 
                  uniqueId : strEnv :^(id message) {
    /* Handle Cloud to Device message */
}];

CPID – Get your CPID
UNIQUEID  – you get from IoT hub while creating templates.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

Swift

#import <IoTConnect/IoTConnectSDK.h>
var uniqueId="sdk001" var cpId="abcde1"
var strEnv="<<ENV>>” 
objectIoTConnect = IoTConnectSDK(cpId, uniqueId, strEnv, { (message) in
        /* Handle Cloud to Device message */
})

CPID – Get your CPID
UNIQUEID  – Your Device ID.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

Android

IoTConnectSDK ioTConnectSDK = new IoTConnectSDK(context
                       ,cpid,uniqueId, callerClassObject,env);

CPID – Get your CPID
UNIQUEID  – Your Device ID.
CALLERCLASSOBJECT – It is a callback function. You can receive all the messages and commands.
ENV – Optional param (Default value is PROD),Get your Environment(ENV) Details

public void onReceiveMsg( HubToSdkDataBean cloudResponse) {
         if(cloudResponse!=null) {
                System.out.println("ACK : " + cloudResponse.getAck());
                System.out.println("ACK ID : " + cloudResponse.getAckid());
                System.out.println("Command : " + cloudResponse.getCommand());
                System.out.println("UniqueId : " + cloudResponse.getUniqueid());
                System.out.println("Value : " + cloudResponse.getValue());
        }
}

Powered by Softweb – An Avnet Company.

Copyright ©2022 Avnet, Inc. All rights reserved.

Human Rights Privacy Terms of Use