Skip to content

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.

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.

ParameterValue
Broker Hostmqtt.siliconwit.io
Port1884 (UDP)
AuthenticationDevice ID (username) + Access Token (password)
Publish Topicd/{device_id}/t
Subscribe Topicd/{device_id}/c

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 Platform

Key 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

Before publishing, MQTT-SN clients must register their topic string to get a numeric topic ID. The gateway handles this mapping:

  1. Client sends REGISTER with topic string d/{device_id}/t
  2. Gateway assigns a topic ID (e.g., 1)
  3. Client uses topic ID 1 for all subsequent publishes

Most MQTT-SN client libraries handle registration automatically.

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.

#include <WiFi.h>
#include <WiFiUdp.h>
// MQTT-SN broker details
#define BROKER_HOST "mqtt.siliconwit.io"
#define BROKER_PORT 1884
// Your SiliconWit device credentials
const 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);
}

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.

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