Understanding MQTT Protocol Development Concepts

Hello everyone! Today, we will discuss one of the most commonly used communication protocols in the Internet of Things (IoT) — the MQTT protocol. I will explain this seemingly professional technical concept in the simplest way possible.

What is MQTT?

MQTT (Message Queuing Telemetry Transport) is like a “WeChat” system specifically designed for IoT devices. It allows various smart devices (such as temperature sensors, smart bulbs, etc.) to easily send and receive messages from each other.

This protocol was originally developed by IBM and is characterized by:

  • Very lightweight (does not consume resources)

  • Open standard (anyone can use it)

  • Simple to implement

  • Particularly suitable for poor network conditions or devices with limited performance

Understanding MQTT Protocol Development Concepts

How does MQTT work?

Imagine a postal system:

  1. Post Office (Broker): This is the core of MQTT, responsible for receiving and distributing all messages

  2. Sender (Publisher): For example, a temperature sensor that “sends” temperature data to the post office

  3. Receiver (Subscriber): For example, your mobile app that tells the post office, “I want to receive temperature data”

When the temperature sensor publishes a temperature message to the “temp” topic, all devices that have subscribed to the “temp” topic will automatically receive this message.

Building an MQTT Service Yourself

Next, we will actually set up a simple MQTT service with just a few lines of code!

Using Aedes to set up the MQTT Broker

Step 1: Set Up the Post Office (Broker)

var mqtt = require("mqtt");
var client = mqtt.connect("mqtt://localhost:18080");

client.on('connect', (e) => {
  console.log("Successfully connected to the post office");
  client.subscribe('temp', function (err) {
    console.log("Subscribed to temperature information");
  });
});

client.on('message', function (topic, message) {
  console.log("Received new message:" + topic + ":\t" + message.toString());
});

This code creates an MQTT server, equivalent to opening a post office.

Step 2: Create the Sender (Publisher)

var mqtt = require("mqtt");
var client = mqtt.connect("mqtt://localhost:18080");

client.on("connect", (e) => {
    console.log("Successfully connected to the post office");
    setInterval(() => {
        client.publish("temp", "25.6");  // Send a message saying "25.6" every second
        console.log("Temperature message sent");
    }, 1000);
});

This program will send temperature data of 25.6°C every second.

Step 3: Create the Receiver (Subscriber)

var mqtt = require("mqtt");
var client = mqtt.connect("mqtt://localhost:18080");

client.on('connect', (e) => {
  console.log("Successfully connected to the post office");
  client.subscribe('temp', function (err) {
    console.log("Subscribed to temperature information");
  });
});

client.on('message', function (topic, message) {
  console.log("Received new message:" + topic + ":\t" + message.toString());
});

This program will display the received temperature data in real-time.

Running Effects

When you run these three programs simultaneously, you will see:

  1. The Post Office (Broker) says: “The post office is open! Listening on port 18080”

  2. The Sender (Publisher) keeps saying: “Temperature message sent”

  3. The Receiver (Subscriber) continuously displays: “Received new message: temp: 25.6”

It works just like a real postal system!

Why is MQTT so Popular?

  1. Bandwidth Efficient: The protocol is designed to be very lightweight, making it particularly suitable for IoT devices

  2. Good Real-time Performance: Messages can be pushed to subscribers immediately

  3. Flexible: Subscribers can be added or removed at any time

  4. Stable: It can work even in unstable network conditions

Practical Application Scenarios

  • Smart Home: Temperature Sensor → Mobile App

  • Industrial IoT: Machine Status Monitoring

  • Vehicle Networking: Vehicle Location Tracking

  • Agricultural IoT: Soil Moisture Monitoring

Leave a Comment