How to connect a microcontroller to Cumulocity IoT via LwM2M

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.

Pre-requirements

In this tutorial, lwm2m-esp will be used to deploy a LwM2M client on the microcontroller ESP32 board. This project is based on open-source repository Wakaama.

This client has been tested on the ESP32 Dev KitC.

To build and flash image to the board, PlatformIO 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.

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

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

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.

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:

Next steps

After successfully registering your device you can monitor and manage your first microcontroller on the Cumulocity IoT platform. Here are some more LwM2M device related instructions:

You can also check LwM2M section in the Cumulocity IoT documentation:

https://cumulocity.com/docs/protocol-integration/lwm2m/#overview

4 Likes

Thanks Yishu.
I sent you a LinkedIn invite. Hope we can connect. Thx