# MQTT Publish (TLS-Beta)

This function block sends data to an MQTT broker using optional TLS security. It provides settings for broker address, authentication, TLS mode, QoS, retain flag and keepalive—so you can publish formatted messages from your system to any MQTT topic.

## 📥 Inputs (sockets) <a href="#inputs" id="inputs"></a>

`Enable` Toggle to activate publishing. When this input is TRUE the block will attempt to send the provided data.

`Data` The data to publish. This generic socket accepts text or structured data (recommended to format as JSON or string).

## 📤 Outputs (sockets) <a href="#outputs" id="outputs"></a>

This function block does not produce any outputs.

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

`Broker Address` Enter the MQTT broker host or IP address.

`Port` Enter the broker port. Common default is 1883.

`Topic` The MQTT topic to publish messages to.

`Username` Optional username for broker authentication.

`Password` Optional password for broker authentication (masked in the UI).

`TLS Mode Selection` Choose TLS behaviour. Options include `None`, `Auto` (use system/root certificate), and specific TLS versions with a custom certificate file.

`Cert File Path` Optional path to a .crt file when using custom TLS configuration.

`Quality of Service` Choose message delivery level (0, 1 or 2).

`Keep alive (s)` Set the keepalive interval in seconds.

`Retain` Switch to publish retained messages on the broker.

## 🎯 Key features <a href="#features" id="features"></a>

* Secure publishing with selectable TLS options, including auto certificate handling or custom certificate file.
* Authentication support with username/password.
* QoS and retain flags for delivery guarantees and persistent messages.
* Automatic reconnect attempts when connection is lost and simple status logging to help diagnose issues.
* Simple UI controls for all common MQTT parameters—no programming required.

## 📝 How to use <a href="#usage" id="usage"></a>

1. Fill `Broker Address` and `Port` to point to your MQTT broker.
2. Set `Topic` for the target messages.
3. If needed, provide `Username` and `Password`. Leave them empty for anonymous brokers.
4. Pick `TLS Mode Selection`. Use `Auto` for standard secure connections without supplying a certificate file, or provide `Cert File Path` if your broker requires a specific .crt file.
5. Configure `Quality of Service`, `Keep alive (s)` and enable `Retain` if you want messages stored on the broker.
6. Feed the data you want to send into the `Data` input. Use `Enable` to trigger sending (TRUE to enable).
7. Monitor logs shown by the block for connection and publish status.

## 📊 What happens when it runs <a href="#evaluation" id="evaluation"></a>

When enabled, the block verifies connection parameters and attempts to connect or reconnect to the broker if necessary. Once connected, it publishes the provided `Data` to the configured `Topic` using the selected QoS and retain settings. Connection problems and publish results are surfaced via the block’s status/log messages.

## 💡 Tips and Tricks <a href="#tips-and-tricks" id="tips-and-tricks"></a>

* Combine with `Data to JSON` to convert multiple data fields into a single JSON message suitable for IoT dashboards.
* Use `String Merge` when you want to build a custom text message (for example merging device id + status).
* Add `Date-Time` to include timestamps in your messages for logging or timeline plots.
* Trigger publishing on events by coupling this block with `Logic Input` or `Rising Edge` so messages are only sent when something meaningful happens.
* Use `MQTT Subscribe (TLS-Beta)` in a separate instance of the same block type to verify messages are received on the broker (handy for end-to-end checks).
* Before sending image-heavy data, consider using `Image Logger` or `Image Write` to save images locally and publish only metadata or a file path via MQTT to reduce bandwidth.

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

* Connection failed: check `Broker Address`, `Port`, firewall and network reachability. Try a simple MQTT client to confirm broker is reachable.
* Certificate/TLS errors: switch `TLS Mode Selection` to `Auto` if you do not have a certificate file, or supply the correct `Cert File Path` when custom TLS is required.
* Authentication failure: verify `Username` and `Password` with your broker.
* Messages not visible on broker: confirm `Topic`, QoS and `Retain` settings; use `MQTT Subscribe (TLS-Beta)` to monitor the topic locally.
* Unexpected data format: preview or log your message before publishing using `Debug Input`, `String Merge`, or `Data to JSON` to ensure the format matches your subscribers’ expectations.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.augelab.com/function-blocks/input-output/communication/mqtt-publish.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
