LwM2M API

Giao thức LwM2M (IETF) cho quản lý thiết bị IoT chuẩn hóa — resource mapping, security PSK/X.509, và device management

LwM2M Device API

LwM2M (Lightweight Machine-to-Machine) là chuẩn IETF cho quản lý thiết bị IoT. Định nghĩa model dữ liệu chuẩn qua ObjectsResources, phù hợp với thiết bị NB-IoT, LTE-M, LPWAN.

Cấu hình Device Profile

Tạo LwM2M device profile: Device Profiles > Create > Transport: LwM2M

Chọn LwM2M Objects

Objects chuẩn IPSO:

Object IDTênDữ liệu
0SecurityServer URI, security mode
1ServerBinding, lifetime, min/max period
3DeviceManufacturer, model, serial, battery
4Connectivity MonitoringSignal strength, cell ID
5Firmware UpdateState, update result
6LocationLatitude, longitude, altitude
19BinaryAppDataContainerCustom binary data
3303Temperature SensorSensor value, units
3304Humidity SensorSensor value
3313AccelerometerX, Y, Z axes

Resource Mapping

Mỗi resource LwM2M có thể map thành:

  • Attribute: Dữ liệu tĩnh (model, manufacturer, location)
  • Telemetry: Dữ liệu chuỗi thời gian (temperature, signal strength)
  • Observe: Subscribe để nhận push khi giá trị thay đổi

Ví dụ mapping:

Object 3 (Device): /3/0/0 Manufacturer → attribute: manufacturer /3/0/1 Model Number → attribute: model /3/0/2 Serial Number → attribute: serial_number /3/0/9 Battery Level → telemetry: battery (+ observe) Object 4 (Connectivity): /4/0/2 Signal Strength → telemetry: signal (+ observe) /4/0/8 Cell ID → attribute: cell_id Object 3303 (Temperature): /3303/0/5700 Value → telemetry: temperature (+ observe) /3303/0/5701 Unit → attribute: temp_unit

Security Modes

No Security (development only)

coap://host:5685

Không dùng trong production.

Pre-Shared Key (PSK)

coaps://host:5686 Identity: device-identity-string Key: 0102030405... (hex)

Đơn giản, phù hợp thiết bị có ít tài nguyên.

X.509 Certificate

coaps://host:5686 Certificate: [PEM] Private Key: [PEM]

An toàn nhất, dùng cho production.

Raw Public Key (RPK)

coaps://host:5686 Public Key: [DER] Private Key: [DER]

Observation Strategy

Cách platform theo dõi dữ liệu từ thiết bị:

StrategyMô tảBăng thông
SingleObserve từng resource riêng lẻCao
Composite AllObserve tất cả resources cùng lúcThấp
Composite by ObjectNhóm theo ObjectTrung bình

Khuyến nghị: Composite All cho thiết bị pin, Single cho độ chính xác cao.


RPC Operations

Gửi lệnh từ server đến thiết bị qua API:

POST /api/v1/{deviceId}/rpc Content-Type: application/json { "method": "Execute", "params": { "id": "5/0/4" } }
MethodMô tả
ReadĐọc giá trị resource
WriteGhi giá trị resource
ExecuteThực thi resource (reboot, factory reset...)
ObserveĐăng ký observe
Cancel ObserveHủy observe
DiscoverKhám phá resources
Write AttributesCấu hình observe (pmin, pmax, step)

Ví dụ: Đọc nhiệt độ

{ "method": "Read", "params": { "id": "3303/0/5700" } }

Ví dụ: Reboot thiết bị

{ "method": "Execute", "params": { "id": "3/0/4" } }

Ví dụ: Cấu hình observe (báo khi thay đổi > 0.5°C)

{ "method": "Write Attributes", "params": { "id": "3303/0/5700", "attributes": { "pmin": 30, "pmax": 300, "step": 0.5 } } }

OTA Firmware Update (Object 5)

LwM2M hỗ trợ OTA qua Object 5 (Firmware Update):

/5/0/0 Package URI ← server ghi URL firmware /5/0/3 State → thiết bị báo: 0=Idle, 1=Downloading, 2=Downloaded, 3=Updating /5/0/5 Update Result → 0=Initial, 1=Success, 2-9=Error codes /5/0/2 Update ← server Execute để kích hoạt cài đặt

Platform tự động quản lý khi dùng tính năng OTA trong VieLang IoT.


DTLS Connection ID

Hỗ trợ DTLS CID (RFC 9146) cho môi trường NAT:

Enable CID: ✓ CID Length: 8 bytes

Cho phép thiết bị duy trì session khi IP/port thay đổi — quan trọng cho NB-IoT.


Transport Log

Xem log giao tiếp LwM2M của thiết bị:

Device > Latest Telemetry > transportLog

Log chứa các sự kiện: Register, Update, Deregister, Observe, Notify, Write, Execute.


Ví dụ: Thiết bị NB-IoT với Wakaama

#include "liblwm2m.h" lwm2m_object_t *securityObj = get_security_object( 0, "coaps://vielang.io:5686", PSK_IDENTITY, PSK_KEY, PSK_KEY_LEN); lwm2m_object_t *objects[] = { securityObj, get_server_object(), get_device_object(), get_temperature_object() }; lwm2m_context_t *ctx = lwm2m_init(NULL); lwm2m_configure(ctx, "MyDevice-001", NULL, NULL, 4, objects); lwm2m_register(ctx);