Introduction to MQTT-SN (MQTT for Sensor Networks)

MQTT-SN uses UDP and not TCP for its transport.

UDP is a connection less protocol whereas TCP is connection orientated.

MQTT-SN is designed,as far as possible., to work in the same way as MQTT

In that regard MQTT-SN usually requires a connection to the broker before it can send and receive messages.

This connection is in effect a virtual connection.

QOS Levels

MQTT-SN  supports QOS 0,1,2 as per MQTT, but it also supports a special publish QOS of 3 or -1.

Note: it is known as QOS -1 but the QOS flag in the message is set to 11 or decimal 3.

Publishing messages with a QOS of -1 or 3 doesn’t require an initial connection to have been set up.

You can publish using the topic id or short topic name.

Subscribing to MQTT-SN Topics

You can subscribe to a topics using 3 different formats:

  • A long topic name as per MQTT e.g. house/sensor1
  • A short topic name of 2 characters only e.g. s1
  • A pre-defined topic id (integer) e.g. 1

Wildcards can be used as per MQTT, but they only make sense for long topic names.

Publishing Messages With an Established Connection

You can publish a message without registering a topic or subscribing to a topic using:

  • A pre configured topic ID
  • A short topic name- 2 characters

You can also publish by getting a topic ID by either:

  • Subscribing to the long topic name
  • Registering the Long topic name

The Subscribe and register functions return a topic ID that you use in place of the long topic name when publishing.

Topic ids are assigned to each client and they are not broker wide. For example:

A client may subscribe to topic house/bulb1 and get a topic ID of 1. A second client may subscribe to topic house/bulb2 and get a topic ID of 1.

Topic id for client 2 refers to house/bulb2, and for client1 topic id 1 refers to house/bulb1

Note: When using QOS of 1 or 2 you need to wait for a PUBACL message before you publish a new message. So the format is

  1. PUB
  2. Wait PUBACK
  3. PUB
  4. etc

Publishing Messages Without a Connection

You can publish messages without first creating a connection by using QOS of -1 or 3.

You can publish a message without registering a topic or subscribing to a topic using:

  • A pre configured topic ID
  • A short topic name – 2 characters.

Published messages aren’t acknowledged.

This mode of publish is ideal for simple sensors

Broker Discovery

MQTT-SN clients have the ability to discover brokers.

There are two mechanisms used:

  • Advertising by a broker or Gateway
  • A Search by the client

Both methods use a multicast packet. Currently there is no standardized multicast packet address.

I did come across a proposal on Github for testing you will have to use a know free multicast address.

How it Works

The broker of gateway advertising on a multicast address. In the packet is the name of the advertising Gateway the advertising duration and the Gateway number.

The client maintains a list of active gateways and can use the duration to determine if a gateway is still active.

The client must be listening on the multicast address.

Alternatively the client can search for a gateway by sending a search packet on a multicast address.

This can be answered by either another client or a Gateway.

Mechanisms need to be employed to prevent broadcast storms and are covered in the specification.

Topic Registration

A client can register a topic with a broker and a broker can also register a topic with a client.

Client Registration

The client registers a long topic name with the broker and the broker returns a topic ID that the client uses to refers to that topic name when it publishes messages.

Broker or Gateway Registration

If a client subscribes to a wildcard topic e.g. house/#

What happens when a broker receives a publish for topic house/bulb2, as the client doesn’t have a topic ID for house/bulb2.

In the case the broker assigns a topic ID and notifies the client using topic registration.

MQTT Brokers

To test MQTT-SN you will need a broker. There currently aren’t many NQTT-SN brokers available.

The one I’m using for testing is the RSMB broker developed by Ian Craggs of IBM .

You can download the RSMB ( Really small message broker here here.  Here are the install and configuration instructions..

MQTT-SN Python Client

The RSMB src files also have a Python client. The client is written in python2.7

MQTT-SN-python-client-files

I have ported it to python3 but the files have lots of print statements that I have used to try and understand how it works. If you want them then use the contact form and I’ll email them.

Here are my current usage notes

Useful Links

Related Tutorials:

Save

Save

Save

Facebooktwittergoogle_plusredditpinterestlinkedinmail

3 comments

    1. I have edited the notes and added more details for the compile.You need to do this on Linux and run the make program when in the src folder. There are lots of .c and .h files in this folder but only one makefile.
      Once compiled you only the need the broker_mqtts file and should copy it into another folder away from the src files. The documentation is in the doc folder and also has install instructions.

Leave a Reply to steve Cancel reply

Your email address will not be published. Required fields are marked *