Today I am writing some info for MQTT Protocol. If you are a beginner and come to know about IoT, home automation, or want to make automation systems, this protocol will be very helpful for you. So you are in the right place now.
What is MQTT?
MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency, or unreliable networks. The design principles are to minimize network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.
How does it work?
The Basic thing to know about its working is to understand publishing and subscribing.
There are two main parts of MQTT communication
When we talk about the client in programing its means MQTT client. A Client means it will give something or it will take something. In MQTT broker both publisher and subscriber are clients because both are giving and taking something from the broker. If you want to define it in specific words we can say “MQTT client is any device that runs MQTT library and connects to the broker through a wireless network.
There are a few things that you should know before to use it because it based on these things.
Different clients which connect to the broker through the wireless networks only publish a message to on broker or subscribe to receive those messages from the broker which is already published by some other client.
Messages are information that one client wants to send to another client through a broker. It can be anything information, data, and commands to perform some type of action.
The important part to understand in MQTT communication is topics. Consider a client which wants to publish a message on the broker for a specific task, topics will tell which message goes where. At the same time, a client which is subscriber wants to read a message, but the question is that how will this client know which message to be read?. A developer who is working on it specifies topics to write(publish) and read(subscribe) on the broker. So the client can understand from topics what is to read.
Topics in MQTT communication represent by strings. Each string is separated by a forward slash. The figure below shows a example of room temperature. One more thing, to remember is that topics are case sensitive.
If you want to check the temperature of “Room 1 on the second floor in your home” you have to subscribe broker by the above message. At the same time, another client that is connected to a temperature sensor would publish temperature data on the same topic.
MQTT broker is the heart of communication and responsible for data subscribing and publishing. Broker filter published messages by clients then send the data on these messages to other clients which subscribe to get data.
There are many brokers to use but most prominent is cloud MQTT or mosquitto which is easy to use. It can be installed in raspberry pi and also in desktop PC. I will write a tutorial for its installation later.
How to use MQTT in IoT and automation as a non-techie.
You have to make a broker and so you can connect different clients to it. Different microcontrollers like Arduino, Esp32, and raspberry pi can perform an action according to the command received from the broker.
What steps you have to follow to make it run?
- Setup your MQTT broker in raspberry or in laptop.
- Add clients, Such as Esp8266/Esp32 or any other.
- Communicate data between client and broker.
MQTT is a lightweight messaging protocol using small bandwidth and fast. It is recommended to use for the internet of things and Automation projects. I can hope this article will be helpful for beginners.
If you are learning about IoT and home automation you should also read my post about Arduino
Thanks for reading this post and please comment if you need to know something more about it. Support me by subscribing to my blog I will further upload about MQTT installation and its IoT applications.