• 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

Standard 2.0

Card Image

Node.js Standard Authorized SDK

Version 2.3.0

Updated On 04/20

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:

  1. NodeJs : Node.js supported version v8.x to v12.x (Download)
  2. Npm : NPM is compatible to the node version.

Installation 

    1. Extract the “iotconnect–sdk-node-v2.3.0.zip“ (You can download SDK
      file from here)
    2. 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)
    3. Using terminal/command prompt goto sample folder
      • cd sample
    4. 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.
    5. Run Sample File:
      • node YOURSAMPLEFILE.js (dowload from above list)
    6. Public Method and Implementations

      Declare Iot Connect SDK package

      var sdk = require('iotconnect-sdk');
      

      Prerequisite input data

      • Get your Environment(ENV) Details
      • Get your CPID
      var uniqueId = <<uniqueId>>; 
      var cpid = <<CPID>>; 
      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:
      var sdkOptions = {
      	"certificate" : { 
      		"SSLKeyPath"    : "<< file path >>/key.pem",
      		"SSLCertPath"   : "<< file path >>/cert.pem",
      		"SSLCaPath"     : "<< file path >>/ca.cert.pem"
      	}
      }
      • 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

      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 proper

      Logfile 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 path

      Send 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.

Powered by Softweb – An Avnet Company.

Copyright ©2022 Avnet, Inc. All rights reserved.

Human Rights Privacy Terms of Use