• 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

C Language SDK

Version 2.2.0

Release On 09/20

nRF9160 Board – Sample Code of SDK

# Date Name Download
1 11/09/2020 Nordic nRF9160 SDK 2.2.0 Download

How to run a C sample code nRF9160 Board?

Prerequisite

  • Install nRF9160 SDK with 1.2.0 version and complete Getting Started with nRF9160 DK. Reference
    link Click here

Installation

  • For making new application, create a folder in nrf-sample folder and name is “IoTConnect” where you installed
    nRFConnet SDK.

    • Path to create folder “…Nordic\ncs\nrf\samples\nrf9160\IoTConnect”
  • Now unzip the “iotconnect-C-sdk-nRF-2.2.0.zip” SDK which you can download from our IoTConnect help portal.
  • We have “main.c” and “main.h” file in “nRF9160-DK\src” with “IoTConnect_config.h”.
    • you need to input “uniqueIdID”, “CPID” and “env” in IoTConnect_config.h file. You can see more in below section title with “Prerequisite input data”
  • In the other folder “nRF9160-DK\IoTConnect\cert” we have a “certificate.h” in here you have to put your device certificate
#define CLOUD_CLIENT_PRIVATE_KEY \
"-----BEGIN RSA PRIVATE KEY-----\n"
----------------------------------
----------------------------------
"-----END RSA PRIVATE KEY-----\n"

#define CLOUD_CLIENT_PUBLIC_CERTIFICATE \
"-----BEGIN CERTIFICATE-----\n"
----------------------------------
----------------------------------
"-----END CERTIFICATE-----\n"

#define CLOUD_CA_CERTIFICATE \
"-----BEGIN CERTIFICATE-----\n" \
----------------------------------
----------------------------------
"-----END CERTIFICATE-----\n"

Ready to go

  • This script can send the data to given input(uniqueid, cpid)
  • Get your Environment(ENV) Details
  • Get your CPID

Usage:

Prerequisite input data

#define  IOTCONNECT_DEVICE_CP_ID        "IoTConnectENV"; // IoTConnect platform Environment
#define  IOTCONNECT_DEVICE_UNIQUE_ID    "yourCpid";  // your CPID name can grab it from IoTConnect portal
#define  IOTCONNECT_DEVICE_ENV          "yourUniqueID";  // your UniqueID name can grab it from IoTConnect portal

To get the device information and connect to the device

err = IoTConnect_init(IOTCONNECT_DEVICE_CP_ID, IOTCONNECT_DEVICE_UNIQUE_ID, IOTCONNECT_DEVICE_ENV, Device_CallBack, Twin_CallBack);
if (err) {
        printk("Failed to Init IoTConnect SDK");
        return ;
}

To receive the command from Cloud to Device(C2D)

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);
}

Data input format

char *Sensor_data(void){
    cJSON *Attribute_json = NULL;
    cJSON *Device_data1 = NULL;
    cJSON Data = NULL, Data1= NULL;

    Attribute_json = cJSON_CreateArray();
    if (Attribute_json == NULL){
        printk("Unable to allocate Attribute_json Object\n");
        return ;    
      }
      cJSON_AddItemToArray(Attribute_json, Device_data1 = cJSON_CreateObject());
      cJSON_AddStringToObject(Device_data1, "uniqueId",IOTCONNECT_DEVICE_UNIQUE_ID);
      cJSON_AddStringToObject(Device_data1, "time", Get_Time());
      cJSON_AddItemToObject(Device_data1, "data", Data = cJSON_CreateObject());
      cJSON_AddStringToObject(Data,"Humidity", "Black" );
      cJSON_AddNumberToObject(Data, "Temperature",  18);
      cJSON_AddItemToObject(Data, "Gyroscope", Data1 = cJSON_CreateObject());
      cJSON_AddNumberToObject(Data1, "x",  128);
      cJSON_AddStringToObject(Data1, "y", "Black" );
      cJSON_AddNumberToObject(Data1, "z",  318);
      
      char *msg = cJSON_PrintUnformatted(Attribute_json);
      cJSON_Delete(Attribute_json);
      return  msg;
}

To send the data from Device To Cloud(D2C)

SendData(Attribute_json_Data);

To receive the twin from Cloud to Device(C2D)

void Twin_CallBack(char topic, char payload) {      
    printk("\n Twin_msg payload is >>  %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{
        printk("\n Twin_msg on topic >> %s and payload is >>  %s", topic, payload);
    
    }
}

Get all the Twin property from the cloud to your device.

getAllTwins()

Disconnects IoTConnect SDK using Dispose Method.

err = IoTConnect_abort();
if (err) {
    printk("Failed to Abord IoTConnect SDK");
    return ;
}

Powered by Softweb – An Avnet Company.

Copyright ©2022 Avnet, Inc. All rights reserved.

Human Rights Privacy Terms of Use