How to connect a microcontroller to Cumulocity IoT via LwM2M
Tags: iot, microcontroller, lightweight, opensource
Published: 2024-05-14 07:57:39

---
title: How to connect a microcontroller to Cumulocity IoT via LwM2M
published: true
date: 2024-04-12 07:55:55 UTC
tags: iot, microcontroller, Lightweight, opensource
canonical_url: https://tech.forums.softwareag.com/t/how-to-connect-a-microcontroller-to-cumulocity-iot-via-lwm2m/294356
---

 

## Introduction

Lightweight M2M (LwM2M) is an open-source protocol for IoT devices management. It is designed for addressing the needs of mobile low-power devices with very little compute power.  
This is ideal for the low-power microcontrollers which require efficient communication.

Cumulocity IoT supports integrating devices via LwM2M protocol. This article will show you as an example how to make a microcontroller – ESP32 to become a LwM2M client and then connect it to your Cumulocity IoT Platform via LwM2M protocol.

[![structure](https://global.discourse-cdn.com/techcommunity/optimized/3X/2/d/2d7defbf382c8c04268de08ff616f3ea83bb5138_2_297x375.png)](https://global.discourse-cdn.com/techcommunity/original/3X/2/d/2d7defbf382c8c04268de08ff616f3ea83bb5138.png "structure")

## Pre-requirements

In this tutorial, [lwm2m-esp](https://github.com/antonzk/lwm2m-esp) will be used to deploy a LwM2M client on the microcontroller ESP32 board. This project is based on open-source repository [Wakaama](https://github.com/eclipse-wakaama/wakaama).

This client has been tested on the ESP32 Dev KitC.

To build and flash image to the board, [PlatformIO](https://platformio.org/) is required. PlatformIO is a professional tool to build and flash images to the microcontrollers. You can install it as an extension in VScode.

> To install the PlatformIO extension to VScode you can follow the [installation instruction](https://docs.platformio.org/en/latest/integration/ide/vscode.html#installation).

## Prepare LwM2M client on the microcontroller

The LwM2M ESP32 project is a LwM2M client example for EPS32 boards. It supports common LwM2M objects including WLAN connectivity.

### Build, flash image

**1. Clone the repository**

`git clone https://github.com/antonzk/lwm2m-esp.git`

**2. Import project**

Open the home page of PlatformIO in VScode, open the targeted directory with `Open Project`

[![homepage](https://global.discourse-cdn.com/techcommunity/optimized/3X/a/e/ae507fcedfcc973fee68d42fe09bc55a1f17d069_2_517x336.jpeg)](https://global.discourse-cdn.com/techcommunity/original/3X/a/e/ae507fcedfcc973fee68d42fe09bc55a1f17d069.jpeg "homepage")

**3. Config LwM2M client**

Open the home page of PlatformIO, navigate to the configuration page of current project under the Projects tab.

- Set the `platform` under `Platform option`. For ESP32 board choose `espressif32`.
- Set the `board` under `Platform option`. You can select the matching board here, for example `esp32dev`.
- Set `DWIFI_SSID` and `DWIFI_PASSWORD` under `Build Options`
- Save the configurations

Open the `lib/LwM2m/LwM2mManager.h`

- Set the server URI. For example, `coap://lwm2m.eu-latest.cumulocity.com`.
- Set the server port to 5783.
- Give the device a unique name. This is the identity of the device and is used to register the device on the platform.

**4. Build and flash image to the device**

[![toolbarp](https://global.discourse-cdn.com/techcommunity/original/3X/a/6/a6d107e7250aae363b6edb656997386cf00d5c87.png)](https://global.discourse-cdn.com/techcommunity/original/3X/a/6/a6d107e7250aae363b6edb656997386cf00d5c87.png "toolbarp")

Click the `PlatformIO: Build` button on the toolbar. Once the project is built, click the `PlatformIO: Upload` button to flash the image to the device.

### Monitor Device

After flashing image to the ESP32 board, you can monitor the device and check its logs in the VScode Terminal by clicking the `PlatformIO: Serial Monitor` button.

[![logs](https://global.discourse-cdn.com/techcommunity/original/3X/8/2/828c2afe0e70ccc6c24800f909d7ac2b979453fd.png)](https://global.discourse-cdn.com/techcommunity/original/3X/8/2/828c2afe0e70ccc6c24800f909d7ac2b979453fd.png "logs")

When the Wi-Fi is connected and the terminal shows no errors, the LwM2M client is ready to be connected to your Cumulocity IoT platform.

## Connect microcontroller to Cumulocity IoT platform

1. Copy the device name you set in `lib/LwM2m/LwM2mManager.h`
2. Log in to your Cumulocity tenant, navigate to `Registration` under Devices in the Device Management application
3. Click on `Register device` → `Single Registration` → `LWM2M`
4. Paste the device name into the `Device Endpoint Client ID` field
5. Choose NO\_SEC as `security mode`
6. Set the same LWM2M Server URI as you set in `lib/LwM2m/LwM2mManager.h`, for example: `coap://lwm2m.eu-latest.cumulocity.com:5783`
7. Keep other configurations the same and register the device

Now you should be able to find your microcontroller in `All Devices` list:

[![dashboard](https://global.discourse-cdn.com/techcommunity/optimized/3X/9/d/9d5c7112e81daad4992bb98b4e79538d43216259_2_690x372.jpeg)](https://global.discourse-cdn.com/techcommunity/original/3X/9/d/9d5c7112e81daad4992bb98b4e79538d43216259.jpeg "dashboard")

## Next steps
[Read the full article here](https://tech.forums.softwareag.com/t/how-to-connect-a-microcontroller-to-cumulocity-iot-via-lwm2m/294356) to find out what the next steps are.