Sample Code for lettest version of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 04/20/2020 | Symmetric Key Sample Nodejs v2.3.0 | Download |
2 | 04/20/2020 | Symmetric Key Gateway Sample Nodejs v2.3.0 | Download |
3 | 04/20/2020 | Self Sign Sample Nodejs v2.3.0 | Download |
4 | 04/20/2020 | Self Sign Gateway Sample Nodejs v2.3.0 | Download |
5 | 04/20/2020 | CA Sign Sample Nodejs v2.3.0 | Download |
6 | 04/20/2020 | CA Sign Gateway Sample Nodejs v2.3.0 | Download |
How to build NodeJS sample code?
Prerequisite tools:
- NodeJs : Node.js supported version v8.x to v12.x (Download)
- Npm : NPM is compatible to the node version.
Installation
-
- Extract the “iotconnect–sdk-node-v2.3.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-v2.3.0/
- npm install (Install prerequisite nodejs library)
- npm install iotconnect-sdk (Install the ‘iotconnect-sdk’ 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)
- 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:
- If your device is using certificate base authentication then you have to provide bellow information
else you can keep it blank. Make sure each file path are valid and accessible:- certificate.SSLKeyPath : Device key file path
- certificate.SSLCertPath: Device certificate file path
- certificate.SSLCaPath : Root server CA certificate file path
- In case of unavailable of network connection, It will store offline data in offlineData.json file
which will be created on root location
Public Method and Implementations
Declare Iot Connect SDK package
var sdk = require('iotconnect-sdk');
Prerequisite input data
var uniqueId = <<uniqueId>>; var cpid = <<CPID>>; var env = <<env>>;
SDK Configuration
var sdkOptions = { "certificate" : { "SSLKeyPath" : "<< file path >>/key.pem", "SSLCertPath" : "<< file path >>/cert.pem", "SSLCaPath" : "<< file path >>/ca.cert.pem" } }
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 sdk(cpid, uniqueId, 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; var time = new Date(); if(data.ackId != null) iotConnectSDK.sendAck(obj, time, mt) } else if(data.cmdType == '0x02') { // Firmware OTA command var obj = { "ackId": data.ackId, "st": 7, "msg": "", "childId": "" } var mt = 11; var time = new Date(); if(data.ackId != null) iotConnectSDK.sendAck(obj, time, mt) } }
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); });
To send the data from Device To Cloud(D2C)
var data = [{ "uniqueId": "123456", "time" : '2019-12-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": "" } var time = new Date(); - Message Type mt = 5; // for "0x01" Device command mt = 11; // for "0x02" Firmware command iotConnectSDK.sendAck(obj, time, mt)ll
Troubleshooting
BaseUrl not found
Message: { status: false, data: null, message: ‘Invalid URI “sync”‘ }
Reason: CPID and ENV not proper.
Solution:
– Make sure you CPID, uniqueID and ENV are proper on the firmware file if it’s proper then make sure
internet connect Lost.CPID not found
Message: RC CODE : 6 (CPID_NOT_FOUND)
Reason: The wrong CPID entered
Solution:
– CPID should be valid. You can take it from here (How to get CPID)
– Check the combination of CPID and ENV (IoTConnect platform environment)Device not found
Message: RC CODE : 3 (DEVICE_NOT_FOUND)
Reason: Device not registered on IoTConnect cloud
Solution:
– It should be valid and respective to the entered CPID and ENV
– The device should be registered with entered uniqueId on IoTConnect cloud respective to the selected CPID
and ENV
– If your device name is wrong then you need to stop the script and run again after uniqueId correction.Device not registered
Message: RC CODE : 1 (DEVICE_NOT_REGISTERED)
Reason: Device not registered on cloud
Solution:
– If your uniqueId is right and till you getting the above error then verify below process
– It should be registered on IoTConnect Platform respective to the selected CPID and ENV
– Its status should be “Acquired” (registered
on the cloud)
– SDK process is running and rechecking till you follow the above two-point then your device will get
connected.A network connection is not available
Message: { status: false, data: [], message: ‘Network connection error.’ }
Reason: Network connection not available.
Solution: Check the network connection and try again.X.509 authentication device issue (CA signed or self signed)
Message: “SDK options:: Set proper certificate file path and try again.”
Reason:
– CA or Self signed certificate not defined properly
Solution:
– Add your device authenticate certificate in sdkOptions in device initialize method.
– Check certificate path properLogfile create an issue
Message: “<< Exception error >>”
Reason: In proper file path and permission
Solution:
– Give a proper read and write permission to the device folder
– Check log file pathSend data before device connect or after disconnect
Message: “<< cpid_uniqueid >> :: sendData :: The device is not available on the cloud.”
Reason: Device to cloud connection not available
Solution: Check the device to the cloud connection. - Extract the “iotconnect–sdk-node-v2.3.0.zip“ (You can download SDK