Device communication method and apparatus based on MQTT protocol
By generating random topics and performing authentication through the MQTT topic management server, the security risks of communication between devices using the MQTT protocol are resolved, and secure communication between nodes is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ZTE CORP
- Filing Date
- 2021-12-02
- Publication Date
- 2026-05-12
AI Technical Summary
Existing inter-device communication based on the MQTT protocol has security risks, especially malicious nodes that can steal private information or publish misconfiguration information by subscribing to specific topics. Existing preventive measures such as TLS authentication are easily cracked, and network attacks can compromise IoT devices.
Random topics are generated by the MQTT topic management server, and authentication and communication are performed between the controlled node and the control node. The randomized topics are used for information transmission to ensure communication security.
It improves the security of inter-node communication in the MQTT protocol network, prevents malicious nodes from eavesdropping or interfering, and enhances the security and reliability of information transmission.
Smart Images

Figure CN116232616B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of communications, and more specifically, to a device communication method and apparatus based on the Message Queuing Telemetry Transport (MQTT) protocol. Background Technology
[0002] MQTT is an Internet of Things (IoT) communication protocol. As a low-overhead, low-bandwidth instant messaging protocol, it is widely used in IoT, small devices, and mobile applications.
[0003] In IoT systems, there are often point-to-point device configuration / information query needs. The MQTT protocol is based on the publish / subscribe model, in which two IoT nodes with communication needs typically communicate through agreed-upon topics.
[0004] Under this mechanism, if a malicious MQTT node exists in the network that knows the agreed-upon topic, it can steal private information by subscribing to specific topics or publishing incorrect configuration information to those topics. This poses a significant information security risk.
[0005] The common approach to preventing such security issues is to prohibit unauthorized nodes from accessing the MQTT network. This can be achieved by using two-way authentication with Transport Layer Security (TLS) at the MQTT protocol layer to ensure that only legitimate devices can access the MQTT network. However, this approach has the following drawbacks:
[0006] 1. If the software of an IoT device is cracked, unauthorized devices can also authenticate and access the MQTT network by extracting the corresponding authentication certificate;
[0007] 2. Network attackers can use other network services of IoT devices to intrude into IoT devices that have already connected to the MQTT network, thereby achieving the purpose of illegally intruding into the MQTT network. Summary of the Invention
[0008] This invention provides a device communication method and apparatus based on the MQTT protocol, which at least solves the problem of security risks in device-to-device communication based on the MQTT protocol in related technologies.
[0009] According to an embodiment of the present invention, a device communication method based on the MQTT protocol is provided, applied to an MQTT topic management server, comprising: receiving a first message reported by a controlled node, wherein the first message includes the device ID of the controlled node; generating a random topic corresponding to the device ID of the controlled node, and returning the random topic to the controlled node; and after authenticating the control node, sending the random topic to the control node, so that the control node and the controlled node can communicate via the random topic using the MQTT protocol.
[0010] In an exemplary embodiment, the MQTT topic management server receiving the first message reported by the controlled node includes: the MQTT topic management server receiving the first message reported by the controlled node through the Hyper Text Transfer Protocol (HTTP) POST method.
[0011] In an exemplary embodiment, the MQTT topic management server generates a random topic corresponding to the device ID of the controlled node and returns the random topic to the controlled node, including: the MQTT topic management server calls a random string generator to generate a first random string as the random topic and caches the mapping between the device ID of the controlled node and the first random string; the MQTT topic management server returns the first random string to the controlled node.
[0012] In one exemplary embodiment, after the MQTT topic management server returns the first random string to the controlled node, the method further includes: the MQTT topic management server sending a first subscription message to the MQTT broker server, wherein the subscription topic in the first subscription message is a will topic, and the will topic includes the first random string and a fixed suffix.
[0013] In an exemplary embodiment, after the MQTT topic management server returns the first random string to the controlled node, the method further includes: the controlled node sending an MQTT connection message to the MQTT broker server, wherein the will topic in the MQTT connection message includes the first random string and a fixed suffix.
[0014] In one exemplary embodiment, the method further includes: after the controlled node successfully establishes an MQTT connection, sending a second subscription message to the MQTT broker server, wherein the subscription topic in the second subscription message is the first random string.
[0015] In an exemplary embodiment, before the MQTT topic management server sends the random topic to the control node, the method further includes: the MQTT topic management server receiving an authentication message reported by the control node via HTTP POST, the authentication message including the username and password required for authentication by the control node; the MQTT topic management server sending a first reply message to the control node, wherein a cookie is set as an authentication marker in the first reply message; the MQTT topic management server receiving a second message sent by the control node, wherein the second message includes the device ID of the controlled node and the returned cookie; the MQTT topic management server confirming that the node sending the message is the control node based on the cookie in the second message, and searching for the first random string in the cached mapping between device IDs and first random strings based on the device ID in the message, generating a second random string, and returning the first random string and the second random string to the control node.
[0016] In one exemplary embodiment, after returning the first random string and the second random string to the control node, the method further includes: the control node sending a third subscription message to the MQTT broker server, wherein the subscription topic in the third subscription message is the second random string; after receiving a successful subscription reply from the MQTT broker server, the control node sending a first publish message to the MQTT broker server, wherein the MQTT topic of the first publish message is the first random string, and the message content includes a management message and the second random string.
[0017] In one exemplary embodiment, after the MQTT broker server receives the first publish message, the method further includes: the MQTT broker server forwarding the first publish message to the controlled node; the controlled node, after executing a management message based on the first publish message, sending a second publish message to the MQTT broker server, wherein the MQTT subject of the second publish message is the second random string, and the message content is a reply to the management message; after receiving the second publish message, the MQTT broker server forwards the second publish message to the control node based on the MQTT subject being the second random string.
[0018] In one exemplary embodiment, the method further includes: if the MQTT broker server discovers that the controlled node is offline, it sends a third publication message to all nodes that have subscribed to the will topic, the message content of the third publication message being that the controlled node is offline.
[0019] In one exemplary embodiment, the method further includes: after receiving the third MQTT publish message, the MQTT topic management server deletes the cached mapping between the device ID of the controlled node and the first random string according to a random topic.
[0020] In one exemplary embodiment, the method further includes: an MQTT topic management server receiving an authentication message reported by the control node via HTTP POST, the authentication message including the username and password required for the control node authentication; the MQTT topic management server sending a first reply message to the control node, wherein a cookie is set as an authentication marker in the first reply message; the MQTT topic management server receiving a second message sent by the control node, wherein the second message includes the device ID of the controlled node and the returned cookie; the MQTT topic management server confirming that the node sending the message is the control node based on the cookie in the second message, and searching for the first random string in the cached mapping between device IDs and first random strings based on the device ID in the message; if not found, sending a second reply message to the control node, wherein the content of the second reply message is "The controlled node could not be found".
[0021] In an exemplary embodiment, after the MQTT topic management server sends the second reply message to the control node, the method further includes: the control node, based on the received second reply message, discovers that the controlled node is offline, and then abandons the management of the controlled node.
[0022] According to another embodiment of the present invention, a device communication method based on the MQTT protocol is also provided, applied to a controlled node. The method includes: reporting a first message to the MQTT topic management server, wherein the first message includes the device ID of the controlled node; receiving a random topic generated by the MQTT topic management server corresponding to the device ID of the controlled node; and after the control node is authenticated by the MQTT topic management server and obtains the random topic from the MQTT topic management server, the controlled node and the control node communicate using the MQTT protocol through the random topic.
[0023] According to another embodiment of the present invention, an MQTT topic management server is provided, comprising: a first receiving module, configured to receive a first message reported by a controlled node, wherein the first message includes the device ID of the controlled node; a generating module, configured to generate a random topic corresponding to the device ID of the controlled node, and return the random topic to the controlled node; and an authentication module, configured to send the random topic to the control node after authenticating the control node, so that the control node and the controlled node can communicate via the random topic using the MQTT protocol.
[0024] According to another embodiment of the present invention, a controlled node is also provided, characterized in that it includes: a sending module, configured to report a first message to the MQTT topic management server, wherein the first message includes the device ID of the controlled node; a second receiving module, configured to receive a random topic generated by the MQTT topic management server corresponding to the device ID of the controlled node; and a communication module, configured to communicate with the control node via the random topic through the random topic after the control node has been authenticated by the MQTT topic management server and obtained the random topic from the MQTT topic management server.
[0025] According to yet another embodiment of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, wherein the computer program is configured to perform the steps in any of the above method embodiments when executed.
[0026] According to yet another embodiment of the present invention, an electronic device is also provided, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0027] In the above embodiments of the present invention, the MQTT topics for node communication are managed by an MQTT topic management server, and the MQTT topics are randomized so that malicious network nodes cannot listen to or interfere with node communication, thereby improving the security of inter-node communication in the MQTT protocol network. Attached Figure Description
[0028] Figure 1 This is a schematic diagram of a network structure according to an embodiment of the present invention;
[0029] Figure 2 This is a flowchart of a device communication method based on the MQTT protocol according to an embodiment of the present invention;
[0030] Figure 3 This is a structural block diagram of an MQTT topic management server according to an embodiment of the present invention;
[0031] Figure 4 This is a schematic diagram of the structure of a controlled node according to an embodiment of the present invention;
[0032] Figure 5 This is a flowchart of a communication method for a controlled node in an online scenario according to an embodiment of the present invention;
[0033] Figure 6 This is a flowchart of a communication method for a controlled node in an offline scenario according to an embodiment of the present invention. Detailed Implementation
[0034] The embodiments of the present invention will be described in detail below with reference to the accompanying drawings and examples.
[0035] It should be noted that the terms "first," "second," etc., in the specification, claims, and drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0036] In an MQTT network, MQTT clients can subscribe to topics or push messages to a topic. When an MQTT broker receives a message pushed to a topic by an MQTT client, it forwards this message to all other MQTT clients subscribed to that topic. This mechanism facilitates one-to-many communication while also ensuring that information from one-to-one communications is easily accessible to other MQTT clients. Additionally, MQTT clients can register will messages with the MQTT broker. When an MQTT client disconnects its MQTT connection or stops responding to heartbeats, the MQTT broker will push this registered will message to the registered topic.
[0037] Therefore, this invention provides a device communication method based on the MQTT protocol. In the network environment used in this invention, IoT device nodes (i.e., controlled nodes) and management nodes (i.e., control nodes) all act as MQTT clients connected to the MQTT broker server, forming an MQTT network.
[0038] In this embodiment, IoT device nodes can connect to the MQTT topic management server and can only report device IDs; they cannot obtain information from the MQTT topic management server. Management nodes can connect to the MQTT topic management server using strong authentication methods (such as username / password login) and can obtain secure communication MQTT topics using the device ID they wish to manage.
[0039] like Figure 1 As shown, the network architecture used in this embodiment of the invention is as follows:
[0040] MQTT broker server: Responsible for accepting MQTT connections and forwarding MQTT messages. All message exchanges between the control node and the controlled nodes are forwarded by the MQTT broker server.
[0041] Controlled nodes: These are typically IoT device nodes that accept configuration / information queries from the control nodes.
[0042] Control node: Generally, it is the management backend or management controlled node (PP) of IoT devices, responsible for the management / information collection of IoT devices.
[0043] The MQTT topic management server accepts HTTP connections between controlled nodes and the control node. Controlled nodes report their device IDs via the HTTP POST method, and the MQTT topic management server generates a corresponding random topic. The control node retrieves the random topic corresponding to the device ID via the HTTP GET method.
[0044] Random string generator: Called by the MQTT topic management server, it generates random MQTT topics and ensures that the generated random strings are unique.
[0045] The device communication method based on the MQTT protocol provided in this embodiment of the invention can be run on... Figure 1 On the network architecture shown, Figure 2 This is a flowchart of a method according to an embodiment of the present invention, such as... Figure 2 As shown, the process includes the following steps:
[0046] Step S201: The MQTT topic management server receives a first message reported by the controlled node, wherein the first message includes the device ID of the controlled node;
[0047] Step S202: The MQTT topic management server generates a random topic corresponding to the device ID of the controlled node and returns the random topic to the controlled node.
[0048] In step S203, after authenticating the control node, the MQTT topic management server sends the random topic to the control node so that the control node and the controlled node can communicate via the MQTT protocol through the random topic.
[0049] In one exemplary embodiment, the MQTT topic management server receives the first message reported by the controlled node via the HTTP POST method.
[0050] In an exemplary embodiment, the MQTT topic management server calls a random string generator to generate a first random string as the random topic, and caches the mapping between the device ID of the controlled node and the first random string; the MQTT topic management server returns the first random string to the controlled node.
[0051] In one exemplary embodiment, after the MQTT topic management server returns the first random string to the controlled node, the method further includes: the MQTT topic management server sending a first subscription message to the MQTT broker server, wherein the subscription topic in the first subscription message is a will topic, and the will topic includes the first random string and a fixed suffix.
[0052] In an exemplary embodiment, after the MQTT topic management server returns the first random string to the controlled node, the method further includes: the controlled node sending an MQTT connection message to the MQTT broker server, wherein the will topic in the MQTT connection message includes the first random string and a fixed suffix.
[0053] In one exemplary embodiment, the method further includes: after the controlled node successfully establishes an MQTT connection, sending a second subscription message to the MQTT broker server, wherein the subscription topic in the second subscription message is the first random string.
[0054] In an exemplary embodiment, before the MQTT topic management server sends the random topic to the control node, the method further includes: the MQTT topic management server receiving an authentication message reported by the control node via HTTP POST, the authentication message including the username and password required for authentication by the control node; the MQTT topic management server sending a first reply message to the control node, wherein a cookie is set as an authentication marker in the first reply message; the MQTT topic management server receiving a second message sent by the control node, wherein the second message includes the device ID of the controlled node and the returned cookie; the MQTT topic management server confirming that the node sending the message is the control node based on the cookie in the second message, and searching for the first random string in the cached mapping between device IDs and first random strings based on the device ID in the message, generating a second random string, and returning the first random string and the second random string to the control node.
[0055] In one exemplary embodiment, after returning the first random string and the second random string to the control node, the method further includes: the control node sending a third subscription message to the MQTT broker server, wherein the subscription topic in the third subscription message is the second random string; after receiving a successful subscription reply from the MQTT broker server, the control node sending a first publish message to the MQTT broker server, wherein the MQTT topic of the first publish message is the first random string, and the message content includes a management message and the second random string.
[0056] In one exemplary embodiment, after the MQTT broker server receives the first publish message, the method further includes: the MQTT broker server forwarding the first publish message to the controlled node; the controlled node, after executing a management message based on the first publish message, sending a second publish message to the MQTT broker server, wherein the MQTT subject of the second publish message is the second random string, and the message content is a reply to the management message; after receiving the second publish message, the MQTT broker server forwards the second publish message to the control node based on the MQTT subject being the second random string.
[0057] In one exemplary embodiment, the method further includes: if the MQTT broker server discovers that the controlled node is offline, it sends a third publication message to all nodes that have subscribed to the will topic, the message content of the third publication message being that the controlled node is offline.
[0058] In one exemplary embodiment, the method further includes: after receiving the third MQTT publish message, the MQTT topic management server deletes the cached mapping between the device ID of the controlled node and the first random string according to a random topic.
[0059] In one exemplary embodiment, the method further includes: an MQTT topic management server receiving an authentication message reported by the control node via HTTP POST, the authentication message including the username and password required for the control node authentication; the MQTT topic management server sending a first reply message to the control node, wherein a cookie is set as an authentication marker in the first reply message; the MQTT topic management server receiving a second message sent by the control node, wherein the second message includes the device ID of the controlled node and the returned cookie; the MQTT topic management server confirming that the node sending the message is the control node based on the cookie in the second message, and searching for the first random string in the cached mapping between device IDs and first random strings based on the device ID in the message; if not found, sending a second reply message to the control node, wherein the content of the second reply message is "The controlled node could not be found".
[0060] In an exemplary embodiment, after the MQTT topic management server sends the second reply message to the control node, the method further includes: the control node, based on the received second reply message, discovers that the controlled node is offline, and then abandons the management of the controlled node.
[0061] In the above embodiments of the present invention, the MQTT topics for node communication are managed by an MQTT topic management server, and the MQTT topics are randomized so that malicious network nodes cannot listen to or interfere with node communication, thereby improving the security of inter-node communication in the MQTT protocol network.
[0062] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0063] This embodiment also provides an MQTT topic management server and a controlled node for implementing the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0064] Figure 3 This is a structural block diagram of an MQTT topic management server according to an embodiment of the present invention, such as... Figure 3 As shown, the MQTT topic management server 100 includes a first receiving module 10, a generating module 20, and an authentication module 30.
[0065] The first receiving module 10 is used to receive a first message reported by the controlled node, wherein the first message includes the device ID of the controlled node.
[0066] The generation module 20 is used to generate a random topic corresponding to the device ID of the controlled node and return the random topic to the controlled node.
[0067] The authentication module 30 is used to send the random topic to the control node after authenticating the control node, so that the control node and the controlled node can communicate via the MQTT protocol through the random topic.
[0068] Figure 4 This is a structural block diagram of a controlled node according to an embodiment of the present invention, such as... Figure 4 As shown, the controlled node 200 includes a sending module 40, a second receiving module 50, and a communication module 60.
[0069] The sending module 40 is used to report a first message to the MQTT topic management server, wherein the first message includes the device ID of the controlled node.
[0070] The second receiving module 50 is used to receive a random topic generated by the MQTT topic management server that corresponds to the device ID of the controlled node.
[0071] The communication module 60 is used to communicate with the control node via the random topic after the control node has been authenticated by the MQTT topic management server and obtained the random topic from the MQTT topic management server.
[0072] It should be noted that the above modules can be implemented by software or hardware. For the latter, they can be implemented in the following ways, but are not limited to: all the above modules are located in the same processor; or, the above modules are located in different processors in any combination.
[0073] To facilitate understanding of the technical solutions provided by this invention, the following will describe them in detail with reference to specific application scenarios:
[0074] Example 1
[0075] This embodiment provides a device communication method based on the MQTT protocol. In this embodiment, the controlled node accesses the MQTT network and is managed by the control node. Figure 5 As shown, the method of this implementation includes the following steps:
[0076] In step S501, the controlled node sends an HTTP POST message (Message 1) to the MQTT topic management server. This message contains the node device ID.
[0077] In step S502, after receiving message 1, the MQTT topic management server calls the random string generator to generate random string 1 and caches the mapping between the device ID and random string 1. Then, it returns random string 1 as a reply to message 1 to the controlled node.
[0078] In step S503, the MQTT topic management server then sends an MQTT Subscribe message (message 2) to the MQTT broker server, subscribing to the topic: random string 1 + "Last Will". For example, if the random string is: randomString, then the subscribed topic is: random String Last Will.
[0079] In step S504, the controlled node receives the reply to message 1, sends an MQTT CONNECT message (message 3) to the MQTT broker server, and sets the Will Topic in the CONNECT message to a random string 1 + "Last Will", and the Will Message to "offline".
[0080] Step S505: After the controlled node successfully connects to the MQTT, it sends an MQTT Subscribe message (message 4) to the MQTT broker server, with the subscription topic being: random string 1.
[0081] In step S506, the control node connects to the network and sends an HTTP POST message (Message 5) to the MQTT topic management server. The message content contains the username / password required for control node authentication. Upon receiving Message 5, the MQTT topic management server authenticates the username / password and sets a cookie in its response to Message 5 as an authentication marker.
[0082] In step S507, the control node needs to establish a connection with the controlled node for management. First, the control node sends an HTTP POST message (message 6) to the MQTT topic management server. The message content includes the controlled node's device ID and the cookie returned in message 5.
[0083] In step S508, the MQTT topic management server receives message 6, recognizes the node that sent the message as the control node based on the cookie in the message, and then searches for the corresponding random string 1 in the mapping between device IDs and random string 1 in its own server based on the device ID in the message. At the same time, it generates random string 2. Then, it returns random string 1 and random string 2 as a reply to message 6 to the control node.
[0084] In step S509, the control node sends an MQTT Subscribe message (message 7) to the MQTT broker server, subscribing to the topic "random string 2".
[0085] In step S510, after receiving a successful subscription reply from the MQTT broker server, the control node sends an MQTT Publish message to the MQTT broker server (message 8). The MQTT subject of this message is random string 1, and the message content is management message + random string 2. Upon receiving message 8, the MQTT broker server, because the MQTT subject is random string 1, will forward this message to the controlled node according to the MQTT protocol function.
[0086] In step S511, after receiving message 8 and executing the management message, the controlled node sends a Publish message (message 9) to the MQTT broker server. The MQTT subject of this message is random string 2, and the message content is a reply to the management message.
[0087] In step S512, the MQTT broker server receives message 9. Because the MQTT topic is random string 2, it forwards this message to the control node according to the MQTT protocol. The control node receives message 9, completing the sending and receiving of management messages.
[0088] Examples of messages in this embodiment are shown in Table 1:
[0089] Table 1
[0090]
[0091] Example 2
[0092] This embodiment also provides a device communication method based on the MQTT protocol. The difference from Embodiment 1 is that in this embodiment, the controlled node is offline, and the controlling node cannot manage the controlled node.
[0093] In this embodiment, the initial state of each node is between steps S506 and S507 in embodiment 1. At this time, both the controlled node and the control node have been initialized, but the control node has not yet started connecting to the controlled node.
[0094] like Figure 6 As shown, the method in this embodiment mainly includes the following steps:
[0095] In step S601, the controlled node goes offline. According to the MQTT protocol, the MQTT broker server discovers that the controlled node has lost connection and sends an MQTT Publish message (Message 1) to all nodes that have subscribed to the topic of random string 1 + "LastWill". The message content is "offline".
[0096] In step S602, because the MQTT topic management server subscribed to the topic "random string 1 + "LastWill" during the random topic process on the controlled node, the MQTT topic management server will receive message 1. Based on random topic 1, the random MQTT topic management server deletes the mapping relationship between the device ID and random string 1.
[0097] In step S603, the control node establishes a connection with the controlled node for management. The control node sends an HTTP POST message (message 2) to the MQTT topic management server. The message content includes the controlled node's device ID and authentication cookie.
[0098] In step S604, the MQTT topic management server receives message 2. Based on the cookie in the message, it recognizes the node that sent the message as the control node. Then, based on the device ID in the message, it searches for the corresponding random string 1 in the mapping between device IDs and random string 1 on its own server, but fails to find it. It then replies to message 2 with the message content: Device not found.
[0099] In step S605, the control node receives a reply to message 2, discovers that the controlled node is offline, and then abandons this management task.
[0100] Examples of messages in this embodiment are shown in Table 2:
[0101] Table 2
[0102]
[0103] The above embodiments of the present invention extend a point-to-point secure communication scheme based on the MQTT protocol network, solving the point-to-point secure communication needs of IoT devices in the MQTT network due to configuration management / information collection and other reasons.
[0104] Embodiments of the present invention also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to perform the steps in any of the above method embodiments when executed.
[0105] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.
[0106] Embodiments of the present invention also provide an electronic device including a memory and a processor, the memory storing a computer program and the processor being configured to run the computer program to perform the steps in any of the above method embodiments.
[0107] In one exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0108] Specific examples in this embodiment can be found in the examples described in the above embodiments and exemplary implementations, and will not be repeated here.
[0109] It is obvious to those skilled in the art that the modules or steps of the present invention described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. They can be implemented using computer-executable program code, and thus can be stored in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those described herein, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the present invention is not limited to any particular combination of hardware and software.
[0110] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, or improvements made within the principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A device communication method based on the MQTT protocol, applied to an MQTT topic management server, characterized in that, include: Receive a first message reported by the controlled node, wherein the first message includes the device ID of the controlled node; Generate a random topic corresponding to the device ID of the controlled node, and return the random topic to the controlled node; After authenticating the control node, the random topic is sent to the control node so that the control node and the controlled node can communicate via the MQTT protocol through the random topic; The MQTT topic management server generates a random topic corresponding to the device ID of the controlled node and returns the random topic to the controlled node, including: The MQTT topic management server calls a random string generator to generate a first random string as the random topic, and caches the mapping between the device ID of the controlled node and the first random string; The MQTT topic management server returns the first random string to the controlled node; Before the MQTT topic management server sends the random topic to the control node, it also includes: The MQTT topic management server receives the authentication message reported by the control node via HTTP POST, and the authentication message includes the username and password required for the control node to authenticate. The MQTT topic management server sends a first reply message to the control node, wherein a cookie is set in the first reply message as an authentication marker; The MQTT topic management server receives a second message sent by the control node, wherein the second message contains the device ID of the controlled node and the cookie returned in the first reply message; The MQTT topic management server confirms that the node that sent the message is the control node based on the cookie in the second message, and looks up the first random string in the cached mapping between device ID and first random string based on the device ID in the message, generates a second random string, and returns the first random string and the second random string to the control node.
2. The method according to claim 1, characterized in that, The first message received by the MQTT topic management server from the controlled nodes includes: The MQTT topic management server receives the first message reported by the controlled node via the HTTP POST method.
3. The method according to claim 1, characterized in that, After the MQTT topic management server returns the first random string to the controlled node, the process further includes: The MQTT topic management server sends a first subscription message to the MQTT broker server, wherein the subscription topic in the first subscription message is a will topic, and the will topic includes the first random string and a fixed suffix.
4. The method according to claim 3, characterized in that, After the MQTT topic management server returns the first random string to the controlled node, it further includes: The controlled node sends an MQTT connection message to the MQTT broker server, wherein the will subject in the MQTT connection message includes the first random string and a fixed suffix.
5. The method according to claim 4, characterized in that, Also includes: After the controlled node successfully establishes an MQTT connection, it sends a second subscription message to the MQTT broker server. The subscription topic in the second subscription message is the first random string.
6. The method according to claim 1, characterized in that, After returning the first and second random strings to the control node, the method further includes: The control node sends a third subscription message to the MQTT broker server, wherein the topic of the subscription in the third subscription message is the second random string; After receiving a successful subscription reply from the MQTT broker server, the control node sends a first publish message to the MQTT broker server. The MQTT subject of the first publish message is the first random string, and the message content includes a management message and a second random string.
7. The method according to claim 6, characterized in that, After receiving the first publish message, the MQTT broker server also includes: The MQTT broker server forwards the first published message to the controlled node; After the controlled node executes the management message according to the first published message, it sends a second published message to the MQTT broker server, wherein the MQTT subject of the second published message is the second random string, and the message content is a reply to the management message; After receiving the second publish message, the MQTT broker server forwards the second publish message to the control node based on the MQTT topic being the second random string.
8. The method according to claim 5, characterized in that, Also includes: If the MQTT broker server detects that the controlled node is offline, it sends a third publication message to all nodes that have subscribed to the will topic. The message content of the third publication message is that the controlled node is offline.
9. The method according to claim 8, characterized in that, Also includes: After receiving the third publish message, the MQTT topic management server deletes the cached mapping between the device ID of the controlled node and the first random string according to the random topic.
10. The method according to claim 9, characterized in that, Also includes: The MQTT topic management server receives the authentication message reported by the control node via HTTP POST, and the authentication message includes the username and password required for the control node to authenticate. The MQTT topic management server sends a first reply message to the control node, wherein a cookie is set in the first reply message as an authentication marker; The MQTT topic management server receives a second message sent by the control node, wherein the second message contains the device ID of the controlled node and the returned cookie; The MQTT topic management server confirms that the node that sent the message is the control node based on the cookie in the second message, and searches for the first random string in the cached mapping between device ID and first random string based on the device ID in the message. If it cannot find it, it sends a second reply message to the control node, wherein the content of the second reply message is "The controlled node could not be found".
11. The method according to claim 10, characterized in that, After the MQTT topic management server sends the second reply message to the control node, it also includes: If the control node finds that the controlled node is offline based on the received second reply message, it will abandon the management of the controlled node.
12. A device communication method based on the MQTT protocol, applied to a controlled node, characterized in that, include: Report a first message to the MQTT topic management server, wherein the first message includes the device ID of the controlled node; Receive a random topic generated by the MQTT topic management server, corresponding to the device ID of the controlled node; After the control node is authenticated by the MQTT topic management server and obtains the random topic from the MQTT topic management server, the controlled node and the control node communicate using the MQTT protocol through the random topic. The device communication method based on the MQTT protocol further includes: receiving a first publish message sent by an MQTT broker server, and after executing a management message according to the first publish message, sending a second publish message to the MQTT broker server, wherein the MQTT subject of the second publish message is a second random string, and the message content is a reply to the management message.
13. An MQTT topic management server, characterized in that, include: The first receiving module is configured to receive a first message reported by the controlled node, wherein the first message includes the device ID of the controlled node; A generation module is used to generate a random topic corresponding to the device ID of the controlled node and return the random topic to the controlled node; An authentication module is used to send the random topic to the control node after authenticating the control node, so that the control node and the controlled node can communicate via the MQTT protocol through the random topic; The MQTT topic management server is also used to call a random string generator to generate a first random string as the random topic, and cache the mapping between the device ID of the controlled node and the first random string; and return the first random string to the controlled node. The MQTT topic management server is further configured to receive an authentication message reported by the control node via HTTP POST, the authentication message including the username and password required for the control node authentication; send a first reply message to the control node, wherein a cookie is set as an authentication marker in the first reply message; receive a second message sent by the control node, wherein the second message includes the device ID of the controlled node and the cookie returned in the first reply message; confirm that the node sending the message is the control node based on the cookie in the second message, and search for the first random string in the cached mapping between device IDs and first random strings based on the device ID in the message, generate a second random string, and return the first random string and the second random string to the control node.
14. A controlled node, characterized in that, include: The sending module is used to report a first message to the MQTT topic management server, wherein the first message includes the device ID of the controlled node; The second receiving module is used to receive a random topic generated by the MQTT topic management server that corresponds to the device ID of the controlled node; The communication module is used to communicate with the control node via the random topic after the control node has been authenticated by the MQTT topic management server and obtained the random topic from the MQTT topic management server; The controlled node is also configured to receive a first publish message sent by the MQTT broker server, and after executing the management message according to the first publish message, send a second publish message to the MQTT broker server, wherein the MQTT subject of the second publish message is a second random string, and the message content is a reply to the management message.
15. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the method described in any one of claims 1 to 11.
16. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method described in any one of claims 1 to 11.