CoAP Protocol
CoAP (Constrained Application Protocol) is a lightweight RESTful protocol designed for constrained devices and low-power networks. It uses UDP and follows a request/response model similar to HTTP, making it familiar for web developers working with IoT.
When to Use CoAP
Section titled “When to Use CoAP”Use CoAP when your device:
- Uses NB-IoT or other low-power wide-area networks (LPWAN)
- Needs a simple request/response model (like HTTP)
- Has limited memory and cannot maintain persistent connections
- Sends data infrequently (e.g., once per hour)
For devices that need continuous data streaming or real-time commands, standard MQTT over TLS is recommended.
Connection Details
Section titled “Connection Details”| Parameter | Value |
|---|---|
| Broker Host | mqtt.siliconwit.io |
| Port | 5683 (UDP) |
| Authentication | Device ID (username) + Access Token (password) |
| Method | POST to publish telemetry |
How It Works
Section titled “How It Works”CoAP messages are translated by the gateway into MQTT messages, which then flow through the standard SiliconWit telemetry pipeline:
Device (CoAP/UDP) --> Gateway (port 5683) --> MQTT Broker --> SiliconWit PlatformKey characteristics:
- RESTful: Uses familiar GET, POST, PUT, DELETE methods
- UDP-based: Low overhead, no TCP handshake
- Observe: Subscribe to resource changes (similar to MQTT subscribe)
- Block transfer: Send large payloads in smaller chunks
- Content negotiation: Supports JSON, CBOR, and other formats
Publishing Telemetry
Section titled “Publishing Telemetry”To send data, make a CoAP POST request to the telemetry resource:
POST coap://mqtt.siliconwit.io:5683/mqtt/d/{device_id}/tContent-Format: application/jsonPayload: {"temperature": 25.5, "humidity": 60}Authentication
Section titled “Authentication”CoAP uses the same credentials as standard MQTT:
- Username: Your Device ID (from the SiliconWit dashboard)
- Password: Your Access Token (from the SiliconWit dashboard)
Credentials are passed in the CoAP request. The exact method depends on your CoAP library.
Example: Python with CoAP
Section titled “Example: Python with CoAP”import asyncioimport jsonfrom aiocoap import Context, Message, POST
BROKER = "mqtt.siliconwit.io"PORT = 5683DEVICE_ID = "YOUR_DEVICE_ID"ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
async def publish_telemetry(): context = await Context.create_client_context()
payload = json.dumps({ "temperature": 25.5, "humidity": 60, "battery": 3.7 }).encode()
uri = f"coap://{BROKER}:{PORT}/mqtt/d/{DEVICE_ID}/t"
request = Message( code=POST, uri=uri, payload=payload ) request.opt.content_format = 50 # application/json
response = await context.request(request).response print(f"Response: {response.code}")
asyncio.run(publish_telemetry())Observing Commands
Section titled “Observing Commands”To receive commands from the SiliconWit platform, use CoAP’s Observe option:
GET coap://mqtt.siliconwit.io:5683/mqtt/d/{device_id}/cObserve: 0The gateway will push notifications whenever a command is published to your device’s command topic.
Payload Format
Section titled “Payload Format”CoAP payloads follow the same JSON format as standard MQTT:
{ "temperature": 25.5, "humidity": 60, "battery": 3.7}See Topics & Payloads for detailed formatting guidelines.
Limitations
Section titled “Limitations”- No encryption: CoAP currently runs over plain UDP without DTLS. For security-sensitive deployments, use standard MQTT with TLS on port 8883
- Gateway translation: Some CoAP features (like block-wise transfer) may not map directly to MQTT concepts
- Payload size: Keep payloads under 1 KB for reliable UDP delivery
Protocol Comparison
Section titled “Protocol Comparison”| Feature | MQTT (TLS) | MQTT-SN | CoAP |
|---|---|---|---|
| Transport | TCP + TLS | UDP | UDP |
| Port | 8883 | 1884 | 5683 |
| Encryption | TLS | None | None |
| Model | Pub/Sub | Pub/Sub | Request/Response |
| Best for | WiFi, Ethernet | 2G/GPRS, NB-IoT | NB-IoT, LPWAN |
| Persistent connection | Yes | Optional | No |
Next Steps
Section titled “Next Steps”- MQTT-SN Protocol - UDP-based publish/subscribe alternative
- MQTT Basics - Standard MQTT guide
- Topics & Payloads - Message formatting
- Authentication - Credential management