Random Nerd Tutorials
Shares

What is MQTT and How It Works

Shares

In this post, I’m going to introduce you to the MQTT protocol. MQTT stands for MQ Telemetry Transport.

It is a nice lightweight publish and subscribe system where you can publish and receive messages as a client.

It makes it really easy to establish a communication between multiple devices.

It is a simple messaging protocol, designed for constrained devices and with low-bandwidth. So, it’s the perfect solution for Internet of Things applications.

Recommended resources:

If you like home automation and you want to build a complete home automation system, I recommend downloading my home automation course.

Let’s get started!

Watch the video tutorial below


High Level Overview

Here’s a quick high level overview of what MQTT allows you to do.

You can send a command with a client (like Node-RED) to control an output:

Or you can read data from a sensor and publish it to a client (like Node-RED):

MQTT Basic Concepts

In MQTT there are a few basic concepts that you need to understand:

  • Publish/Subscribe
  • Messages
  • Topics
  • Broker

MQTT – Publish/Subscribe

The first concept is the publish and subscribe system.

What does that mean?

This means that a device can publish messages to your devices. Or your device can subscribe to a particular topic to receive those messages.

For example device 1 publishes on a topic.

Device 2 is subscribed to the same topic as device 1 is publishing in.

So, device 2 receives the message.

MQTT – Messages

Messages are the information that you want to exchange between your devices. Whether it’s a command or data.

With the publish and subscribe system you can do pretty much anything you want in your home automation projects.

MQTT – Topics

Another important concept is topics. Topics are the way you register interest for incoming messages or how you specify where you want to publish your message.

Topics are represented with strings separated by slashes. The slashes indicate the topic level.

Here’s an example on how you would create a topic for a lamp in your home office:

For example if you want to turn on a lamp in your home office using an ESP and a RPi, you publish a message to a topic using a client (Node-RED) saying “ON”.

Your device needs to be subscribed to that same topic, so it would receive the “ON” message and finally turn on the lamp.

Note: topics are case-sensitive, which makes these two topics different:

MQTT – Broker

At last, you also need to be aware of the term broker.

The broker is primarily responsible for receiving all messages, filtering the messages, decide who is interested in it and then publishing the message to all subscribed clients.

There are several brokers you can use. In future posts, we’re going to use the Mosquitto broker which can be installed in the Raspberry Pi.

Wrapping up

In the next blog post, we will experiment with MQTT and ESP8266 to see how everything works with practical examples.

Like home automation? Learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino with my course: Build a Home Automation System for $100.

Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

Leave a Comment:

Add Your Reply