nRF9160 Board – Sample Code of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 04/16/2021 | Nordic nRF9160 SDK 3.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
- Install the SEGGER Embedded Studio with 4.50 and nRF connect 3.6.0 version for edit and run the application
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-sdk-c-nRF9160-v3.0.zip” SDK which you can download from our IoTConnect help portal.
- we have “IoTConnect_config.h” file in nRF9160-DK\src with “main.c”(You can download this firmware file from above sample code).
- You need to input “uniqueId”, “CPID” and “env” in IoTConnect_config.h file. You can see more in below section title with “Prerequisite input data”
- You need to input “uniqueId”, “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"
Usage:
To initialize the SDK object need to import below sdk package
#include "IoTConnect_Config.h" #include "main.h"
Prerequisite input data
#define IOTCONNECT_DEVICE_CP_ID "IoTConnectENV"; #define IOTCONNECT_DEVICE_UNIQUE_ID "yourCpid"; #define IOTCONNECT_DEVICE_ENV "yourUniqueID";
- IOTCONNECT_DEVICE_UNIQUE_ID : Its device ID which register on IotConnect platform and also its status has Active and Acquired.
- IOTCONNECT_DEVICE_CP_ID : It is the company code. It gets from the IoTConnect UI portal “Settings->Key Vault”.
- IOTCONNECT_DEVICE_ENV : It is the UI platform environment. It gets from the IoTConnect UI portal “Settings->Key Vault”.
SdkOptions is for the SDK configuration and needs to parse in SDK object initialize call. You need to manage the below configuration as per your device authentication type.
cJSON *SDKoption; SDKoption = cJSON_CreateObject(); char *SDK_option = cJSON_PrintUnformatted(SDKoption);
Note: sdkOptions is optional. For now not require any configuration to set in sdkOptions so set as NULL.
To Initialize the SDK object and connect to the cloud
IoTConnect_init(IOTCONNECT_DEVICE_CP_ID, IOTCONNECT_DEVICE_UNIQUE_ID, DeviceCallback, TwinUpdateCallback, SDK_option, IOTCONNECT_DEVICE_ENV);
To Connect mqtt client with IoTConnect
IoTConnect_connect();
To receive the command from Cloud to Device(C2D)
void DeviceCallback(char *payload){ printk("\n Cmd_msg >> %s",payload); if(payload.cmdType == "0x01") // Device Command if(payload.cmdType == "0x02") // Firmware Command if(payload.cmdType == "0x16") // Device connection status }
This is the standard data input format for Gateway and non Gateway device to send the data on IoTConnect cloud(D2C)
// Example For Non Gateway Device String Attribute_json_Data = [{ "uniqueId": "<< Device UniqueId >>", "time" : "<< date >>", "data": {} }]; SendData(Attribute_json_Data);
- time : Date format should be as defined # “2021-01-24T10:06:17.857Z”
- data : JSON data type format # {“temperature”: 15.55, “gyroscope” : { ‘x’ : -1.2 }}
To send the command acknowledgment from device to cloud.
Ack_Json=cJSON_CreateObject(); cJSON_AddStringToObject(Ack_Json, "ackId", cmd_ackID); cJSON_AddStringToObject(Ack_Json, "msg", ""); cJSON_AddStringToObject(Ack_Json, "childId", cmd_UNI); cJSON_AddNumberToObject(Ack_Json, "st", Status); char *Ack_Json_Data = cJSON_Print(Ack_Json); int msgType = ""; // 5 ("0x01" device command), 11 ("0x02" Firmware OTA command) SendAck(Ack_Json_Data, msgType);
- ackId(*) : Command Acknowledgment GUID which will receive from command payload
(data.ackId) - st(*) : Acknowledgment status sent to cloud
4 = Fail
6 = Device command[0x01]
7 = Firmware OTA command[0x02]) - msg : It is used to send your custom message
- childId : It is used for Gateway’s child device OTA update only
0x01 : null or “” for Device command
0x02 : null or “” for Gateway device and mandatory for Gateway child device’s OTA update.
How to get the “childId”?
– You will get child uniqueId for child device OTA command from payload “data.urls[~].uniqueId” - msgType : Message type
5 = Device command[0x01]
11 = Firmware OTA command[0x02]
To receive the twin from Cloud to Device(C2D)
void TwinUpdateCallback(char *payload){ printk("\n Twin_msg payload is >> %s", payload); }
To update the Twins reported property
char *key = "twin01", *value = "18"; UpdateTwin(key,value);
- key : Desired property key received from Twin callback message
- value : Value of the respective desired property
Get all the Twin property from the cloud to your device.
GetAllTwins();
To disconnect the device from IoTConnect cloud
IoTConnect_abort();