MQTT and the world of internet of things (IoT)

Written by The CloudMQTT Team

MQTT is a publish/subscribe protocol used by IoT devices to communicate with each other. Other alternatives to MQTT include XMPP and AMQP, but the main reason for MQTT’s popularity is that it is lightweight yet it also has robust bandwidth efficiency. The protocol is capable of transmitting data accurately under a low-bandwidth network.

IBM released the MQTT protocol in 1999 to communicate between IoT devices without an active internet connection. This article covers vital details related to MQTT including its uses, advantages, and several reasons for its popularity.

Also included are some interesting MQTT-based IoT projects. Beginners that are eager to get started with some cool and exciting IoT projects will definitely find them inspirational. Though MQTT holds several benefits, it still has some issues, and this article also offers a brief look at what to avoid and adjust.

What is MQTT?

MQTT is short for “Message Queuing Telemetry Transport," which operates on restricted low bandwidth networks and those with high latency. Such features make it suited to be used for M2M (machine-to-machine) communication, hence the wide use of MQTT in IoT. MQTT runs on TCP/IP, and any ordered, bi-directional, lossless connections can support it. Any data sent over the network through the MQTT protocol is application-specific.

In asynchronous communication, the sender doesn’t have to wait for the response from the receiver, so it can continue to send as much data as it wants. This is a huge benefit to IoT devices, let’s explore how.

Suppose a sensor needs to continuously send data in real-time to the user. It doesn’t have to wait for a response and can keep sending data that is usually displayed on a screen. This asynchronous communication also reduces the load on the network since no response is required that would create an extra load on the network. As a result, updates from the sensors are instantaneous and that’s why MQTT is preferred in most IoT projects.

To make the data transmission more efficient, each message has a header whose size is confined to 2 bytes. No message exceeds 256 MB in size.

MQTT in IoT

Luckily, no IoT developer is required to be an expert on MQTT prior to using it in their IoT projects. So how does MQTT play a vital role in the world of IoT? IoT primarily makes use of several sensors, whose work is to transmit the data to the cloud server. Some sort of transmission protocol is needed to carry out the data transmission process, and that’s where MQTT comes into the picture.

By way of an example, let’s suppose you’re working on a temperature monitoring project. You’ll need a temperature sensor, which will constantly send updated temperature readings to the cloud server. This data will pass through an MQTT broker in real-time through the MQTT protocol. The destination will be a cloud server and you’ll be able to see all the readings on your computer or mobile device.

It’s important to note that the temperature sensor will keep sending updated readings continuously to the cloud server without expecting a response. The cloud server will send a request to retrieve messages/data sent by the temperature sensor, but under normal circumstances, no message is sent to the sensors. Since the entire message sending process is an example of asynchronous communication, MQTT becomes ideal in the world of IoT.

Communication Using MQTT

Ideally, the following components are involved in communication using MQTT:

  • Publish and Subscribe system
  • Message/Data
  • Topics
  • MQTT Broker

Publish and Subscribe System

The “Publish and Subscribe System” states that a device can send/publish a message to any topic. On the other hand, it must be subscribed to the topic to be able to receive messages/data from it. For example, suppose a light bulb sends the data to a topic. If your smartphone is subscribed to that particular topic, it can receive that data.

Message/Data

A message is data exchanged between devices. It can be text, string, numbers, etc., and the format of a message is application specific.

Topics

A topic is the destination where the publisher is trying to publish the message. The destination is defined somewhat like a URL of a website or the file destination in a Windows PC. Topics have forward slashes that denote the continuity of the topic.

For instance, imagine you’ve set up a home office and you’re trying to send a message (“switch on”) to a lamp through your mobile phone. In this scenario, “lamp” is a part of the topic. Since the concerned lamp is located in the home office, then the “home” and “office” will also be a part of the topic. So, in this case, the final topic will be as follows:

Home/office/lamp

As “home” and “office” are two words, they must be separated by a forward slash (/) in a topic. The publisher publishes the message on the topic, and the lamp receives it through e.g. a Raspberry Pi or any other similar hardware.

MQTT Broker

An MQTT broker is software that you can install on hardware such as Raspberry Pi. The MQTT broker is responsible for forwarding the message to the device(s) or servers subscribed to the topic. Additionally, a broker performs message filtering, which is quite an important task.

Imagine 100 devices are subscribed to a topic but the message has to be passed to only a few of them. By filtering the message, the MQTT broker publishes/forwards it only to interested devices.

Importance of MQTT - Key Benefits

Here is a list of notable advantages that make MQTT so popular worldwide:

  1. MQTT clients are relatively lightweight, even the headers are only 2 bytes. This means that they consume the least hardware resources, and can be installed on small microcontrollers, saving money.
  2. This protocol facilitates two-way communication, though several IoT projects don’t necessarily require synchronous communication. Components can send data to the cloud server and vice versa.
  3. MQTT protocol is capable of connecting lots of IoT devices in a project. So no matter how many components you assemble in your project, MQTT will ensure that they’re up and online.
  4. Even if a client disconnects from the network, MQTT supports persistent sessions to ensure that the client is reconnected as soon as possible.
  5. Security is a major issue with IoT projects, but MQTT overcomes this hurdle as it can encrypts all the data with TLS before forwarding it. MQTT can also use a reliable authentication protocol (OAuth) for authenticating the clients connected to the network.
  6. MQTT allows quicker development by reducing the coding time spent on a project. It also reduces the lines of code to be written to develop a feature in the project.
  7. For the sake of security, MQTT protocol won’t read the messages unless the broker is asked to do so explicitly. This ensures that the data isn’t leaked at the broker level.
  8. The network bandwidth consumption is incredibly low when using the MQTT protocol. Less data will need less time to get transferred from the publisher to the subscriber, which helps achieve low latency.
  9. Since it supports asynchronous communication, you can easily build IoT projects that deliver results in real-time.
  10. You can transmit any type of data using the MQTT protocol, no matter if it’s a text, number, boolean, etc. The only thing that matters is that the subscriber must be able to interpret it.

Interesting MQTT Projects For Beginners

MQTT will become clearer once you get started with some IoT projects for newbies. Follow this link to find some interesting projects that are specially designed for newcomers. All of them make use of the MQTT protocol. Even if you have zero practical experience, those projects will cover everything from gathering all the required materials, tools, and equipment along with the coding. For even better understanding, don’t hesitate to watch the videos available as well.

CloudMQTT

CloudMQTT provides Mosquitto (MQTT) brokers that are located on the cloud. You can connect hardware such as Raspberry Pi to CloudMQTT. Moreover, it can also connect to a computer and other similar devices.

Ideally, you can implement an MQTT broker through CloudMQTT. Here’s a quick procedure to do that:

  1. Sign up on the CloudMQTT website.
  2. Log in and open up the “Instances” page.
  3. To create a fresh instance, click on “Create New Instance”.
  4. Enter the name as desired and select a plan. We recommend adding a tag as well, which will help identify your broker.
  5. Select a data center close to you/your application(s). If unsure about this, simply select US-East-1.
  6. Select a Mosquitto version and click “Review”.

These are the basic steps for setting up a broker for an IoT project. More details are available here.

Why CloudMQTT - Major Advantages

CloudMQTT makes it easier for IoT developers, especially beginners, to set up a broker for their projects. Here are some strong advantages of using CloudMQTT:

  1. It facilitates the exchange of messages between low-power sensors and mobile devices including smartphones and microcontrollers like Arduino.
  2. Most of the setup process is automated on CloudMQTT. This will allow you to focus on building your IoT product.
  3. In case of any issues or queries, CloudMQTT support is available 24x7.
  4. The CloudMQTT WebSocket client will enable you to interact with your device(s) such as sensor(s). You’ll be able to view all the data and even send commands to it through your browser. This feature is beneficial for testing and debugging processes.
  5. You can easily integrate Amazon Kinesis with CloudMQTT in case you’re looking to handle bulk data stream processing.
  6. Different members of a team using different CloudMQTT accounts can manage the same instance. You can decide where the notifications are sent and who will pay the bills or define a team leader.
  7. If you want to connect one MQTT broker to another, you can make use of the “Bridges” function. Though this feature is generally used to set up a personal HA cluster, which is an advanced topic.

IoT devices require the internet to work with each other and with other services. The underlying protocol, TCP/IP, is how internet connected devices communicate. But another layer is needed to direct the traffic and keep the data secure, and that’s where MQTT comes in. MQTT is a message protocol that supports asynchronous communication between services and devices. The messages between sender and receiver are decoupled, making MQTT the perfect choice for networks with latency or size challenges. Get started with a free instance from CloudMQTT and dig into your own IoT project today.