Cellular: nRF Cloud REST Device Message

Note

The nRF Cloud REST library has been deprecated and it will be removed in one of the future releases. Use the Cellular: nRF Cloud CoAP device message sample instead.

The REST Device Message sample demonstrates how to use the nRF Cloud REST API to send Device Messages using the SendDeviceMessage REST endpoint. Every button press sends a message to nRF Cloud.

The sample uses the nRF Cloud Log library. When the device first comes online, it sends a log message that indicates the sample version. When a button is pressed, the device sends the same log message to nRF Cloud indicating the sample version.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

Thingy:91 X

PCA20065

thingy91x

thingy91x/nrf9151/ns

nRF9161 DK

PCA10153

nrf9161dk

nrf9161dk/nrf9161/ns

nRF9160 DK

PCA10090

nrf9160dk

nrf9160dk/nrf9160/ns

nRF9151 DK

PCA10171

nrf9151dk

nrf9151dk/nrf9151/ns

For more security, it is recommended to use the */ns variant of the board target. When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

The sample requires an nRF Cloud account.

Your device must be onboarded with nRF Cloud. If it is not, follow the instructions in Device on-boarding.

Note

This sample requires modem firmware v1.3.x or later for nRF9160 and v2.0.0 or later for nRF91x1.

Limitations

The nRF Cloud REST API requires all requests to be authenticated with a JSON Web Token (JWT). See nRF Cloud Security for more details. Generating valid JWTs requires the network carrier to provide date and time to the modem, so the sample must first connect to an LTE carrier and determine the current date and time before REST requests can be sent.

Note also that the nRF Cloud REST API is stateless. This differs from the nRF Cloud MQTT API, which requires you to establish and maintain an MQTT connection while sending Device Messages.

User interface

Once the device is onboarded and connected, each press of Button 1 generates a device-to-cloud button press Device Message over REST. These messages are sent to the non-bulk D2C (device-to-cloud) topic, detailed in topics used by devices running the nRF Cloud library.

The configured LTE LED (LED 1 by default) (CONFIG_REST_DEVICE_MESSAGE_LTE_LED_NUM) is lit once an LTE connection is established and JWT tokens are ready to be generated.

The configured Send LED (LED 2 by default) (CONFIG_REST_DEVICE_MESSAGE_SEND_LED_NUM) is lit when a REST request is being sent.

Press Button 1 to have a message sent to nRF Cloud:

{
  "appId": "BUTTON",
  "messageType": "DATA",
  "data": "1",
  "timestamp": "4/19/2023 12:34:21 PM"
}

Onboarding your device to nRF Cloud

You must onboard your device to nRF Cloud for this sample to function. You only need to do this once for each device.

To onboard your device, install nRF Cloud Utils_ and follow the instructions in the README.

Configuration

See Configuring and building for information about how to permanently or temporarily change the configuration.

Configuration options

Set the following configuration options for the sample:

CONFIG_REST_DEVICE_MESSAGE_LTE_LED_NUM - LTE LED number

This configuration option defines which LED is used to indicate LTE connection success.

CONFIG_REST_DEVICE_MESSAGE_SEND_LED_NUM - Send LED number

This configuration option defines which LED is used to indicate a REST request is being sent.

Sending traces over UART on an nRF91 Series DK

To send modem traces over UART on an nRF91 Series DK, configuration must be added for the UART device in the devicetree and Kconfig. This is done by adding the modem trace UART snippet when building and programming.

Use the Cellular Monitor app for capturing and analyzing modem traces.

TF-M logging must use the same UART as the application. For more details, see shared TF-M logging.

Building and running

This sample can be found under samples/cellular/nrf_cloud_rest_device_message in the nRF Connect SDK folder structure.

For more security, it is recommended to use the */ns variant of the board target (see the Requirements section above.) When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Note

When building repository applications in the SDK repositories, building with sysbuild is enabled by default. If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.

The configuration file for this sample is located in samples/cellular/nrf_cloud_rest_device_message. See Configuring and building for information on how to configure the parameters.

nRF Cloud logging overlay

To enable Zephyr Logging to nRF Cloud using the nRF Cloud Log library, add the following parameter to your build command:

-DEXTRA_CONF_FILE=overlay_nrfcloud_logging.conf

This overlay allows the sample and various subsystems that have logging enabled to send their logs to nRF Cloud. Set the CONFIG_NRF_CLOUD_LOG_OUTPUT_LEVEL option to the log level of messages to send to nRF Cloud, such as 4 for debug log messages. Set the CONFIG_NRF_CLOUD_REST_DEVICE_MESSAGE_SAMPLE_LOG_LEVEL_DBG option so that log messages are generated on each button press.

Querying Device Messages over REST API

To query the Device Messages received by the nRF Cloud backend, send a GET request to the ListMessages endpoint.

Dependencies

This sample uses the following nRF Connect SDK libraries: