Skip to main content

Connecting a device using CoAP

This end-to-end guide demonstrates how to perform the following operations:

  • Build a sample with CoAP support.
  • Prepare the device to connect to nRF Cloud over CoAP.
  • Onboard the device.
  • Confirm that the device is connected and sending data to nRF Cloud over CoAP. This guide uses a sample that also supports Location Services.

Requirements

note

The command examples in this guide are valid for Windows. Your command-line interface may vary if you use another operating system.

Prerequisites

Building the nRF Cloud CoAP Device message sample

Build the CoAP Device message sample using nRF Connect for VS Code. This sample allows you to use AT commands when you inject credentials to the device in a later step, as well as demonstrate connectivity to nRF Cloud over CoAP.

For the steps in this section, refer to How to build an application and include the following changes:

  1. Create a new application for the CoAP Device message sample and the nRF91 DK target you are using in nRF Connect for VS Code:

    1. Under Kconfig fragments, select the overlay_coap.conf overlay.

      note

      The nRF Cloud library allows you to configure supported samples to use either a UUID or IMEI. The default is UUID. See Configuration options for device ID for more information.

  2. Build and flash the sample.

Installing device credentials and onboarding

This step depends on whether you are using the device for the first time or have already onboarded it.

For nRF91x1 devices, see the auto-onboarding option. The nRF Cloud CoAP Device Message sample does not comes with Provisioning service enabled, you would need to include this configuration in case you would like to test this feature. For devices that are not compatible with the nRF Cloud Security Service, proceed with the steps that follow.

Before you can onboard the device and connect to nRF Cloud, you need to inject credentials to the device, including the CoAP CA certificate. If you have onboarded the device already and added it to your team, you still need to inject the CoAP CA certificate.

If you have never connected your device to nRF Cloud before, you must create a self-signed CA certificate, as well as inject the AWS and CoAP CA certificates. Complete the following steps:

  1. Open a command prompt (or other command-line interface according to your operating system).

  2. If your device is not already switched on and connected over USB, do this now.

  3. Create a self-signed CA certificate using the create_ca_cert script:

    create_ca_cert
  4. Run the device_credentials_installer.py script with the --coap argument. Including this argument passes the necessary AWS CA and CoAP CA certificates to the device so you do not need to update them manually. Check the Utils README.md for more information on the required and optional arguments:

    device_credentials_installer -d --ca *_ca.pem --ca-key *_prv.pem --verify --coap

    This generates a CSV file for onboarding.

  5. Upload the CSV file using the nrf_cloud_onboard.py script:

    Your nRF Cloud REST API key is a required parameter and can be found on your User Account page.

    nrf_cloud_onboard --api-key $API_KEY --csv onboard.csv

    The service returns a bulk operation ID, which the script uses to fetch the operation results.

You can now confirm that the device is connected and sending information to nRF Cloud from the device's page in the nRF Cloud portal. If the device is not sending messages to nRF Cloud, press the RESET (SW5) button to reboot the device.

note

The connection may return a failure on the first try. If this happens, allow the device to attempt connecting again before you begin troubleshooting.