Skip to content

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.

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.

ParameterValue
Broker Hostmqtt.siliconwit.io
Port5683 (UDP)
AuthenticationDevice ID (username) + Access Token (password)
MethodPOST to publish telemetry

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 Platform

Key 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

To send data, make a CoAP POST request to the telemetry resource:

POST coap://mqtt.siliconwit.io:5683/mqtt/d/{device_id}/t
Content-Format: application/json
Payload: {"temperature": 25.5, "humidity": 60}

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.

import asyncio
import json
from aiocoap import Context, Message, POST
BROKER = "mqtt.siliconwit.io"
PORT = 5683
DEVICE_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())

To receive commands from the SiliconWit platform, use CoAP’s Observe option:

GET coap://mqtt.siliconwit.io:5683/mqtt/d/{device_id}/c
Observe: 0

The gateway will push notifications whenever a command is published to your device’s command topic.

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.

  • 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
FeatureMQTT (TLS)MQTT-SNCoAP
TransportTCP + TLSUDPUDP
Port888318845683
EncryptionTLSNoneNone
ModelPub/SubPub/SubRequest/Response
Best forWiFi, Ethernet2G/GPRS, NB-IoTNB-IoT, LPWAN
Persistent connectionYesOptionalNo