Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save coderbyheart/932fc06e044aaf68af3cc87ee52057e1 to your computer and use it in GitHub Desktop.
Save coderbyheart/932fc06e044aaf68af3cc87ee52057e1 to your computer and use it in GitHub Desktop.
How to provision a nRF9160 DK with custom cloud credentials

How to provision a nRF9160 DK with custom cloud credentials using nRF Connect SDKs Asset Tracker sample.

In this example we want to provision the nRF9160 Development Kit with custom credentials to connect to an MQTT broker. For this guide we still want to connect it to nRF Connect for Cloud but as a new device.

Aquire a new certificate to connect to nRF Connect for Cloud

There is a section on connecting via MQTT to nRF Connect for Cloud in the handbook, but here are necessary steps in short:

Open the handbook and log in to with your nRF Connect for Cloud credentials. If you do not have an account, create a new one. (Note that you cannot use your DevZone account at the moment)

Note down your API key.

Register a new device by calling sending this POST request

http POST https://api.nrfcloud.com/v1/devices \
  Authorization:"Bearer <your API key>"

(Note: we use httpie in this example.)
This is an asynchronous operation so it will only return HTTP/1.1 202 Accepted.

List your devices by sending this GET request http https://api.nrfcloud.com/v1/devices Authorization:"Bearer <your API key>". Look for a device with the "type": "Generic", e.g.:

{  
  "$meta": {  
    "createdAt": "2019-01-30T11:59:46.882Z",  
    "version": "1.0"  
  },  
  "id": "8b20a06f-71f9-4002-bf52-619354f417fb",  
  "name": "8b20a06f-71f9-4002-bf52-619354f417fb",  
  "tags": [],  
  "type": "Generic"  
}

Note down the id of the device (e.g. 8b20a06f-71f9-4002-bf52-619354f417fb)

Now create a new certificate for this device by sending this POST request:

http POST https://api.nrfcloud.com/v1/devices/8b20a06f-71f9-4002-bf52-619354f417fb/certificates \
  Authorization:"Bearer <your API key>"

This will return the certificate and the private and public key pair for the device.

HTTP/1.1 201 Created

{
    "certificate": "-----BEGIN CERTIFICATE-----\nMIIDWTCCAk...RphzZRkj+/d7\n-----END CERTIFICATE-----\n",
    "clientId": "8b20a06f-71f9-4002-bf52-619354f417fb",
    "id": "arn:aws:iot:us-east-1:680502709288:cert/407bd802e629b94a8d1676354b779e781e406afa0233aaf12700cf1daa7b7ba7",
    "privateKey": "-----BEGIN RSA PRIVATE KEY-----\nMIIEow...kbppa4\n-----END RSA PRIVATE KEY-----\n",
    "publicKey": "-----BEGIN PUBLIC KEY-----\nMIIBIjANB...QIDAQAB\n-----END PUBLIC KEY-----\n"
}

Compile and run the Asset tracker sample application with custom certificates

Set up the nRF Connect SDK by following this getting started guide.

Copy and format the certificates into nrf/samples/nrf9160/asset_tracker/src/certificates.h header file.

Configure the nRF Cloud library to provision the certificates by adding the following configurations to nrf/samples/nrf9160/asset_tracker/prj.conf

# nRF Cloud
CONFIG_NRF_CLOUD=y
CONFIG_NRF_CLOUD_PROVISION_CERTIFICATES=y
CONFIG_NRF_CLOUD_CERTIFICATES_FILE="certificates.h"
CONFIG_NRF_CLOUD_SEC_TAG=1

Compile, flash and run the Asset tracker sample according to nrf/samples/nrf9160/asset_tracker/README.rst

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment