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
-
nRF91 device.
-
nRF Connect SDK v2.5.0 or later.
-
nRF Connect for Desktop, and the latest versions of the following apps:
- Serial Terminal for AT commands.
- Programmer to update the modem firmware.
-
Modem firmware:
- For nRF9151: Modem firmware v2.0.0 or later.
- For nRF9160: Modem firmware v1.3.5 or later.
-
VS Code with the nRF Connect for VS Code extension.
-
nRF Cloud CoAP Device message sample. This sample includes CoAP support, as well as AT commands, which you need for certificate and credential management. See Building the nRF Cloud multi-service sample.
The command examples in this guide are valid for Windows. Your command-line interface may vary if you use another operating system.
Prerequisites
- Create an nRF Cloud account.
- Install nRF Cloud Utils, you can find the instruction in the README section.
- Update the modem firmware using the Programmer app if your device does not meet the modem firmware requirement.
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:
-
Create a new application for the CoAP Device message sample and the nRF91 DK target you are using in nRF Connect for VS Code:
-
Under Kconfig fragments, select the
overlay_coap.confoverlay.noteThe 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.
-
-
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.
- Onboarding the device for the first time
- The device is already onboarded
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:
-
Open a command prompt (or other command-line interface according to your operating system).
-
If your device is not already switched on and connected over USB, do this now.
-
Create a self-signed CA certificate using the
create_ca_certscript:create_ca_cert -
Run the
device_credentials_installer.pyscript with the--coapargument. 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 UtilsREADME.mdfor more information on the required and optional arguments:device_credentials_installer -d --ca *_ca.pem --ca-key *_prv.pem --verify --coapThis generates a CSV file for onboarding.
-
Upload the CSV file using the
nrf_cloud_onboard.pyscript: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.csvThe service returns a bulk operation ID, which the script uses to fetch the operation results.
You might need to update the credentials on your already-onboarded device to use CoAP. See Managing device credentials.
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.
The connection may return a failure on the first try. If this happens, allow the device to attempt connecting again before you begin troubleshooting.