# Publish to an MQTT broker

> Publish an eaxPLC's Unified Namespace straight to an MQTT broker, as JSON topics or as a Sparkplug B edge node, without eaxScada in between; publishing never runs on the scan, and writes from the broker are off by default.

Product: eaxPLC · Type: How-to guide · Editions: Free, Pro
Source: https://eaxware.com/docs/eaxplc/how-to/publish-to-an-mqtt-broker/
Status: migrated page, not yet verified against the current code.

## Goal

The PLC's variables reach the plant's MQTT broker with the same paths, values and quality as on the NATS Unified Namespace, directly from the PLC.

## Before you start

- You are a Lead Engineer or Administrator: every change of these settings is signed with your password and recorded in the PLC's audit trail.
- You know the broker's URL (`tcp://`, `ssl://`, `ws://` or `wss://`), and, if it asks for them, a user name and password or a client certificate.
- Publishing is off until you switch it on. A simulated PLC never opens an MQTT connection.

## Steps

1. Open **File > MQTT Publishing**.
2. Under **Broker**, tick **Publish to an MQTT broker** and enter one broker URL per line. Enter the user name and password if the broker needs them. The password is never shown again: the page says **Set** or **Not set**, and an empty field keeps it.
3. For `ssl://` or `wss://`, paste the broker's CA certificates under **TLS** (empty: the system's), and a client certificate with its private key if the broker identifies clients by certificate.
4. Under **Topics**, choose **JSON topics** or **Sparkplug B**. The topic root defaults to the PLC's UNS prefix. **Variables published** takes patterns such as `Tank*, Line2.*`; empty publishes every variable the NATS UNS publishes (strings are not published).
5. Click **Test connection**. The PLC connects to the broker and disconnects; nothing is published.
6. Click **Sign and save…**, give a reason and your password. The PLC connects with the new settings at once.

The settings are kept in `data/mqtt.json`; the password and the client key in `data/mqtt/secrets.json`, readable by the PLC's account only. A headless eaxPLC runtime reads the same files.

## Topic layout

JSON topics:

| Topic | Payload |
| --- | --- |
| `<root>/<variable>` | `{"path","value","quality","timestamp","device_id","address"}`, the NATS UNS payload. FB members and array elements are levels: `Timer1.Q` is `<root>/Timer1/Q`, `Recipe[3]` is `<root>/Recipe/3`. |
| `<root>/system/Fault` | 1 while the PLC is in a fault state, 0 otherwise. |
| `<root>/system/online` | `true` while the PLC is connected; the broker publishes `false` when the connection breaks (last will). |

Values are published when they change and every 30 seconds (setting **Republish every value every**), retained by default so a new subscriber gets the current state at once.

## Sparkplug B notes

- The PLC is one edge node (group id and edge node id: by default the first level of the topic root and the PLC id), with its variables as node metrics. There are no devices.
- Metric names are the UNS paths (`<root>/<variable>`). The NBIRTH gives each an alias and a datatype from the PLC type (BOOL Boolean, INT Int16, DINT Int32, REAL Float, LREAL Double, and so on); NDATA carries aliases only.
- Every metric carries the property `Quality` (Int32: 192 Good, 64 Uncertain while the PLC is stopped or faulted, or before its first cycle).
- The NDEATH registered as the connection's will carries the bdSeq of the NBIRTH it ends. seq runs from 0 (NBIRTH) to 255 and wraps.
- After every reconnection an NBIRTH comes before any NDATA. Values buffered while the broker was away follow it as NDATA marked historical.
- `Node Control/Rebirth` in an NCMD always republishes the NBIRTH, whether commands are on or off. A download that adds or removes variables also rebirths.
- With a primary host id, the PLC publishes nothing until that host's STATE says online, and ends the session (NDEATH) when it says offline.
- In eaxScada, an MQTT channel's browse offers each metric by its full address, `spBv1.0/<group>/NDATA/<edge node>/<metric>`. A write by that address goes to this edge node only, never to a metric of the same name on another node. Writes are refused after the node's NDEATH until its next NBIRTH, and a metric no longer in the latest NBIRTH reads Bad and cannot be written.

## When the broker is away

Publishing runs beside the scan, never in it: a slow or unreachable broker never lengthens a cycle. Values that change while the broker is away wait in a buffer of **Buffer while the broker is away** messages (default 10 000); when it is full the oldest go first, and the page counts them under **Dropped**. The diagnostics (state, last error, messages sent, buffer, drops, bdSeq and seq) refresh on the page while it is open.

## Commands from the broker

:::danger
With **Accept writes from the broker** on, any client the broker lets publish on the command topics can write PLC variables. Switch it on only for a broker whose publish permissions you control.
:::

Commands are off by default. When they are on:

- JSON: publish a number, `true`/`false`, a string, or `{"value": x}` on `<root>/command/<variable>`. Sparkplug B: an NCMD with the metric's name or alias.
- Each write is checked like a UNS command: only the variables the writable patterns (`--uns-writable`) allow, with read/write access, not a CONSTANT, within the write range. A variable on the safety-target list (Project Settings), or any variable sharing its storage, is never written over MQTT.
- An accepted write is applied at the next cycle boundary, never forced. Accepted and refused writes are recorded in the PLC's audit trail (as `MQTT`) and in the log.
- A retained command message is ignored, so a write never replays when the PLC reconnects.

## Result

The page shows **Connected** and the messages sent grow. A subscriber to `<root>/#` (JSON) or `spBv1.0/<group>/#` (Sparkplug B) sees the PLC's values.
