Recently, while testing the integration of Mobotix cameras with HomeAssistant, I found that the Mobotix camera images could be successfully integrated into HomeAssistant via the Onvif protocol or RTSP. So how can the alarm signals from the Mobotix camera be integrated into HA? Since the Mobotix camera supports the MQTT protocol, and HA also supports MQTT, it is very likely that the camera’s alarm signals can be integrated into HA via the MQTT protocol. Thus, I began to ponder the MQTT protocol.
MQTT can be simply understood as a common messaging protocol for the Internet of Things (IoT). The full name of this protocol is Message Queuing Telemetry Transport, which is a message transmission protocol based on a client-server, publish-subscribe model. It is characterized by being lightweight, open-source, simple, and easy to implement. The greatest advantage of MQTT is that it can provide real-time and reliable messaging services for connecting remote devices with minimal code and limited bandwidth. As a low-overhead, low-bandwidth instant messaging protocol, it has a wide range of applications in IoT, small devices, and mobile applications.
MQTT is a client-server based message publish/subscribe transmission protocol. The MQTT protocol is lightweight, simple, open, and easy to implement, which makes it applicable in a wide range of scenarios. The current version of the MQTT protocol is v5.0, released in 2017, while MQTT v3.1.1, released in 2014, is also frequently used. In addition to the standard version, there is a simplified version called MQTT-SN, which is mainly aimed at embedded devices that typically operate on low-power TCP/IP networks, such as ZigBee. The MQTT protocol runs on TCP/IP or other network protocols, providing ordered, lossless, and bidirectional connections. The characteristics include:
-
Using a publish/subscribe messaging model, it provides one-to-many message distribution to decouple from applications.
-
A message transmission mechanism that masks the payload content.
-
Minimized data transmission and protocol exchange (the protocol header is only 2 bytes) to reduce network traffic.
-
A notification mechanism that alerts both parties during abnormal interruptions.
-
Three levels of Quality of Service (QoS) for transmitted messages:
-
At most once, this level may result in message loss or duplication, with message publication relying on the underlying TCP/IP network. That is: <=1
-
At least once, this level ensures message delivery, but messages may be duplicated. That is: >=1
-
Exactly once, ensuring that messages are delivered only once. That is: =1. This level can be used in some strict billing systems.
1
Implementation of the MQTT Protocol
Implementing the MQTT protocol requires: a client and a server. There are three identities in the MQTT protocol: Publisher (Publish), Broker (Server), and Subscriber (Subscribe). Both the message publisher and subscriber are clients, while the message broker is the server. The message publisher can also be a subscriber. The messages transmitted by MQTT are divided into two parts: Topic and Payload.
-
Topic can be understood as the type of message. After subscribing, the subscriber will receive the message content (payload) of that topic.
-
Payload can be understood as the content of the message, referring to the specific content that the subscriber needs to use.
2
Network Transmission and Application Messages
MQTT will build the underlying network transmission: it will establish a connection from the client to the server, providing an ordered, lossless, byte-stream-based bidirectional transmission between the two.
When application data is sent through the MQTT network, MQTT will associate the relevant Quality of Service (QoS) and topic name (Topic) with it.
3
MQTT Client
An application or device that uses the MQTT protocol always establishes a network connection to the server. Clients can:
-
Publish information that other clients may subscribe to.
-
Subscribe to messages published by other clients.
-
Unsubscribe or delete application messages.
-
Disconnect from the server.
4
MQTT Server
The MQTT server is referred to as the “message broker” and can be an application or a device. It is located between the message publisher and subscriber, and it can:
-
Accept network connections from clients.
-
Accept application information published by clients.
-
Process subscription and unsubscription requests from clients.
-
Forward application messages to subscribed clients.
5
Subscriptions, Topics, and Sessions in the MQTT Protocol
To understand the transmission process of the MQTT protocol, it is necessary to know the meanings of the following terms:
-
Subscription includes a Topic Filter and maximum Quality of Service (QoS). A subscription is associated with a Session. A session can contain multiple subscriptions, and each subscription in a session has a different Topic Filter.
-
A Session is established when each client connects to the server, and there is state interaction between the client and the server. A session exists across a network and may span multiple continuous network connections between the client and the server.
-
Topic Name is a label connected to an application message that matches the server’s subscription. The server will send messages to each client that matches the subscribed label.
-
Topic Filter is a wildcard filter for Topic Names used in subscription expressions, indicating multiple topics that the subscription matches.
-
Payload is the specific content received by the message subscriber.
Click to identify and follow us
If you find this article helpful, please like it