Skip to content

nRF Cloud CoAP Device Message

Overview

The nRF Cloud CoAP Device Message sample demonstrates how to use the nRF Cloud CoAP API to send device message.

After the sample initializes and connects to the network, it sends a device message to nRF Cloud. Every button press also sends a message to nRF Cloud.

Requirements

Before you start, check that you have the required hardware and software:

  • 1x nRF9151 Connect Kit
  • 1x nano-SIM card with LTE-M or NB-IoT support
  • 1x U.FL cabled LTE-M/NB-IoT/NR+ Flexible Antenna (included in the box)
  • 1x USB-C Cable
  • A computer running macOS, Ubuntu, or Windows 10 or newer

Don't have an nRF Cloud account?

To connect your device and use nRF Cloud services, you must create an nRF Cloud account:

  1. Go to the nRF Cloud portal and click Register.
  2. Enter your email address and choose a password.
  3. Click Create Account.
  4. Check for a verification email from nRF Cloud.

    Tip

    If you do not see the verification email, check your junk mail folder for an email from no-reply@verificationemail.com.

  5. Copy the six-digit verification code and paste it into the registration dialog box.

    Tip

    If you accidentally closed the registration dialog box, repeat Step 1 and click Already have a code?. Enter your email address and verification code.

You can now log in to the nRF Cloud portal with your email and password. After logging in, you can see the Dashboard view that displays your device count and service usage.

Device on-boarding with nRF Cloud

Your device must be onboarded with nRF Cloud. If it is not, follow the nRF Cloud Device Provisioning to provision your device.

Set up your board

  1. Insert the nano-SIM card into the nano-SIM card slot.
  2. Attach the U.FL cabled LTE-M/NB-IoT/NR+ Flexible Antenna.
  3. Connect the nRF9151 Connect Kit to the computer with a USB-C cable.

Building the sample

To build the sample, follow the instructions in Getting Started Guide to set up your preferred building environment.

Use the following steps to build the nRF Cloud CoAP Device Message sample on the command line.

  1. Open a terminal window.

  2. Go to NCS-Project/nrf9151-connectkit repository cloned in the Getting Started Guide.

  3. Build the sample using the west build command, specifying the board (following the -b option) as nrf9151_connectkit/nrf9151/ns.

    west build -p always -b nrf9151_connectkit/nrf9151/ns samples/nrf_cloud_coap_device_message
    

    The -p always option forces a pristine build, and is recommended for new users. Users may also use the -p auto option, which will use heuristics to determine if a pristine build is required, such as when building another sample.

    Note

    This sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M).

  4. After building the sample successfully, the firmware with the name tfm_merged.hex can be found in the build/nrf_cloud_coap_device_message/zephyr directory.

Flashing the firmware

Set up your board before flashing the firmware. You can flash the sample using west flash:

west flash

Tip

In case you wonder, the west flash will execute the following command:

pyocd load --target nrf91 --frequency 4000000 build/nrf_cloud_coap_device_message/zephyr/tfm_merged.hex

Testing

After programming the sample, test it by performing the following steps:

  1. Open up a serial terminal, specifying the correct serial port that your computer uses to communicate with the nRF9151 SiP:

    1. Start PuTTY.
    2. Configure the correct serial port and click Open:

    Open up a terminal and run:

    screen <serial-port-name> 115200
    

    Open up a terminal and run:

    screen <serial-port-name> 115200
    
  2. Press the DFU/RST button to reset the nRF9151 SiP.

  3. Once the device is provisioned and connected, you should see the output, similar to what is shown in the following:

    Terminal
    [INF] All pins have been configured as non-secure
    [NOT] Booting TF-M v2.3.0**
    [NOT] Built Thu 18 Jun 2026 08:38:41 UTC
    [INF] Float ABI: Hard, Lazy stacking enabled
    *** Booting My Application v1.0.0-e67dc29a98a4 ***
    *** Using nRF Connect SDK v3.3.99-95ed8f7e7406 ***
    *** Using Zephyr OS v4.4.0-14033cef1f73 ***
    [00:00:00.392,669] <inf> nrf_cloud_coap_device_message: nRF Cloud CoAP Device Message Sample, version: 1.0.0
    [00:00:00.392,700] <inf> nrf_cloud_coap_device_message: Reset reason: 0x10001
    [00:00:00.922,210] <inf> nrf_cloud_credentials: Sec Tag: 16842753; CA: Yes, Client Cert: Yes, Private Key: Yes
    [00:00:00.922,271] <inf> nrf_cloud_credentials: CA Size: 1792, AWS: Likely, CoAP: Likely
    [00:00:00.922,271] <inf> nrf_cloud_coap_device_message: nRF Cloud credentials detected!
    [00:00:00.949,218] <inf> nrf_cloud_coap_device_message: Enabling connectivity...
    +CGEV: EXCE STATUS 0
    %MDMEV: SEARCH STATUS 1
    +CEREG: 2,"1D23","0D70394B",9
    %MDMEV: PRACH CE-LEVEL 0
    +CSCON: 1
    +CGEV: ME PDN ACT 0,0
    +CNEC_ESM: 50,0
    %MDMEV: SEARCH STATUS 2
    +CEREG: 1,"1D23","0D70394B",9,,,"11100000","11100000"
    [00:01:23.320,281] <inf> nrf_cloud_coap_device_message: Connected to LTE
    [00:01:23.320,434] <inf> nrf_cloud_coap_device_message: Waiting for modem to acquire network time...
    %XTIME: "23","62608180147123","00"
    [00:01:26.321,319] <inf> nrf_cloud_coap_device_message: Network time obtained
    [00:01:26.321,380] <inf> nrf_cloud_info: Device ID: 5034474b-3731-40ab-809f-152c5c11a9a5
    [00:01:26.321,990] <inf> nrf_cloud_info: IMEI:      359404230074347
    [00:01:26.425,872] <inf> nrf_cloud_info: UUID:      5034474b-3731-40ab-809f-152c5c11a9a5
    [00:01:26.426,330] <inf> nrf_cloud_info: Modem FW:  mfw_nrf91x1_2.0.4
    [00:01:26.426,361] <inf> nrf_cloud_info: Protocol:          CoAP
    [00:01:26.426,391] <inf> nrf_cloud_info: Download protocol: HTTPS
    [00:01:26.426,391] <inf> nrf_cloud_info: Sec tag:           16842753
    [00:01:26.426,422] <inf> nrf_cloud_info: CoAP JWT Sec tag:  16842753
    [00:01:26.426,452] <inf> nrf_cloud_info: Host name:         coap.nrfcloud.com
    [00:01:30.046,234] <inf> nrf_cloud_coap_transport: Request authorization with JWT
    [00:01:30.557,189] <inf> nrf_cloud_coap_transport: Authorization result_code: 2.01
    [00:01:30.557,434] <inf> nrf_cloud_coap_transport: Authorized
    [00:01:30.557,800] <inf> nrf_cloud_coap_transport: DTLS CID is active
    [00:01:31.269,592] <inf> nrf_cloud_coap_device_message: Sending message:'{"sample_message":"Hello World, from the CoAP Device Message Sample! Message ID: 1781772084927"}'
    [00:01:31.628,387] <inf> nrf_cloud_coap_device_message: Message sent
    [00:01:31.628,387] <inf> nrf_cloud_coap_device_message: Sent Hello World message with ID: 1781772084927
    
  4. Press USR/P25 button to have a message sent to nRF Cloud:

    Terminal
    +CSCON: 0
    [00:03:32.793,945] <inf> nrf_cloud_coap_device_message: Sending message:'{"appId":"BUTTON", "messageType":"DATA", "data":"1"}'
    %MDMEV: PRACH CE-LEVEL 0
    +CSCON: 1
    [00:03:33.536,163] <inf> nrf_cloud_coap_device_message: Message sent
    [00:03:33.536,163] <inf> nrf_cloud_coap_device_message: Button pressed 1 times
    [00:03:35.517,547] <inf> nrf_cloud_coap_device_message: Sending message:'{"appId":"BUTTON", "messageType":"DATA", "data":"1"}'
    [00:03:36.402,252] <inf> nrf_cloud_coap_device_message: Message sent
    [00:03:36.402,282] <inf> nrf_cloud_coap_device_message: Button pressed 2 times
    [00:03:37.047,485] <inf> nrf_cloud_coap_device_message: Sending message:'{"appId":"BUTTON", "messageType":"DATA", "data":"1"}'
    [00:03:37.601,287] <inf> nrf_cloud_coap_device_message: Message sent
    [00:03:37.601,318] <inf> nrf_cloud_coap_device_message: Button pressed 3 times
    +CSCON: 0
    
  5. After the messages sent, verify that on the nRF Cloud:

    1. Select Device Management -> Devices.
    2. Click the ID of the device you added.
    3. On the device’s page, you should see the messages displayed on the Terminal.