nRF9160: LwM2M Client

The LwM2M Client demonstrates how to use Lightweight Machine to Machine (LwM2M) protocol to connect an nRF9160 DK to an LwM2M server such as Leshan Demo Server via LTE. Once connected, the device can be queried to obtain GPS and sensor data, and to retrieve information about the modem.

Requirements

The sample supports the following development kit:

Hardware platforms

PCA

Board name

Build target

nRF9160 DK

PCA10090

nrf9160dk_nrf9160

nrf9160dk_nrf9160ns

The sample also requires an LwM2M server URL address available on the internet. For this sample, the URL address mentioned on the Leshan Demo Server page is used.

The sample is configured to compile and run as a non-secure application on nRF91’s Cortex-M33. Therefore, it automatically includes the Secure Partition Manager that prepares the required peripherals to be available for the application.

You can also configure it to use TF-M instead of Secure Partition Manager.

Overview

LwM2M is an application layer protocol based on CoAP/UDP. It is designed to expose various resources for reading, writing and executing via an LwM2M server in a very lightweight environment. The nRF9160 sample sends data such as button and switch states, accelerometer data (the physical orientation of the device), temperature and GPS position. It can also receive activation commands such as buzzer activation and light control.

Button states

DOWN/UP

Switch states

ON/OFF

Accelerometer data

FLIP

Temperature

TEMP

GPS coordinates

GPS

Buzzer

TRIGGER

Light control

ON/OFF

DTLS Support

The sample has DTLS security enabled by default. You need to provide the following information to the LwM2M server before you can make a successful connection:

  • Client endpoint

  • Identity

  • Pre-shared key

The following instructions describe how to register your device and these instructions are specific to Leshan Demo Server:

  1. Open the Leshan Demo Server web UI.

  2. Click on Security in the upper-right corner.

  3. Click on Add new client security configuration.

  4. Enter the following data and click Create:

    • Client endpoint - nrf-{your Device IMEI}

    • Security mode - Pre-Shared Key

    • Identity: - nrf-{your Device IMEI}

    • Key - 000102030405060708090a0b0c0d0e0f

  5. Build and run the LwM2M Client sample.

Configuration

See Configuring your application for information about how to permanently or temporarily change the configuration.

Configuration options

Check and configure the following configuration options for the sample:

CONFIG_APP_LWM2M_SERVER - LWM2M Server configuration

The sample configuration specifies the LWM2M Server to be used. In this sample, you can set this option to leshan.eclipseprojects.io (public Leshan Demo Server).

Configuration files

The sample provides predefined configuration files for typical use cases. You can find the configuration files in the samples/nrf9160/lwm2m_client directory.

The following files are available:

  • prj.conf - Standard default configuration file

  • overlay-queue - Enables LwM2M Queue Mode support

  • overlay-bootstrap.conf - Enables LwM2M bootstrap support

Building and Running

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

The sample is built as a non-secure firmware image for the nrf9160dk_nrf9160ns build target. Because of this, it automatically includes the Secure Partition Manager. You can also configure it to use TF-M instead of SPM.

See Building and programming a sample application for information about how to build and program the application.

Queue Mode support

To use the LwM2M Client sample with LwM2M Queue Mode support, build it with the -DOVERLAY_CONFIG=overlay-queue.conf option. See Providing CMake options for instructions on how to add this option.

Bootstrap support

To build the LwM2M Client sample with LwM2M bootstrap support, build it with the -DOVERLAY_CONFIG=overlay-bootstrap.conf option. See Providing CMake options for instructions on how to add this option.

In order to successfully run the bootstrap procedure, the device must be first registered in the LwM2M bootstrap server.

The following instructions describe how to register your device and these instructions are specific to Leshan Demo Server:

  1. Open the Leshan Demo Server bootstrap web UI.

  2. Click on Add new client bootstrap configuration.

  3. Enter the client endpoint - nrf-{your device IMEI}

  4. In the LWM2M Bootstrap Server tab, enter the following data:

    • Security mode - Pre-Shared Key

    • Identity - nrf-{your device IMEI}

    • Key - 000102030405060708090a0b0c0d0e0f

  5. In the LWM2M Server section, choose the desired configuration (No security or Pre-Shared Key). If you choose Pre-Shared Key, add the values for Identity and Key fields (the configured Identity/Key need not match the Bootstrap Server configuration). The same credentials will be provided in the Leshan Demo Server Security configuration page (see DTLS Support for instructions). If No Security is chosen, no further configuration is needed. Note that in this mode, no DTLS will be used for the communication with the LwM2M server.

  6. After adding values for the fields under both the LWM2M Bootstrap Server and LWM2M Server tabs, click Create.

  7. Build and run the LwM2M Client sample.

Testing

After programming the sample to your development kit, test it by performing the following steps:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager.

  2. Connect to the kit with a terminal emulator (for example, PuTTY). See How to connect with PuTTY for the required settings.

  3. Observe that the sample starts and automatically connects to the LwM2M Server with an endpoint named “nrf-{your Device IMEI}”.

Note

The IMEI of your device can be found on the bottom of the nRF9160 DK near a bar code with the FCC ID at the bottom.

Sample Output

The following output is logged in the terminal:

*** Booting Zephyr OS build zephyr-v2.4.0-198-ga4ead9805140  ***
[00:00:00.262,542] <inf> app_lwm2m_client: Run LWM2M client
[00:00:00.263,214] <inf> app_lwm2m_client: Initializing modem.
[00:00:00.277,648] <inf> app_lwm2m_client: endpoint: nrf-352656100162687
[00:00:00.278,167] <dbg> app_lwm2m_loc.lwm2m_init_location: GPS device found: GPS_SIM
[00:00:00.278,320] <err> app_lwm2m_temp: No temperature device found.
[00:00:00.278,869] <inf> app_lwm2m_accel: accelerometer normal
[00:00:00.278,961] <inf> app_lwm2m_firmware: Update Counter: current 0, update 0
[00:00:00.278,991] <inf> app_lwm2m_firmware: Image is not confirmed OK
[00:00:00.279,083] <inf> app_lwm2m_firmware: Marked image as OK
[00:00:00.279,083] <dbg> app_lwm2m_firmware.lwm2m_init_image: Erased flash area 7
[00:00:00.279,663] <inf> app_lwm2m_firmware: Update Counter updated
[00:00:00.279,663] <inf> app_lwm2m_firmware: Firmware updated successfully
[00:00:02.177,093] <inf> app_lwm2m_client: Connecting to LTE network.
[00:00:02.177,093] <inf> app_lwm2m_client: This may take several minutes.
[00:00:20.575,836] <inf> app_lwm2m_client: Connected to LTE network
[00:00:20.577,117] <inf> net_lwm2m_rd_client: Start LWM2M Client: nrf-352656100162687
[00:00:21.258,300] <inf> net_lwm2m_rd_client: RD Client started with endpoint 'nrf-352656100162687' with client lifetime 30
[00:00:22.898,651] <dbg> app_lwm2m_client.rd_client_event: Registration complete
[00:00:22.898,681] <inf> net_lwm2m_rd_client: Registration Done (EP='RMUcu4z1A2')

Dependencies

This application uses the following nRF Connect SDK libraries and drivers:

It uses the following sdk-nrfxlib library:

In addition, it uses the following sample: