# 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 <a href="#inputs" id="inputs"></a>

`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 <a href="#outputs" id="outputs"></a>

This function block does not produce any outputs.

## 🕹️ Controls <a href="#controls" id="controls"></a>

`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 <a href="#features" id="features"></a>

`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 <a href="#usage" id="usage"></a>

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 <a href="#evaluation" id="evaluation"></a>

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 <a href="#tips-and-tricks" id="tips-and-tricks"></a>

<details>

<summary>Using Quality of Service</summary>

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

</details>

<details>

<summary>Testing Connection</summary>

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

</details>

<details>

<summary>Retaining Messages</summary>

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.

</details>

## 🛠️ Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

<details>

<summary>MQTT Connection Issues</summary>

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

</details>

<details>

<summary>Failed to Publish Data</summary>

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

</details>
