MQTT communication

What is MQTT?

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 and was released by IBM in 1999.

The biggest advantage of MQTT is that it can provide real-time and reliable message service for connecting remote devices with very little code and limited bandwidth.

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.

UIFlow and MQTT

In UIFlow, we can use MQTT function to realize communication and interaction between two or more COREs, thus achieving powerful remote control functions.

MQTT server

  • 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.

  • MQTT fuction

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

Initialization procedure

  • 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

attention

  • When you have multiple devices at the same time, the ID name (the ID below is "M5stack") is not allowed to be duplicated with other options in the configuration information, and it is not allowed to duplicate the IDs of other devices. At the same time, the same server In the device with the same ID name, only one online is allowed.
  • MQTT Start

Add a Start block below the MQTT configuration block, which means that it will start running after the configuration information.

Publish

Function Description

Publish refers to the link in the communication to publish data, including two parts for the content of the publication "topic", "content" (msg)

  • Publish "topic"

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

Instructions

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

Function Description

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)

  • Subscribe "topic"

Set the topic to subscribe to

  • Get topic data "content" (msg)

Get the message content under this subscription

Instructions

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 Cases

Implementation function

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

  • complete program

Instructions

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.

On This Page