Cellular: nRF Cloud MQTT device message
The nRF Cloud MQTT device message sample demonstrates how to use the nRF Cloud MQTT API to send device messages. Every button press sends a message to nRF Cloud.
Requirements
The sample supports the following development kits:
Hardware platforms |
PCA |
Board name |
|
|---|---|---|---|
PCA20065 |
|
||
PCA10153 |
|
||
PCA10090 |
|
||
PCA10171 |
|
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 an nRF9160 DK, or modem firmware v2.x.x for the nRF9161 and nRF9151 DKs.
User interface
- Button 1:
Press to send a device message to nRF Cloud.
- LED 1:
Indicates that the device is connected to LTE and is ready to send messages.
- LED 2:
Indicates that a message is being sent to nRF Cloud.
Configuration
See Configuring and building for information about how to permanently or temporarily change the configuration.
Setup
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.
Building and running
This sample can be found under samples/cellular/nrf_cloud_mqtt_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.
Testing
After programming the sample to your development kit, complete the following steps to test it:
Connect the kit to the computer using a USB cable. The kit is assigned a serial port. Serial ports are referred to as COM ports on Windows, /dev/ttyACM devices on Linux, and /dev/tty devices on macOS. To list Nordic Semiconductor devices connected to your computer together with their serial ports, open a terminal and run the
nrfutil device listcommand. Alternatively, check your operating system’s device manager or its equivalent.Connect to the kit with a terminal emulator (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
Reset the development kit.
Observe in the terminal window that the application starts. This is indicated by output similar to the following (there is also a lot of additional information about the LTE connection):
*** Booting My Application v1.0.0-2d648132f3d7 *** *** Using nRF Connect SDK v3.1.99-2d648132f3d7 *** *** Using Zephyr OS v4.2.99-36d18bbab237 *** [00:00:00.288,421] <inf> nrf_cloud_mqtt_device_message: nRF Cloud MQTT Device Message Sample, version: 1.0.0 [00:00:00.288,452] <inf> nrf_cloud_mqtt_device_message: Reset reason: 0x1 [00:00:00.857,360] <inf> nrf_cloud_credentials: Sec Tag: 16842753; CA: Yes, Client Cert: Yes, Private Key: Yes [00:00:00.857,421] <inf> nrf_cloud_credentials: CA Size: 1824, AWS: Likely, CoAP: Likely [00:00:00.857,452] <inf> nrf_cloud_mqtt_device_message: nRF Cloud credentials detected! [00:00:00.864,044] <inf> nrf_cloud_mqtt_device_message: Enabling connectivity... +CGEV: EXCE STATUS 0 +CEREG: 2,"81A6","03229B10",7 %MDMEV: PRACH CE-LEVEL 0 +CSCON: 1 +CGEV: ME PDN ACT 0 %MDMEV: SEARCH STATUS 2 +CEREG: 1,"81A6","03229B10",7,,,"11100000","11100000" [00:00:02.621,734] <inf> nrf_cloud_mqtt_device_message: Connected to LTE [00:00:02.621,856] <inf> nrf_cloud_mqtt_device_message: Waiting for modem to acquire network time... %XTIME: "80","52010221044480","01" +CGEV: IPV6 0 [00:00:05.622,772] <inf> nrf_cloud_mqtt_device_message: Network time obtained [00:00:05.625,183] <inf> nrf_cloud_info: Device ID: 12345678-1234-5678-9abc-def012345678 [00:00:05.625,762] <inf> nrf_cloud_info: IMEI: 358240123456789 [00:00:05.744,354] <inf> nrf_cloud_info: UUID: 12345678-1234-5678-9abc-def012345678 [00:00:05.744,842] <inf> nrf_cloud_info: Modem FW: mfw_nrf91x1_2.0.2 [00:00:05.744,873] <inf> nrf_cloud_info: Protocol: MQTT [00:00:05.744,903] <inf> nrf_cloud_info: Download protocol: HTTPS [00:00:05.744,903] <inf> nrf_cloud_info: Sec tag: 16842753 [00:00:05.744,934] <inf> nrf_cloud_info: Host name: mqtt.nrfcloud.com [00:00:05.744,964] <inf> nrf_cloud_mqtt_device_message: Connecting to nRF Cloud... [00:00:07.899,841] <inf> nrf_cloud_log: Changing cloud logging enabled to:1 [00:00:08.379,699] <inf> nrf_cloud_mqtt_device_message: Connection to nRF Cloud ready [00:00:08.379,791] <inf> nrf_cloud_mqtt_device_message: Reset reason: 0x1 [00:00:08.380,981] <inf> nrf_cloud_info: Team ID: 12345678-1234-5678-9abc-def012345670 [00:00:08.487,213] <inf> nrf_cloud_mqtt_device_message: Sent Hello World message with ID: 1760964049761
Troubleshooting
If you are not getting the output similar to the one in Testing, check the following potential issue:
- The network carrier does not provide date and time
The sample requires the network carrier to provide date and time to the modem. Without a valid date and time, the modem cannot generate JWTs with an expiration time.
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: