CoAP Device API
CoAP (Constrained Application Protocol) là giao thức UDP nhẹ hơn MQTT, thiết kế cho thiết bị IoT bị giới hạn về bộ nhớ, CPU và băng thông. Phù hợp cho thiết bị NB-IoT, LTE-M, LoRa.
Kết nối
Host: vielang.io
Port: 5683 (CoAP), 5684 (CoAP + DTLS)
Base URL: coap://host/api/v1/$TOKEN/
Thay $TOKEN bằng Access Token của thiết bị.
Gửi Telemetry
POST coap://host/api/v1/$TOKEN/telemetry
Payload không có timestamp (server gán):
{ "temperature": 25.5, "humidity": 60, "battery": 85 }Payload có timestamp:
{
"ts": 1609459200000,
"values": { "temperature": 25.5, "humidity": 60 }
}Nhiều timestamp:
[
{ "ts": 1609459200000, "values": { "temperature": 25.0 } },
{ "ts": 1609459260000, "values": { "temperature": 25.5 } }
]Gửi Attributes
POST coap://host/api/v1/$TOKEN/attributes
{ "firmware": "v2.1.0", "model": "ESP32-S3", "location": "Room-101" }Đọc Attributes từ Server
GET coap://host/api/v1/$TOKEN/attributes?clientKeys=model&sharedKeys=config,threshold
Response:
{
"client": { "model": "ESP32-S3" },
"shared": { "config": "active", "threshold": 30 }
}Subscribe nhận Attribute Updates (Observe)
Dùng CoAP Observe option để nhận push khi shared attributes thay đổi:
GET coap://host/api/v1/$TOKEN/attributes
[Observe: 0] ← đăng ký subscribe
Server push khi có thay đổi:
{ "threshold": 35, "config": "updated" }RPC — Nhận lệnh từ Server
GET coap://host/api/v1/$TOKEN/rpc
[Observe: 0]
Server push RPC request:
{ "id": 1, "method": "reboot", "params": { "timeout": 60 } }Thiết bị gửi response:
POST coap://host/api/v1/$TOKEN/rpc/1
{ "result": "rebooting" }RPC — Gửi request lên Server (Client-side RPC)
POST coap://host/api/v1/$TOKEN/rpc
{ "method": "getTime", "params": {} }Response từ server:
{ "serverTime": 1609459200000 }Claiming Device
POST coap://host/api/v1/$TOKEN/claim
{ "secretKey": "my-secret-key", "durationMs": 3600000 }Device Provisioning
Thiết bị chưa đăng ký tự đăng ký qua CoAP:
POST coap://host/api/v1/provision
{
"deviceName": "CoAP-Device-001",
"provisionDeviceKey": "abc123",
"provisionDeviceSecret": "secret456"
}Response:
{
"credentialsType": "ACCESS_TOKEN",
"credentialsValue": "new-access-token",
"status": "SUCCESS"
}OTA Firmware Download
GET coap://host/api/v1/$TOKEN/firmware?title=MyFirmware&version=v2.0&chunk=0&size=4096
Tải từng chunk (4KB), lặp cho đến hết.
Bảo mật DTLS
Dùng DTLS (Datagram TLS) với port 5684:
coaps://host/api/v1/$TOKEN/telemetry
Hỗ trợ:
- PSK (Pre-Shared Key) — đơn giản, phù hợp thiết bị nhỏ
- X.509 Certificate — an toàn hơn, dùng cho production
So sánh CoAP vs MQTT
| Tính năng | CoAP | MQTT |
|---|---|---|
| Transport | UDP | TCP |
| Overhead | Rất thấp | Thấp |
| QoS | 0, 1 (CON/NON) | 0, 1, 2 |
| Publish/Subscribe | Observe | Native |
| Phù hợp | NB-IoT, pin thấp | Nhiều tính năng |
Ví dụ C (libcoap)
#include <coap3/coap.h>
// Gửi telemetry
void send_telemetry(coap_context_t *ctx, const char *token) {
coap_uri_t uri;
char url[256];
snprintf(url, sizeof(url),
"coap://vielang.io/api/v1/%s/telemetry", token);
char payload[] = "{\"temperature\": 25.5, \"humidity\": 60}";
// ... gửi POST request với payload
}