前置知识: 物联网

CoAP协议

00:00
3 min Intermediate 2026/6/14

CoAP协议详解:受限环境下的RESTful协议、消息模型、观察模式与安全机制。

1. CoAP 概述

1.1 什么是 CoAP

CoAP(Constrained Application Protocol)是专为受限设备设计的 RESTful 协议,运行在 UDP 之上。

1.2 与 HTTP 对比

对比项CoAPHTTP
传输层UDPTCP
头部大小4 字节数百字节
方法GET/POST/PUT/DELETE相同
数据格式CBOR/JSONJSON/HTML
发现内置需外部
组播支持支持
功耗

1.3 协议栈

┌──────────────┐
│  Application │
├──────────────┤
│  CoAP        │
├──────┬───────┤
│ DTLS │  UDP  │
├──────┴───────┤
│  IPv4/IPv6   │
├──────────────┤
│  6LoWPAN     │
└──────────────┘

2. 消息模型

2.1 消息类型

类型缩写描述
ConfirmableCON需要确认
Non-confirmableNON不需确认
AcknowledgementACK确认响应
ResetRST拒绝/错误

2.2 消息格式

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Ver| T |  TKL  |      Code     |          Message ID           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   Token (if any, TKL bytes) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   Options (if any) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|1 1 1 1 1 1 1 1|    Payload (if any) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

2.3 请求/响应模式

CON 请求

Client → CON GET /temperature → Server
Client ← ACK 2.05 Content "25.5" ← Server

NON 请求

Client → NON GET /temperature → Server
Client ← NON 2.05 Content "25.5" ← Server

分离响应处理时间较长时):

Client → CON GET /temperature → Server
Client ← ACK (空确认) ← Server
Client ← CON 2.05 Content "25.5" ← Server
Client → ACK → Server

3. RESTful 接口

3.1 方法

方法描述
GET获取
POST创建/处理
PUT更新
DELETE删除

3.2 响应码

含义
2.01Created
2.02Deleted
2.03Valid
2.04Changed
2.05Content
4.01Unauthorized
4.04Not Found
4.06Not Acceptable
5.00Internal Server Error

3.3 资源发现

GET /.well-known/core

→ </sensors/temp>;rt="temperature";if="sensor",
   </sensors/humidity>;rt="humidity";if="sensor",
   </actuators/led>;rt="led";if="actuator"

4. 观察模式(Observe)

4.1 原理

客户端注册观察服务器资源变化时主动推送

Client → GET /temperature (Observe=0) → Server
Client ← 2.05 Content "25.5" (Observe=10) ← Server
Client ← 2.05 Content "26.0" (Observe=11) ← Server
Client ← 2.05 Content "25.8" (Observe=12) ← Server

4.2 注册与取消

Observe 描述
0注册观察
1取消观察

5. 组播

5.1 组播地址

地址描述
FF02::FDCoAP 组播(链路本地
FF03::FDCoAP 组播(站点本地)
224.0.1.187IPv4 组播

5.2 组播场景

Client → MULTICAST GET /temperature → All Devices
Client ← 2.05 Content "25.5" ← Device 1
Client ← 2.05 Content "24.0" ← Device 2
Client ← 2.05 Content "26.2" ← Device 3

6. 安全机制

6.1 DTLS

CoAP 使用 DTLS(Datagram TLS)提供安全传输。

模式描述
NoSec安全(默认)
PreSharedKey共享密钥
RawPublicKey原始公钥
CertificateX.509 证书

6.2 OSCORE

OSCORE(Object Security for Constrained RESTful Environments)提供端到端安全,是 CoAP 的推荐安全方案

7. 代码示例

7.1 Python (aiocoap)

import asyncio
from aiocoap import *

async def main():
    context = await Context.create_client_context()
    request = Message(code=GET, uri='coap://localhost/temperature')
    response = await context.request(request).response
    print(f"Temperature: {response.payload.decode()}")

asyncio.run(main())

7.2 CoAP Server

import asyncio
from aiocoap import *

class TemperatureResource(resource.Resource):
    async def render_get(self, request):
        temp = read_sensor()
        return Message(payload=str(temp).encode())

async def main():
    root = resource.Site()
    root.add_resource(['temperature'], TemperatureResource())
    await Context.create_server_context(root)
    await asyncio.get_event_loop().create_future()

asyncio.run(main())

知识检测

学习进度

-- 已学文档
--% 知识覆盖率

学习推荐

专注模式