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
underPlatform option
. For ESP32 board chooseespressif32
. - Set the
board
underPlatform option
. You can select the matching board here, for exampleesp32dev
. - Set
DWIFI_SSID
andDWIFI_PASSWORD
underBuild 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
- Copy the device name you set in
lib/LwM2m/LwM2mManager.h
- Log in to your Cumulocity tenant, navigate to
Registration
under Devices in the Device Management application - Click on
Register device
→Single Registration
→LWM2M
- Paste the device name into the
Device Endpoint Client ID
field - Choose NO_SEC as
security mode
- Set the same LWM2M Server URI as you set in
lib/LwM2m/LwM2mManager.h
, for example:coap://lwm2m.eu-latest.cumulocity.com:5783
- 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:
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]](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 regar…
You can also check LwM2M section in the Cumulocity IoT documentation:
https://cumulocity.com/docs/protocol-integration/lwm2m/#overview
Subscribe to my newsletter
Read articles from TECHcommunity_SAG directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
TECHcommunity_SAG
TECHcommunity_SAG
Discover, Share, and Collaborate with the Software AG Tech Community The Software AG Tech Community is your single best source for expert insights, getting the latest product updates, demos, trial downloads, documentation, code samples, videos and topical articles. But even more important, this community is tailored to meet your needs to improve productivity, accelerate development, solve problems, and achieve your goals. Join our dynamic group of users who rely on Software AG solutions every day, follow the link or you can even sign up and get access to Software AG's Developer Community. Thanks for stopping by, we hope to meet you soon.