MQTT Protocol

Article by:
Date Published:
Last Modified:


MQTT is a lightweight publish/subscribe communications protocol that uses TCP/IP (and other secondary transport mechanisms). It is designed for remote sensors to communicate with controlling devices. The specification is under a royalty-free licence. Interesting features of the protocol include the one-to-many messaging service (publish/subscribe), the Last Will/Testament feature, and the three qualities of service. You can read more about it here.

The MQTT logo. Image from, acquired 2021-03-30.

The MQTT logo. Image from, acquired 2021-03-30.

MQTT-SN (MQTT for Sensor Networks) is an optimized version of the MQTT protocol designed to be used over non-TCP/IP connections.


Andy Stanford-Clark (working at IBM) and Arlen Nipper (working for Eurotech) published the first version of the MQTT protocol in 1999. It was used to monitor oil pipelines that used the SCADA (Supervisory Control and Data Acquisition) control system1, with connections to the internet primarily through expensive satellite connections2.

The MQ in MQTT stood for “Message Queue” in the IBM MQSeries, a family of messaging software products. Version 3.1 of the protocol referred to it as MQ Telemetry Transport, and hence the name “MQTT” was born1.

In 2013, IBM submitted MQTT v3.1 to the OASIS specification body1.

On March 7, 2019, MQTT Version 5 was released, and as of 2022-06 is the most recent major version.

MQTT Brokers

Mosquitto is a very popular open-source MQTT broker.

Online test/sandbox brokers:





Mosquitto can be installed on Windows by downloading the pre-compiled binaries. The x64 version will be installed in a path similar to C:\Program Files\Mosquitto. You will likely want to add that directory path to your system path so that you can call mosquitto from the command line.


If you have added the installation directory to your system path, you can invoke mosquitto with:


By default the above command will start a MQTT broker listening on port 1883 (the default port for non-encrypted traffic). The broker will not run as a daemon, so you can stop the broker with Ctrl-C (or equivalent). It also uses the following defaults:

  • No authentication

You can listen to all system topics by subscribing to $SYS/#.

If you are experiencing problems when running mosquitto, you might want to run in verbose mode -v to print debug information, as by default very little information is printed to the command-line:

$ mosquitto -v

1883 is the default port for unencrypted traffic. 8883 is the default port for traffic encrypted with SSL/TLS.

Common Errors

Client <client-name> disconnected due to protocol error.: Can be due to the client requesting TLS/SSL encryption on the non-encrypted 1883 port.

ssl3_read_bytes:tlsv1 alert unknown ca: Broker does not recognize the CA sent by the connecting client.

Creating Password Files

mosquitto_passwd is a utility provided alongside mosquitto which can generate password files for mosquitto. These password files control user access to the broker.

MQTT Clients

Paho is a popular MQTT client for Desktop and server machines running Windows, Linux or macOS. It’s API is reasonably well documented at

You can install Paho the standard way using pip:

pip install paho-mqtt

The Paho library can then be imported into your Python files with:

import paho.mqtt.client as mqtt

To request that the client creates the connection by supplying a username and password, call username_pw_set() on the client before calling connect():

client.username_pw_set('admin', 'password')
client.connect_async(self.broker_address, 1883, 60)

MQTT Clients For Embedded Devices

There are a number of MQTT clients designed for embedded devices, these include:

  • The embedded Paho MQTT client: Embedded version of the popular Paho MQTT client. It is released as two separate APIs:
    • MQTTPacket: Low-level C library that deals with the serialization/deserialization of MQTT packets.
    • MQTTClient: Higher-level C++ library first written for the mbed platform. Depends on MQTTPacket.
  • arduino-mqtt: Arduino wrapper around the lwmqtt MQTT client. Also available for PlatformIO. This one is pretty popular and is recommended in the AWS IoT tutorials.
  • coreMQTT: MQTT client maintained by the FreeRTOS group (however, the library does not depend on FreeRTOS to operate).


MQTT-SN (MQTT for Sensor Networks, previously known as just MQTT-S3) is a version of the MQTT protocol which has been optimized use on low-power, low-performance, wireless IoT devices. It is designed to work on non-TCP/IP networks, such as Zigbee3.

The following image shows the high-level MQTT-SN architecture, and how MQTT-SN clients talk to a standard MQTT broker.

High-level diagram of the MQTT-SN architecture[^bib-oasis-open-mqtt-protocol-spec-v1.2].

High-level diagram of the MQTT-SN architecture4.

Some of the changes in MQTT-SN include (compared to standard MQTT):

  1. Shortened two-octet (byte) topic IDs are used instead of full topic names (strings)5. This can cut down on the amount of bytes sent for topic identification considerably, e.g. the topic name device1/sensor5/temperature now just becomes topic ID 54.
  2. Ability to pre-assign topic IDs on both the client and Gateways (GW) so they don’t have to be registered across the network (with the REGISTER message).

The complete specification for MQTT-SN is called MQTT For Sensor Networks (MQTT-SN) Protocol Specification: Version 1.2 and can be downloaded for free from

MQTT-SN is designed in such a way that it is agnostic of the underlying networking services. Any network which provides a bi-directional data transfer service between any node and a particular one (a gateway) should be able to support MQTT-SN4.


  1. Wikipedia (2022, June 21). MQTT. Retrieved 2022-06-22, from↩︎

  2. Shaun Behrens (2019, Aug 09). From Oil Pipelines to the IoT: A Brief History of MQTT. Paessler. Retrieved 2022-06-22, from↩︎

  3. MQTT. MQTT Specifications. Retrieved 2022-06-20, from↩︎

  4. Andy Stanford-Clark and Hong Linh Truong (2013, Nov 14). MQTT For Sensor Networks (MQTT-SN) Protocol Specification: Version 1.2. Retrieved 2022-06-21, from↩︎

  5. ublox (2020, Jun 16). MQTT-SN – lowering the cost of IoT at scale. Retrieved 2022-06-20, from↩︎


Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Related Content:


comments powered by Disqus