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
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" } }
//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"); } } }
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):
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; }
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); }
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
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()); } }