Introduction: What Is MQTT? | How to Use Tutorial MQTT | Best Tool for IoT Arduino ESP8266 Projects | Reyax Broker

Hey, what's up, Guys! Akarsh here from CETech.

Weighs as a feather and quick as light this is how we can introduce someone to the technology about which we are going to learn today. The Technology named MQTT is what we are going to learn about today. It is a messaging protocol that is rapidly becoming a part of many IoT-related applications. One such example where MQTT plays a very essential part is the Smart Home. So in this tutorial, we will learn what MQTT is, why we should use it, and how it works. Towards the end of this tutorial, we will also look at an MQTT Broker from Reyax. Don't worry we will also cover MQTT broker in this tutorial. We will not be doing anything on the application end today. We will leave that thing for our next tutorial. You can consider it as an introductory tutorial for the one in which we will actually work on MQTT. So that we feel more comfortable and familiar when we get our hands on it.

Now let's jump on to the knowledgeable part and take a look at this great technology.

Supplies

Reyax MQTT Broker- https://amzn.to/3A2ICkt

Step 1: Get PCBs for Your Projects Manufactured

PCBGOGO, established in 2015, offers turnkey PCB assembly services, including PCB manufacturing, PCB assembly, components sourcing, functional testing, and IC programming. PCBGOGO offers great quality PCBs for prices as cheap as $5 for 10 PCBs with a delivery time of 1-2 days for 2-layer PCBs.

Its manufacturing bases are equipped with the most advanced production equipment such as YAMAHA pick and place machine, Reflow oven, Wave soldering Machine, X-RAY, AOI testing machine; and the most professional technical personnel.

PCBGOGO a Chinese PCB Prototype Manufacturing Company has 5 factories of its own and has successfully fulfilled the needs of 300,000+ Customers till now. They are completing 3000+ online orders daily and each and every material is tested before shipping so you get the best products at your doorstep.

Join PCBGOGO now and you will get a $50 PCB Cash Coupon which can be used while ordering PCBs from PCBGOGO.

Step 2: What Is MQTT?

First of all, we will get to know what MQTT is. So, MQTT is a lightweight publish/subscribe protocol that requires a minimal footprint and bandwidth to connect to an IoT device. The device that sends the data is termed as a publisher and the device that is going to receive and use that data is the one that is termed as the subscriber. When we talk about the data that we transfer over MQTT then we come to know that it transfers a very small amount of data like 0 and 1 or you can say ON and OFF that's why it is considered lightweight and the message delivery is quick and reliable. It is a bidirectional communication protocol i.e. the transfer of data can be done from both ends. MQTT is event-driven and enables messages to be pushed to clients. This type of architecture decouples the clients from each other to enable a highly scalable solution without dependencies between data producers and data consumers.

It was designed by Andy Stanford-Clark (IBM) and Arlen Nipper in 1999 for connecting Oil Pipeline telemetry systems over satellite. Although it started as a proprietary protocol it was released Royalty free in 2010 and became an OASIS standard in 2014. MQTT stands for MQ Telemetry Transport but previously was known as Message Queuing Telemetry Transport. MQTT is fast becoming one of the main protocols for IoT (internet of things) deployments.

Step 3: How MQTT Works?

The MQTT Architecture consists of the following parts:-

  • Client: An MQTT client is any device from a microcontroller up to a fully-fledged server that runs an MQTT library and connects to an MQTT broker over a network. A Client can be either a publisher(The one who is sending the message) or a subscriber(The one who is receiving the message). Basically, the devices which are connected to the MQTT Broker are considered Clients. If a client is acting as a publisher then it will send its message to the broker. The broker will filter the received message based on topics. If a client is acting as a subscriber, then it will subscribe to the topic from which it needs the message and the broker will give it access to the messages stored on that topic.
  • Broker: An MQTT broker is a server that receives all messages from the clients and then routes the messages to the appropriate destination clients. A broker also filters the messages on the basis of topics and helps clients get access to their required messages easily and quickly.
  • Topic: In MQTT, the word topic refers to a UTF-8 string that the broker uses to filter messages for each connected client. The topic consists of one or more topic levels. Each topic level is separated by a forward slash. In comparison to a message queue, MQTT topics are very lightweight. The client does not need to create the desired topic before they publish or subscribe to it. The broker accepts each valid topic without any prior initialization.

MQTT works on a publish/subscribe pattern which provides an alternative to the traditional client-server architecture. In a client-server model, the client communicates directly to the endpoint but this is not the case in the pub/sub-model. Here the communication is settled through an MQTT broker which acts as a middle man for the communication and takes the message from one end to the other. Every message is a discrete chunk of data, opaque to the broker. Every message is published to an address on the broker, known as a topic. Clients may subscribe to multiple topics. Every client subscribed to a topic receives every message published to the topic. The decoupling between subscriber and publisher has three dimensions which are:-

  • Space decoupling, which means that the publisher and subscriber do not exchange any IP address and port.
  • Time decoupling, in which Publisher and Subscriber do not run at the same time.
  • Synchronization decoupling, in which operations on both sides are not needed to be interrupted during publishing or subscribing.

MQTT is a command-response protocol that's why each command is acknowledged whether it is the Command to establish a connection or a command to publish or subscribe. It uses TCP/IP protocol to connect to the broker. As the TCP/IP protocol is connection-oriented and has error correction capability it ensures that the packets are received in order and the information transfer is reliable.

Step 4: Features of MQTT

Some important features of the MQTT protocol are listed below:-

  • Authentication: MQTT provides authentication of every user who intends to publish or subscribe to particular data. The user id and password are stored in the API database. While connecting to the MQTT broker, we provide the username name and password, and the MQTT Broker will validate the credentials based on the values present in the database.
  • Access Control: MQTT determines which user is allowed to access which topics. By default, all users are allowed to access all topics.
  • QoS: The Quality of Service (QoS) level is the Quality transfer of messages which ensures the delivery of messages between sending body & receiving body. There are 3 QoS levels in MQTT:
    • At most once(0) –The message is delivered at most once, or it is not delivered at all.
    • At least once(1) – The message is always delivered at least once.
    • Exactly once(2) – The message is always delivered exactly once.
  • Last Will Message: MQTT uses the Last Will and Testament mechanism to notify ungrateful disconnection of a client to other clients. Whenever a client is connected to a broker, it specifies its last will message which is a normal MQTT message with QoS, topic, retained flag & payload. This message is stored by the Broker until it detects that the client has disconnected ungracefully.
  • Clean Sessions: A clean session is one in which the broker isn’t expected to remember anything about the client when it disconnects. With a nonclean session, the broker will remember client subscriptions and may hold undelivered messages for the client. However this depends on the Quality of service used when subscribing to topics, and the quality of service used when publishing to those topics.
  • Duplicate Message: If a publisher doesn’t receive the acknowledgment of the published packet, it will resend the packet with DUP flag set to true. A duplicate message contains the same Message-ID as the original message.
  • Retain Message: An already published message within any topic is retained by the broker and whenever any client subscribes to the topic then it is able to see the retained messages as well.
  • Keep Alive: Keep alive ensures that the connection between the broker and client is still open and that the broker and the client are aware of being connected. When the client establishes a connection to the broker, the client communicates a time interval in seconds to the broker. This interval defines the maximum length of time that the broker and client may not communicate with each other.

Step 5: Advantages and Disadvantages of MQTT

MQTT brings many powerful benefits to your process such as:

  • Distribute information more efficiently
  • Increase scalability
  • Reduce network bandwidth consumption dramatically
  • Reduce update rates to seconds
  • Very well-suited for remote sensing and control
  • Maximize available bandwidth
  • Extremely lightweight overhead
  • Very secure with permission-based security
  • Used by the oil-and-gas industry, Amazon, Facebook, and other major businesses
  • Saves development time
  • Publish/subscribe protocol collects more data with less bandwidth compared to polling protocols

Some of the drawbacks of MQTT are:-

  • Slower transmit cycle. Fast cycles are critical for systems with more than 250 devices.
  • Lack of security encryption. While MQTT uses Transport Layer Security/Secure Sockets Layer (TLS/SSL), it is primarily unencrypted.
  • Scalability. Creating a globally scalable network is more difficult with an MQTT protocol compared to other competitors.

Step 6: Reyax MQTT Broker

RYC1001 from Reyax is a cloud platform suitable for low-data-volume and power-saving devices. Using the MQTT protocol, you can use simple commands for your applications, monitor end devices, and easily establish your IoT connection. It can be used as long as the device supports the MQTT protocol. The compatibility test of all mainstream IoT communication modules has been completed. It can be used on Android, iOS, Windows, and Linux. The purpose is to allow enterprises to enter the cloud platform at a low cost. REYAX RYC1001 is built on a stable AWS service, we can use simple instructions to integrate the IoT cloud platform quickly as well. Some Important Features of the Reyax RYC1001 MQTT Broker are:-

  • Built on a stable AWS service
  • Use MQTT protocol suitable for low-data-volume and power-saving mode
  • Devices that support the MQTT protocol can be used.
  • Compatibility testing of all mainstream IoT communication modules is completed
  • Support Android, iOS, Windows, Linux
  • Low-cost access to the cloud platform
  • Use simple instructions to quickly integrate the IoT cloud platform

To get some more details about the Reyax RYC1001 MQTT Broker you can check the datasheet of the product from here.

So this brings us to the end of this tutorial. In the next part of this tutorial we are going to get our hands on the MQTT technology and by applying that we will make a relay board that will have an ESP8266 as well and will be capable of connecting to the Internet. It will basically act as a smart plug which we would be able to control from any place where the Internet is available. The board that we will make will be MQTT-based and you will enjoy making it. So check out the next tutorial if you want to get yourself started with MQTT.