Understanding IoT and MQTT Protocol

What is the Internet of Things

Internet of Things (IoT) refers to a network system that utilizes various sensors, devices, and objects to achieve interconnection and control through internet technology. With the continuous improvement of basic communication infrastructure, especially with the advent of 5G, the threshold and cost for connecting everything will gradually penetrate more fields and industries. The development of IoT technology will lead the digital and intelligent transformation (smart homes, smart transportation, smart health, industrial manufacturing, smart agriculture, smart cities, smart logistics, etc.), enhancing social productivity and quality of life.

Related Technologies

  • Sensor Technology: Utilizing sensors to perceive the state of objects and environments, such as temperature, humidity, light, etc.

  • Communication Technology: Including wireless communication technologies (such as Bluetooth, Wi-Fi, LoRa, etc.) and wired communication technologies (such as Ethernet, fiber optics, etc.) for data exchange and communication.

  • Data Storage Technology: Including data processing, storage, and analysis technologies used to handle data collected by sensors.

  • Cloud Computing Technology: Summarizing and analyzing a large amount of IoT data through cloud computing platforms to provide more advanced services and functions.

  • Security Technology: Including data encryption, identity authentication, access control, and other technologies to ensure the security and privacy of IoT systems.

MQTT

MQTT (Message Queue Telemetry Transport) protocol is a lightweight communication protocol based on the publish/subscribe model, built on the TCP/IP protocol. It provides real-time reliable messaging services for remote devices with minimal code and limited bandwidth, meeting the demands for low power consumption and low network bandwidth. Currently, almost all open cloud platforms, such as Alibaba Cloud and Tencent Cloud, support MQTT access.Understanding IoT and MQTT Protocol

Features of MQTT Protocol

  1. Simple implementation

  2. Provides QoS for data transmission

  3. Lightweight, low bandwidth usage

  4. Can transmit any type of data

  5. Can maintain sessions

Quality of Service (QoS) levels for messaging have three levels: QoS 0: At most once delivery: Messages may be lost but will not be retransmitted, potentially leading to message loss or duplicate reception. QoS 1: At least once delivery: Ensures messages can arrive, and if no acknowledgment is received, the client will retransmit the message, but it may lead to duplicate reception. QoS 2: Exactly once delivery: Ensures messages are received only once, involving a more complex message delivery and acknowledgment mechanism to ensure that messages are processed only once.

Installing MQTT Service

Install Mosquitto as the MQTT service. Mosquitto is lightweight, supports multiple systems, and is relatively easy to install and test. For Windows, download the installation package mosquitto-2.0.18-install-windows-x64.exe from the official website (https://mosquitto.org/) and install it by double-clicking it. Start the service (Mosquitto Broker).

Testing – Publish and Subscribe Messages

1 Use a Python script to subscribe to the topic (pip install paho-mqtt)

import paho.mqtt.client as mqtt

# Connection listener
def on_connect(client, userdata, flags, reason_code, properties):
    print(f"Connected with result code {reason_code}")
    client.subscribe("/mq/pub")  # Subscribe to the topic

# Message listener
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

# Initialization settings
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.on_connect = on_connect
mqttc.on_message = on_message

# Connect to MQTT service
mqttc.connect("127.0.0.1", 1883, 60)

mqttc.loop_forever()

2 Use the command line tool to publish a topic message: mosquitto_pub -h localhost -t /mq/pub -m {"data":111}

3 The Python script receives the message as follows, test OKUnderstanding IoT and MQTT Protocol

Leave a Comment