What is MQTT, MQTT features, How does MQTT work, MQTT Connection and Broker, MQTT applications, MQTT with ESP32 or How to publish a message using ESP32 MQTT, MQTT testing, MQTT publish and subscribe architectureHello readers, today we will learn about the messaging protocol supported by ESP32(called MQTT protocol), which is used for IoT applications. The communication protocol to be used for data transmission and connectivity in web-enabled devices depends upon the type of IoT application.

The Internet of Things (IoT) is a network of interconnected computing devices like digital machines, automobiles with inbuilt sensors, having unique identifiers and the ability to communicate data over a network without the need for human intervention.

Before implementation, let’s first have a look at what is MQTT Protocol?

What is MQTT?

  • MQTT stands for Message Queuing Telemetry Protocol and is a messaging or communication protocol used for IoT applications.
  • In MQTT protocol, a publisher sends a message to the broker with a specific topic tag, and the broker forwards this message to all the Subscribers of that mentioned topic.
  • So, in order to understand MQTT Protocol, we need to discuss these 4 terms:
    1. MQTT Topic
    2. MQTT Publisher
    3. MQTT Broker
    4. MQTT Subscriber

Note:

  • The OASIS technical committee is the in-charge of MQTT specifications. The OASIS or the Organization for the Advancement of Structured Information Standards is a non-profit organization dedicated to the development, convergence, and implementation of electronic business standards.

What is MQTT, MQTT features, How does MQTT work, MQTT Connection and Broker, MQTT applications, MQTT with ESP32, publish a message using ESP32 MQTT, ESP32 MQTT testing

Fig 1: ESP32 MQTT Protocol

MQTT Topic

  • In MQTT protocol, a Topic is simply a UTF-8 string i.e. “Arduino”, “ESP32”, “Beginner Tutorials” etc.
  • MQTT Clients can subscribe to these Topics and are called Subscribers to that Topic.
  • MQTT Broker sends messages to the Clients based on their Topic subscription.
  • A topic may have multiple levels, separated by a forward slash.

MQTT Publisher

  • MQTT Publisher(also called MQTT client), as the name suggests, publishes the message on a specific topic and sends it to the broker.
  • In simple words, a publisher sends a message(normally a string) to the MQTT Broker, and this message also contains the Topic information.

MQTT Broker

  • MQTT Broker(also called MQTT server) acts as a coordinator between the subscriber and the publisher in order to create communication.
  • The broker’s job description is as follows:
    • Receiving messages from the publisher
    • Filtering received messages based on assigned Topics from the publisher.
    • Determining the interested subscriber in each message based on the assigned Topic
    • Finally forwarding the messages to subscribers

MQTT Subscriber

  • MQTT Subscriber(also called MQTT client), subscribes to the MQTT Topics and receives the messages from the MQTT broker, sent by the Publisher.

How does MQTT Work

This messaging protocol follows the Publish and Subscribe model. The Publisher and Subscribers of the message, communicate via Topics and are separated from one another. The broker is in charge of their communication. The broker’s job is to filter all incoming messages and distribute them to the subscribers in the most efficient way possible. The broker pushes the information to the client whenever something new becomes available, so the client doesn’t have to pull the information.

Because there are so many ready-to-use brokers and client applications, getting started with MQTT is a breeze.

What is MQTT, MQTT features, How does MQTT work, MQTT Connection and Broker, MQTT applications, MQTT with ESP32 or How to publish a message using ESP32 MQTT, MQTT testing, MQTT publish and subscribe architecture

Fig 2: MQTT Publish and Subscribe architecture

MQTT Features

Light Weight

It is a lightweight and versatile IoT communication and data transfer protocol aimed at IoT developers who want to strike a compromise between flexibility and network resources.

All the MQTT messages have a small footprint which adds a lightweight feature to this protocol.

In MQTT every message has:

  1. 2-byte header (fixed)
  2. A 256 MB message payload
  3. A 2-byte variable header (optional)

Security

MQTT protocol implementation will allow you to use your User name and password for security purposes. If you added the authentication feature while creating the MQTT server then stranger clients can’t communicate to your MQTT server.

Bidirectional communication

There is no direct link between clients in MQTT.

A broker connects the subscriber and the publisher in this messaging protocol. As a result, the subscriber and publisher can converse about any issue that the broker handles.

Eliminate polling

Polling is a procedure in which the controlling devices wait for input from an external device to determine whether the device is ready to broadcast data. MQTT protocol follows instantaneous push-based delivery. So there is no need to continuously check or poll before sending data which results in reduced network traffic.

Storage and forwarding

MQTT supports the storage and forwarding of persistent messages on the broker. Clients can ask for the broker to keep messages after they’ve been published. When this feature is used, any persisted message will be broadcast to a client who has subscribed to a topic. The most recent persistent message is the only one that gets saved. Unlike typical messaging queues, however, the MQTT broker prevents these persisted messages from being backed up inside the server.

Decouple and scale

It enables changes in communication patterns and functionality without causing a system-wide ripple effect.

Simplifies communication

As we have already discussed that MQTT follows Subscriber and Publisher architecture where the broker acts as an interface between the clients. So there is no need of computer to computer interface hence providing simplified communication.

Dynamic targeting

  • It also supports authentication, publishing, subscribing, keep-alive pings.
  • It runs on top of Internet protocol and TCP.

Quality of service

Quality of service is a kind of indicator which ensures that messages are exchanged between the sender and the receiver. There are three levels of QoS:

  1. just once – this level is not reliable but is the fastest one
  2. at least once – this level is the default mode

just once – this level is the most reliable, but slowest

MQTT Applications

MQTT protocol is mostly used in IoT(internet of things) applications for data transmission. The data can be read from some sensors or some temperature value.

What is MQTT, MQTT features, How does MQTT work, MQTT Connection and Broker, MQTT applications, MQTT with ESP32 or How to publish a message using ESP32 MQTT, MQTT testing

Fig 3: MQTT Applications

Some other applications where you can use it are :

  • Security and surveillance
  • Industries & energy
  • Logistics
  • Medical & healthcare

How to Publish a message using ESP32 MQTT?

  • The MQTT protocol is another functionality supported by the ESP32 module. To implement MQTT, we’re utilizing the Arduino IDE.
  • If you’re not sure where to begin coding with the ESP32, check out our prior tutorial, Introduction to the ESP32 Microcontroller Series.

Code Description

  • Global Declarations

Adding libraries will be the initial stage.

  • To use ESP32 MQTT to send a message, you’ll need two libraries.

#include <WiFi.h>

#include <PubSubClient.h>

  • PubSubClient library is not available in ESP32’s library list. You need to download the library first and add the library into Arduino IDE.

What is MQTT, MQTT features, How does MQTT work, MQTT Connection and Broker, MQTT applications, MQTT with ESP32 or How to publish a message using ESP32 MQTT, MQTT testing, Adding library to Arduino IDE

Fig 4: Adding library to Arduino IDE.

  • Click on Add. Zip Library.
  • Browse for the downloaded library file and add.
  • PubSubClient library and Wi-Fi library
  • We covered Wi-Fi in detail in our previous tutorial, ESP32 web server.
  • PubSubClient is a client library that may be used with MQTT applications.
  • Add your SSID and Password to create a wi-fi connection. Follow our previous tutorial, ESP32 web server, to learn more about ESP32 Wi-Fi.

const char* ssid = “public”; //add your SSID

const char* password = “ESP32@123”; // add your password

  • Add the details about MQTT broker

const char* mqttServer = “m11.cloudmqtt.com”;

const int mqttPort = 1883; //12948;

const char* mqttUser = “public”;

const char* mqttPassword = “ESP32@123”;

Arduino setup() Function

  • Initialize the serial monitor.
  • Initialize the Wi-Fi. (follow the previous tutorial for more details about (ESP32 Wi-Fi)

Serial.begin(115200);

WiFi.begin(ssid, password);

 

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println(“Connecting to WiFi..”);

}

  • Print Wi-Fi status on serial monitor

Serial.println(“Connected to the Wi-Fi network”);

  • Define the MQTT server’s address and port address.
  • To do so, we use the PubSubClient object’s setServer function.
  • The address and port, both defined early in global variables declaration, will be passed to this procedure as the first and second arguments, respectively.
  • setCallback method to define a handling function.
  • When a MQTT message is received on a subscribed topic, this handling function is called. This function’s code will be saved for later.

client.setServer(mqttServer, mqttPort);

while (!client.connected()) {

Serial.println(“Connecting to MQTT…”);

if (client.connect(“ESP32Client”, mqttUser, mqttPassword )) {

Serial.println(“connected to MQTT”);

} else {

Serial.print(“failed to connect “);

Serial.print(client.state());

delay(2000);

}

}

  • Publish a topic

client.publish(“esp/test”, “ESP32”);

  • Subscribe for the topic you want to.

client.subscribe(“esp/test”);

Finally, we’ll subscribe to the topic we’re interested in. Other clients’ communications will be published on that topic, and we will receive them this way. For that purpose, we use the subscribe method, which takes the name of the topic we want to subscribe to as an argument.

Arduino loop() Function

  • The next task will be to connect with the MQTT server which will happen in the main loop
  • Here, the device will keep trying to connect with the server unless it establishes a connection.
  • In order for the client to process incoming messages and maintain the connection to the MQTT server, the function should be invoked on a regular basis.

MQTT Testing

For testing purposes, we will use MQTT_lens which is a Google Chrome application to establish a connection with the broker.

  • Here are some screenshots of the MQTT lens broker:

What is MQTT, MQTT features, How does MQTT work, MQTT Connection and Broker, MQTT applications, MQTT with ESP32 or How to publish a message using ESP32 MQTT, MQTT testing

  • Create a connection after adding the following details:
  • Connection, Hostname, Port etc.

What is MQTT, MQTT features, How does MQTT work, MQTT Connection and Broker, MQTT applications, MQTT with ESP32 or How to publish a message using ESP32 MQTT, MQTT testing

Fig: MQTT_lens broker

 

This concludes today’s tutorial, hope you find it helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment