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:
You can also check LwM2M section in the Cumulocity IoT documentation:
This article is part of the TECHniques newsletter blog - technical tips and tricks for the Software AG community. Subscribe to receive our quarterly updates or read the latest issue.