Getting started with LwM2M
Tags: iot, cumulocity, devices, integration
Published: 2023-08-25 08:14:43

---
title: Getting started with LwM2M
published: true
date: 2023-07-13 09:20:06 UTC
tags: iot, cumulocity, devices, integration 
canonical_url: https://tech.forums.softwareag.com/t/getting-started-with-lwm2m/280993
---


# Introduction

Cumulocity IoT offers multiple different options to integrate devices allowing to cope with heterogeneous device fleets accommodating changing requirements as IoT projects scale and evolve.

[![Connectivity Overview](https://global.discourse-cdn.com/techcommunity/optimized/3X/4/c/4c4203c16f2bc8ef310f7cc6ba178b8a1423e174_2_690x291.png)](https://global.discourse-cdn.com/techcommunity/original/3X/4/c/4c4203c16f2bc8ef310f7cc6ba178b8a1423e174.png "Connectivity Overview")

There are different IoT use-cases like metering and asset tracking that require the deployment of dispersed and energy-constrained devices running on batteries in cellular and sensor networks. For such cases LwM2M stands out particularly, as it is very efficient regarding both power consumption as well as data usage. It thereby also recognizes that the average size of the data plan for some IoT devices is limited. Its lightweight design, however, does not change the fact that the protocol still works very efficiently on bigger IoT assets such as business routers or gateways.

Furthermore, it defines an extensible resource and data model, that can be extended and is able to support applications for various kinds of industries. Its service and device management enablement capabilities include for example sensor and meter readings, remote actuation as well as troubleshooting and performing remote firmware updates.

{% embed https://www.youtube.com/watch?v=XARu11qbOXQ %}

**If you are interested to explore how LwM2M devices interact with Cumulocity, you can quickly start by using the LwM2M demo client. We will now go through the different steps required to get it up and running.**

The LwM2M demo implementation is based on the [eclipse Leshan client](https://github.com/eclipse/leshan) . The leshan client demo sends temperature values and supports location tracking and device controls.
{% embed https://github.com/SoftwareAG/cumulocity-lwm2m-demo %}


# Prerequisites

To follow this guide, you only need the following:

- [**Docker**](https://www.docker.com/) must be installed
- A Cumulocity [**(Trial)**](https://signup.softwareag.cloud/#/basic?product=cumulocity&utm_referrer=TechCommunity)tenant

# Steps to follow

## 1. Getting Started

In the context of LwM2M, bootstrap servers are used to provision the LwM2M client with the information required to contact the LwM2M servers. It is therefore necessary to prepare the registration and docker-compose file for the bootstrap server configuration:

### registration.csv

In the registration.csv file, the fields for endpoint ID and LwM2M server URI must be checked. The endpoint ID is required by the bootstrap process in order to provision the LwM2M client with the bootstrap information. Make sure that the endpoint ID is unique and hasn’t been used before to register a device. Otherwise this will result in an error. Additionally, the LwM2M server URI needs to contain the right hosting environment that you or rather your company is using. This should be one of the following public cloud instances:

- [apj.cumulocity.com](http://apj.cumulocity.com)
- [cumulocity.com](http://cumulocity.com)
- [eu-latest.cumulocity.com](http://eu-latest.cumulocity.com)
- [us.cumulocity.com](http://us.cumulocity.com)
- [emea.cumulocity.com](http://emea.cumulocity.com)

coap://lwm2m.**[eu-latest.cumulocity.com](http://eu-latest.cumulocity.com)**:5783

The bootstrap ports are “5683” for unsecure bootstrap connections which we are using for this demo and “5684” for secure bootstrap connections. The LwM2M server ports are “5783” for unsecure server connections and “5784” for secure server connections. Note that these values can be different for other services.

Your individual tenant prefix is not required since there is only one LwM2M server for each public cloud instance. Please note that this registration file is only applicable for the dedicated LwM2M bulk registration process that has been introduced with version 10.15. If you are still using an older version of Cumulocity IoT, use the template for the general bulk registration you can find in the [documentation](https://cumulocity.com/guides/10.14.0/users-guide/device-management/#creds-upload).

[![registration](https://global.discourse-cdn.com/techcommunity/original/3X/a/5/a534ce4431160c197211da50f55949975f147b39.png)](https://global.discourse-cdn.com/techcommunity/original/3X/a/5/a534ce4431160c197211da50f55949975f147b39.png "registration")

### docker-compose.yaml

Edit the docker-compose.yaml and change the endpoint ID and URI in the environment variables for all containers so that they match the entries in the registration file.

```
version: "4.3"
services:
  lwm2m_client_1:
    build:
      context: leshan-demo-client
      args:
        ENDPOINT: "lwm2m_device01" 
        URI: "lwm2m.eu-latest.cumulocity.com"
...

```

## 2. Upload device protocols to Cumulocity IoT

LwM2M’s advantage is not only it’s resource efficiency but also that it provides a predefined and extensible data model. Each piece of information made available by the LwM2M Client is represented as a resource with multiple resources being logically organized into objects. There are multiple predefined objects available e.g. for updating firmware or reporting the state of an analog input. The data model can also be extended by defining additional objects to meet specific needs.

[![LwM2M object model](https://global.discourse-cdn.com/techcommunity/original/3X/1/5/153aec3024eb4a1758b9c74f1fe17538b33c048c.png)](https://global.discourse-cdn.com/techcommunity/original/3X/1/5/153aec3024eb4a1758b9c74f1fe17538b33c048c.png "LwM2M object model")

This demo client uses the following LWM2M objects:

- 3 (device)
- 6 (location),
- 3303 (temperature sensor)
- 3441 (lwm2m test object)

Cumulocity allows any LwM2M objects to be easily interfaced with the platform and supports out-of-the-box integration for the standard LwM2M objects 3(device), 4(connectivity), 5(firmware) and 6(location).

In order to make use of the objects, a protocol definition for each object is required. You can find the protocol definitions as xml files in the resource folder. These need to be uploaded to your Cumulocity IoT tenant in the **Device Management → Device types → Device protocols** tab.

[![device LwM2M post-operations|690x343 protocols](https://global.discourse-cdn.com/techcommunity/optimized/3X/b/3/b324327b411b4933c76cedd1eccf08b178a3a11a_2_690x317.png)](https://global.discourse-cdn.com/techcommunity/original/3X/b/3/b324327b411b4933c76cedd1eccf08b178a3a11a.png "device LwM2M post-operations|690x343 protocols")

The demo client sends a random temperature measurement at 3303/5700 (object/resource). To activate the functionality, open the LwM2M Device Protocol with the name “3303 Temperature” and turn on **Send measurement** for the Sensor Value. Do also enable on **Auto observe** for at least one functionality per protocol. This will instruct the LwM2M device to periodically report data to Cumulocity IoT.

[![send measurement](https://global.discourse-cdn.com/techcommunity/optimized/3X/d/e/de7966585ccd93cc9aac62f783ba002c67b74186_2_689x287.png)](https://global.discourse-cdn.com/techcommunity/original/3X/d/e/de7966585ccd93cc9aac62f783ba002c67b74186.png "send measurement")

The LwM2M objects 3 (device) and 6 (location) are supported out-of-the-box and do not need a mapping configuration.

## 3. Define LwM2M post-registration actions

LwM2M shell commands can be performed in the Shell tab of each device. In addition to this, there is an option to define tenant wide post-operations. Each LwM2M device will execute these after sending a full registration request.

In this example the LwM2M post operations will be used the read the current device info (3) and location (6) from the device, if the devices sends a registration request. To do this go to the tab **Device Management → Device types → LwM2M post-operations** and add the following:

```
read /3
read /6

```

[![LwM2M post-operations](https://global.discourse-cdn.com/techcommunity/optimized/3X/3/e/3e7861a88b5a8860593c24cc0b1c8234f66f7ad8_2_690x268.png)](https://global.discourse-cdn.com/techcommunity/original/3X/3/e/3e7861a88b5a8860593c24cc0b1c8234f66f7ad8.png "LwM2M post-operations")

## 4. Device Registration

To register the LwM2M devices in Cumulocity IoT, upload the CSV file that you prepared in step 1 in the LwM2M bulk registration dialog in **Device Management → Devices → Registration → Register device → LWM2M (Bulk Registration)**.

[![bulk registration](https://global.discourse-cdn.com/techcommunity/optimized/3X/e/6/e664e3d130b95896abaeee6c8ec4a42de670151f_2_690x341.png)](https://global.discourse-cdn.com/techcommunity/original/3X/e/6/e664e3d130b95896abaeee6c8ec4a42de670151f.png "bulk registration")

After the registration process was successful, the new LwM2M demo devices will be shown in the all device list in **Device Management → Devices → All devices**. The devices won’t show any incoming measurement values yet. First the demo client needs to be started. Do so by opening the folder in the Terminal and run the following command:

```
docker-compose up

```

Further information on how to read and send events, alarms or custom actions from LWM2M devices or how to write to LWM2M objects can be found in the [Cumulocity IoT documentation](https://cumulocity.com/guides/protocol-integration/lwm2m/).


[Read full topic](https://tech.forums.softwareag.com/t/getting-started-with-lwm2m/280993)