MQTT (Message Queuing Telemetry Transport) is a "lightweight" communication protocol based on publish/subscribe (publish/subscribe) mode. The protocol was built on the TCP/IP protocol. As a low-overhead, low-bandwidth instant messaging protocol, it has a wide range of applications in the Internet of Things, small devices, and mobile applications.
In UIFlow, we can use MQTT function to realize communication and interaction between two or more COREs, thus achieving powerful remote control functions.
Select MQTT Service
To use the MQTT protocol for data interaction, you need server support. There are many third-party server platforms to choose from. The platform we demonstrate here is CloudMQTT.
When you have created service support on the platform, you will get some configuration information, such as server address, username, password, etc., which will be used in UIFlow's MQTT block.
In the advanced functions of UIFlow, you can find the MQTT function block. We can simply understand the MQTT protocol as two links, "Publish" and "Subscribe".
When the publisher posts a message, the subscriber will get this information to enable communication between devices
MQTT configuration block
Add an MQTT configuration block and connect to the Setup block
information to fill in
Fill in the server information on your personal or third-party server platform to prepare for the next connection
When you have multiple devices in use at the same time, the ID name (the ID in the figure below is "M5stack") is not allowed to repeat with other options in the configuration information, and it is not allowed to repeat with the ID of other devices. At the same time, the same In the server, only one device with the same ID name is allowed to be online. If you need to download a program to the device to run, you need to add a Wi-Fi connection program at the program initialization.
Add a Start block below the MQTT configuration block, which means that it will start running after the configuration information.
Publish refers to the link in the communication to publish data, including two parts for the content of the publication "topic", "content" (msg)
Set a publishing theme, when other devices want to get the content information under the theme, you need to subscribe to the matching theme name
Publish "Content" (msg)
Set content information to be published
When the program runs to the Publish release block, the message is released. example:
When the A button is pressed, a message is posted (the subject is "RGB" and the content is "open")
When the B button is pressed, a message is posted (the subject is "RGB" and the content is "close")
Subscribe subscription refers to the process of receiving data in the communication. When the publisher publishes the information, the subscriber will automatically receive the subscribed topic (message), message content (msg)
Set the topic to subscribe to
Get topic data "content" (msg)
Get the message content under this subscription
Add a Subscribe block, fill in the topic to be subscribed, use the Get topic data block to get, and process the analysis, for example:
When an "open" is obtained from Publish, the RGB bar is lit, and when "close" is obtained, the RGB bar is extinguished.
Use a CORE to program a simple use case to verify functionality, it’s not inly a publisher (Publish), but a subscriber (Subscribe) as well
When the A button is pressed, a message is posted (the subject is "RGB", the content is "open"), and the RGB bar lights up.
When the B button is pressed, a message is posted (the subject is "RGB", the content is "close"), and the RGB bar is extinguished.