MQTT-SN Protocol
MQTT-SN (MQTT for Sensor Networks) is a lightweight publish/subscribe protocol designed for constrained devices and low-bandwidth networks. It runs over UDP, making it ideal for 2G/GPRS, NB-IoT, and other limited connectivity scenarios.
When to Use MQTT-SN
Section titled “When to Use MQTT-SN”Use MQTT-SN when your device:
- Has limited memory or processing power
- Connects over 2G/GPRS, NB-IoT, or Zigbee
- Cannot maintain a persistent TCP connection
- Needs to minimize bandwidth usage
For WiFi or Ethernet devices with sufficient resources, standard MQTT over TLS is recommended.
Connection Details
Section titled “Connection Details”| Parameter | Value |
|---|---|
| Broker Host | mqtt.siliconwit.io |
| Port | 1884 (UDP) |
| Authentication | Device ID (username) + Access Token (password) |
| Publish Topic | d/{device_id}/t |
| Subscribe Topic | d/{device_id}/c |
How It Works
Section titled “How It Works”MQTT-SN uses a gateway that translates between MQTT-SN (UDP) and standard MQTT (TCP). Your device connects to the gateway, and the gateway handles communication with the MQTT broker.
Device (MQTT-SN/UDP) --> Gateway (port 1884) --> MQTT Broker --> SiliconWit PlatformKey differences from standard MQTT:
- UDP instead of TCP: No connection setup overhead
- Topic IDs: Short numeric topic IDs replace long topic strings to save bandwidth
- Sleeping clients: Built-in support for devices that sleep between transmissions
- Discovery: Devices can discover the gateway automatically
Topic Registration
Section titled “Topic Registration”Before publishing, MQTT-SN clients must register their topic string to get a numeric topic ID. The gateway handles this mapping:
- Client sends REGISTER with topic string
d/{device_id}/t - Gateway assigns a topic ID (e.g.,
1) - Client uses topic ID
1for all subsequent publishes
Most MQTT-SN client libraries handle registration automatically.
Authentication
Section titled “Authentication”MQTT-SN 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 sent during the CONNECT phase.
Example: Arduino with MQTT-SN
Section titled “Example: Arduino with MQTT-SN”#include <WiFi.h>#include <WiFiUdp.h>
// MQTT-SN broker details#define BROKER_HOST "mqtt.siliconwit.io"#define BROKER_PORT 1884
// Your SiliconWit device credentialsconst char* deviceId = "YOUR_DEVICE_ID";const char* accessToken = "YOUR_ACCESS_TOKEN";
WiFiUDP udp;
void setup() { Serial.begin(115200);
// Connect to WiFi WiFi.begin("YOUR_SSID", "YOUR_PASSWORD"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected");
// Initialize MQTT-SN connection // (Implementation depends on your MQTT-SN library) // See: https://github.com/ty4tw/mqtt-sn-arduino}
void loop() { // Publish telemetry as JSON String payload = "{\"temperature\": 25.5, \"humidity\": 60}"; // mqttsn.publish(topicId, payload); delay(60000);}Payload Format
Section titled “Payload Format”MQTT-SN 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: MQTT-SN runs over plain UDP without TLS. For security-sensitive deployments, use standard MQTT with TLS on port 8883
- QoS: Supports QoS 0 (at most once) and QoS 1 (at least once). QoS 2 is not supported
- Payload size: Keep payloads small (under 1 KB recommended) 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 |
Next Steps
Section titled “Next Steps”- CoAP Protocol - Alternative UDP protocol for constrained devices
- MQTT Basics - Standard MQTT guide
- Topics & Payloads - Message formatting
- Authentication - Credential management