How to connect a microcontroller to Cumulocity IoT via LwM2M

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.

structure

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

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

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

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 deviceSingle RegistrationLWM2M
  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

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:

Getting started with LwM2M Knowledge base

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]](global.discourse-cdn.com/techcommunity/orig.. "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 regar…

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

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

Read full topic