What is MQTT?

MQTT is an extremely lightweight protocol that allows for messaging over TCP. It is optimized to handle an insane number of connected clients and increases messaging stability over spotty networks. Although MQTT has been around since 1999, the popularity of the protocol has exploded with the advent of “Smart” devices and  DIY-ers. See the full MQTT specification.

MQTT decouples clients from one another by using a broker. Unlike HTTP communication where one client communicates directly with another client; an MQTT client publishes and subscribes to data from a trusted broker. This keeps the clients decoupled from one another. By having their own connections to the broker, data can be communicated even if one or both of the clients have intermittent network access. Because of this MQTT ideal for communicating with remote sensors and systems using cellular networks.

Clients can be publishers, subscribers, or both. In this diagram, you can see a high-level view of the interaction between multiple clients and a broker.

In order to test your MQTT setup, there are a few free MQTT brokers you can use, one of the most popular being HiveMQ. Keep in mind that these free brokers should not be used for production applications; configure a private MQTT broker for your production apps.

In a recent guide, I demonstrate how a temperature sensor is connected with MQTT and published data on an interval.

Popular MQTT Brokers

MosquitoC/C++Windows, Mac, Linux, Raspberry Pi
HiveMQN/AWindows, Mac, Linux
Supports Docker and AWS
ActiveMQJava 5+Windows, Mac, Linux
JMQTTJava 8+Windows, Mac, Linux
RabbitMQ (requires a plugin)ErlangWindows, Mac, Linux
Docker, AWS, and more…
SwiftMQJavaWindows, Mac, Linux

If you just want to play around, a few of the companies that have broker solutions also have browser/desktop MQTT clients that you can use to send sample messages and become familiar with the protocol. HiveMQ [http://www.hivemq.com/demos/websocket-client/ ]and MQTTLens (Chrome Plugin) are the two that I’ve used.

When it comes time to write your own client to publish and subscribe from a broker, there are several libraries to help you get started.

Software Clients

Adafruit IOMIT LicenseRuby, Node.js, Python, Go
HiveMQ MQTT ClientApache v2Java
M2MQTTEclipse Public v1C#
MosquittoEclipse Public v2C
PubSubClientMIT LicenseC++/Arduino
Paho MQTTEclipse Public v1C/C++, C#, Go, Java, JavaScript
mqtt_clientMIT LicenseDart

Where is MQTT used?

As mentioned previously, the booming smart device industry is one of the most common places to find MQTT in use. This includes commercial and consumer electronics. From remote weather stations, farm data trackers, Geolocation Tags, to home security systems; MQTT plays a major role in the architecture of many in leaders in these fields. 

Additionally, the desire to make both fleet and personal vehicles more connected has led automotive software developers to lean on MQTT and LoRaWAN. Even if it’s not embedded in the vehicle’s hardware, MQTT could be used to send data to and from the little sensors that insurance companies ask you to place in your vehicle.

Window Security Sensor Example

Potential implementation of window security via MQTT
  1. When the window is opened or closed, a message is published to the MQTT broker. This message could be: JSON, String, XML, etc…
  2. A server on the backed subscribes to all updates within a specific topic in the broker. When new data is received via subscription, the server decides what to do next. For example, you would only want to sound an alarm if the window was opened after the security was enabled.
  3. The server publishes a message to the broker, this message contains the action for a subscriber of those actions to take.
  4. The same window sensor that published state previously is also subscribing to actions from the server. Depending on the action…
  5. Sound the alarm!!!!

Starting your car over Wifi Example

Potential implementation of starting car over WiFi with MQTT

Next, consider a vehicle such as a 2019 Subaru Forester, which allows you to start the vehicle using the “MySubaru” mobile application. You will notice that 100% of the vehicles which allow remote start via mobile app have a common feature: mobile hotspot; that’s no coincidence. 

A likely implementation of starting your Forester via MySubaru would likely mean:

  1. The user sends an HTTP Request to the MySubaru server, asking to start the vehicle. As a result, the user sees a spinning circle in the app.
  2. Assuming the user can make the request, the server publishes a message to an MQTT broker that instructs a subscriber to do some action. In this case “start”.
  3. Because the Forester is connected via a mobile hotspot, it is connected to the MQTT broker and subscribing to Actions from the server.
  4. Once the Subaru starts, it would publish a message to the broker, confirming that the action was completed.
  5. The MySubaru server is subscribing to a topic on the broker which vehicles will send their status.
  6. Finally, the spinner circle goes away as the result of the original HTTP Request in #1 is completed.

Next Steps with MQTT…

As you explore further into MQTT there are more advanced implementation details you will come across a few more topics: QoS, Security, and Retained Messages.

As each of these require their own deep dive to fully understand, they are out of the scope of this post. Check back soon for related posts that dig into each of these in detail.

To see an example of a temperature sensor that acts as an MQTT client to publish data that is subscribed by other systems, check out the source on Github and our detailed explanation of you can use the ESP32 & DHT11.