Sample Code for lettest version of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 12/08/2020 | TPM device Sample v3.1(preview) | Download |
How to build NodeJS sample code?
Prerequisite tools:
- NodeJs : Node.js supported version v10.x and above (Download)
- Npm : NPM is compatible to the node version.
- TPM authorized device to authenticate the device.
- npm install node-gyp -g
- npm install ffi -g
Note : There are some known issues to installing the above packages for Windows OS. To resolve these issues, run npm install –global –production windows-build-tools using a command prompt in Run as administrator mode, run SET VCTargetsPath=C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140 after replacing the path with your installed version, and then rerun the above installation commands.
Installation
- Extract the “iotconnect-tpm-sdk-node-v3.1.0.zip“ (You can download SDK
file from here) - To install the required libraries use the below command:
- Go to SDK directory path using terminal/Command prompt
- cd iotconnect-sdk-node-v3.1.0/
- npm install (Install prerequisite nodejs library)
- npm install iotconnect-sdk-tpm (Install the ‘iotconnect-sdk-tpm’ package in nodejs library)
- Using terminal/command prompt goto sample folder
- cd sample
- You can take the firmware file from the above location and update the following details
- Prerequisite input data as explained in the usage section as below
- Update sensor attributes according to added in iotconnect cloud platform
- If your device is secure then need to configure the x.509 certificate path as like sdkOptions
given below otherwise leave as it is.
- Run Sample File:
- node YOURSAMPLEFILE.js (dowload from above list)
Public Method and Implementations
Declare Iot Connect SDK package
var SDKClient = require('iotconnect-sdk-tpm');
Prerequisite input data
var uniqueId = <<uniqueId>>; var cpId = <<CPID>>; // It need to get from the IoTConnect platform "Settings->Key Vault". var scopeId = <<Azure DPS ScopeId>>; // It need to get from the IoTConnect platform "Settings->Key Vault". var env = <<env>>;
SDK Configuration
- If your device has an authentication type “Key” then no need any configuration. SDK will manage it
self from device information. - To configure the secure SSL/x509 connection follow below step for CA or SelfSiged certificate
- “sdkOptions” is optional. Mandatory for SSL/x509 device authentication type only.
- Standard file format for the “sdkOptions” is as below:
Note: sdkOptions is optional. Define setting or leave it blank. It will set the default setting for offline storage configuration as per defined above. It may harm your device by storing the large data. Once memory get full may chance to your device script crash and stop the execution.
var sdkOptions = { "offlineStorage": { "disabled": false, //default value = false, false = store data, true = not store data "availSpaceInMb": 1, //size in MB, Default value = unlimted "fileCount": 5 // Default value = 1 } }
- “offlineStorage” : Define the configuration related to the offline data storage in case of unavailable of network connection.
- disabled : //default value = false, false = store data, true = not store data
- availSpaceInMb: Define the file size of offline data which should be in (MB). Default value is infinite
- fileCount : It manages the number of files will be created while storing the offline data. Default value is 1
NOTE: It will cause an issue if property name got changed so please make sure none of the property get renamed. To avoid the file permission issue, start the script with the admin user.
To get the device information and connect to the device
var iotConnectSDK = new SDKClient(cpId, uniqueId, scopeId, callbackMessage, twinCallbackMessage, env, sdkOptions);
To receive the command from Cloud to Device(C2D)
var callbackMessage = function callbackMessage(data){ if(data.cmdType == '0x01') { // Device command var obj = { "ackId": data.ackId, "st": 6, "msg": "", "childId": "" } var mt = 5; if(data.ackId != null) iotConnectSDK.sendAck(obj, mt) } else if(data.cmdType == '0x02') { // Firmware OTA command var obj = { "ackId": data.ackId, "st": 7, "msg": "", "childId": "" } var mt = 11; if(data.ackId != null) iotConnectSDK.sendAck(obj, mt) } else if(data.cmdType == '0x16') { // Firmware OTA command if(data.command) { console.log("Device Connected"); } else { console.log("Device Disconnected"); } } }
To receive the twin from Cloud to Device(C2D)
var twinCallbackMessage = function twinCallbackMessage(data){ console.log(data); }
To get the list of attributes
iotConnectSDK.getAttributes(function(response){ console.log("Attributed :: "+ response); });
Note: you can checkout device attribute format with all device type click here
To send the data from Device To Cloud(D2C)
var data = [{ "uniqueId": "123456", "time" : '2020-11-24T10:06:17.857Z', //Date format should be as defined "data": { "temperature": 15.55, "humidity" : 27.97, "weight" : 36, "gyroscope" : { 'x' : -1.2, 'y' : 0.25, 'z' : 1.1, } } }]; iotConnectSDK.sendData(data);
To update the Twin Property
var key = "<<Desired property key >>"; // Desired proeprty key received from Twin callback message var value = "<< Desired Property value >>"; // Value of respective desired property iotConnectSDK.UpdateTwin(key,value)
To send the acknowledgment to cloud for Device command and Firmware OTA command
var obj = { "ackId": command.ackId, //Command acknowledgement ID received with command "st": << value >>, // 6 (Device command), 7 (Firmware command) "msg": "", "childId": "" } - Message Type mt = 5; // for "0x01" Device command mt = 11; // for "0x02" Firmware command iotConnectSDK.sendAck(obj, mt)
To disconnect the device from the cloud
iotConnectSDK.dispose()
To get the all twin property Desired and Reported
iotConnectSDK.getAllTwins();