MQTT Overview: Simple Protocol for IoT and Messaging
MQTT Overview: Simple Protocol for IoT and Messaging
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
|Mosquito||C/C++||Windows, Mac, Linux, Raspberry Pi|
|HiveMQ||N/A||Windows, Mac, Linux|
Supports Docker and AWS
|ActiveMQ||Java 5+||Windows, Mac, Linux|
|JMQTT||Java 8+||Windows, Mac, Linux|
|RabbitMQ (requires a plugin)||Erlang||Windows, Mac, Linux|
Docker, AWS, and more…
|SwiftMQ||Java||Windows, 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.
|Adafruit IO||MIT License||Ruby, Node.js, Python, Go|
|HiveMQ MQTT Client||Apache v2||Java|
|M2MQTT||Eclipse Public v1||C#|
|Mosquitto||Eclipse Public v2||C|
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
- When the window is opened or closed, a message is published to the MQTT broker. This message could be: JSON, String, XML, etc…
- 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.
- The server publishes a message to the broker, this message contains the action for a subscriber of those actions to take.
- The same window sensor that published state previously is also subscribing to actions from the server. Depending on the action…
- Sound the alarm!!!!
Starting your car over Wifi Example
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:
- 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.
- 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”.
- Because the Forester is connected via a mobile hotspot, it is connected to the MQTT broker and subscribing to Actions from the server.
- Once the Subaru starts, it would publish a message to the broker, confirming that the action was completed.
- The MySubaru server is subscribing to a topic on the broker which vehicles will send their status.
- 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.
[…] you need to learn the basics of MQTT, I recently published an article for beginners that describes MQTT and how it is used.For a full reference of this code and the deep sleep example, look in the Github […]