MQTT-SN works in a similar manor to MQTT and normally requires a connection to a broker/gateway before it can exchange messages.
MQTT-SN uses UDP which is a connectionless protocol the connection is actually a virtual connection.
However because MQTT-SN uses UDP , MQTT-SN also provides the ability to publish message without requiring a connection to a broker or gateway using a message with a QOS of 3 or -1.
However without a connection an MQTT-SN client cannot receive messages from a broker/gateway as it cannot subscribe to topics.
This in many cases is not a problem as many sensors like temperature sensors,motion sensors, smoke alarms etc only send data and don’t need to receive data.
Publishing with QOS -1 or QOS 3
The reason it is also known as QOS 3 is because the QOS field uses 2 bits so:
00=qos 0
01=qos 1
10=qos 2
11=qos -1 or 3
When publishing with QOS of 3 or -1 no connection message is sent.
You can see this in the broker trace below where I use MQTT_SN_PUB to publish without a connection using QOS of -1.
Using an MQTT-SN Broker or Gateway
Unfortunately there is only one MQTT-SN Broker (RSMB) which hasn’t been developed for many years.
Therefore a common option is to use a Gateway to convert MQTT-SN messages into MQTT messages.
Current Gateways implement the full MQTT-SN protocol and although they work, they introduce an extra layer of complexity and generally require an MQTT connection per MQTT-SN client.
For this reason I created a simple node-red flow that functions as an MQTT-SN Gateway for MQTT-SN QOS 3 messages only.
The flow leverage’s the existing packet creation and decode library code mqttsn-packet which can be downloaded here
Using this type of gateway simple MQTT-SN sensors can publish messages to MQTT clients.
The MQTT-SN sensor would not need to implement the full MQTT-SN Protocol stack.
With a bit of extra coding topic and messages filtering, and message combining could also be added.
Illustration Video
The flow is shown below:
Demo Flow
Implementing in Other Languages
Because most of the code is already available it can easily be implemented in C, nodejs and Python.
Settings Required
You will need to configure the UDP listener port and the MQTT broker IP address and port.
Application Areas
Any areas where you only need to publish data and not receive data.
It is particularly suited for lots of sensors as the gateway doesn’t use a single MQTT connection for all MQTT-SN clients.
Sensor Setup
Most IOT sensors support the http protocol for configuration so sensor setup could be done using HTTP and not MQTT-SN.
Related Tutorials and Resources
- MQTT-SN Gateway Advertisement and Discovery
- MQTT-SN Topic Names and Identifiers
- MQTT-SN RSMB Broker Overview,Install and Configuration
- Using The Python MQTT-SN Client
- MQTT-SN Publish and Subscribe Test Tools