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 Objects và Resources, 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 ID | Tên | Dữ liệu |
|---|---|---|
| 0 | Security | Server URI, security mode |
| 1 | Server | Binding, lifetime, min/max period |
| 3 | Device | Manufacturer, model, serial, battery |
| 4 | Connectivity Monitoring | Signal strength, cell ID |
| 5 | Firmware Update | State, update result |
| 6 | Location | Latitude, longitude, altitude |
| 19 | BinaryAppDataContainer | Custom binary data |
| 3303 | Temperature Sensor | Sensor value, units |
| 3304 | Humidity Sensor | Sensor value |
| 3313 | Accelerometer | X, 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ị:
| Strategy | Mô tả | Băng thông |
|---|---|---|
| Single | Observe từng resource riêng lẻ | Cao |
| Composite All | Observe tất cả resources cùng lúc | Thấp |
| Composite by Object | Nhóm theo Object | Trung 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"
}
}| Method | Mô tả |
|---|---|
Read | Đọc giá trị resource |
Write | Ghi giá trị resource |
Execute | Thực thi resource (reboot, factory reset...) |
Observe | Đăng ký observe |
Cancel Observe | Hủy observe |
Discover | Khám phá resources |
Write Attributes | Cấ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);