MQTT Publish

This function block is designed to publish messages to an MQTT broker. It allows users to specify broker parameters, topic, and message contents, enabling straightforward data communication.

📥 Inputs

Enable This boolean input enables or disables the publishing action.

Data This generic input allows users to send any data type to be published to the specified MQTT topic.

📤 Outputs

This function block does not produce any outputs.

🕹️ Controls

Broker Address Input field for specifying the MQTT broker address.

Port Input field for specifying the port to connect to the MQTT broker (default is 1883).

Topic Input field for defining the MQTT topic to which messages will be published.

Username Input field for the broker’s username, if authentication is required.

Password Input field for the broker’s password, if authentication is required.

Quality of Service Dropdown menu to specify the QoS level for message delivery (options are 0, 1, or 2).

Keep alive (s): Input field for the keep-alive interval.

Retain A switch to choose whether or not the published message should be retained by the broker.

🎨 Features

Easy Configuration Users can quickly configure MQTT broker settings and messaging parameters through the user interface.

Dynamic Connection Handling The block automatically reconnects to the MQTT broker when parameters change.

Error Logging Informative log messages provide feedback on the connection status and data publishing success.

📝 Usage Instructions

  1. Set Broker Address and Port: Enter the MQTT broker address and port number.

  2. Define Topic and Auth Credentials: Specify the topic for publishing and, if required, the username and password.

  3. Adjust Parameters: Set the Quality of Service (QoS) level and specify the keep-alive duration as needed.

  4. Connect and Enable: Enable the block and connect it to data inputs to start publishing.

  5. Monitor Logs: Check logs for connection status and success message on data publishing.

📊 Evaluation

When run, this function block sends the provided data to the specified MQTT topic, logging success or any potential errors encountered during the process.

💡 Tips and Tricks

Using Quality of Service

Choose a higher QoS level (1 or 2) for more reliable delivery of important messages, especially in unstable network conditions.

Testing Connection

Before publishing data, test the connection separately, using the logs to diagnose potential connection issues.

Retaining Messages

Use the Retain feature carefully, as it will make the broker store the last published message for that topic, which may affect subscribers needing the most current data.

🛠️ Troubleshooting

MQTT Connection Issues

If you encounter connection errors, double-check the broker address and port number. Also, ensure that your network allows MQTT traffic.

Failed to Publish Data

If data cannot be published, ensure the MQTT client is connected. Check the log messages for any error codes that may indicate the problem.

Last updated