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