Skip to main content

MQTT client

An MQTT client connector reads tags from MQTT brokers (Mosquitto, HiveMQ, EMQX, AWS IoT, Azure IoT Hub and others). One connector can hold several brokers: each broker is a device of the connector, with its own connection, status and tags, and counts as one device for a licence with a device limit.

An MQTT client connector with a broker

SettingMeaning
Host, PortThe broker's host name or IP address. 1883 for plain MQTT, 8883 with TLS
MQTT version3.1.1 (most brokers) or 5
Client IDMust be unique on the broker. Empty: wiconnector-<connector>-<broker>
SessionClean starts fresh on every connect. Persistent asks the broker to keep QoS 1 and 2 messages while WiConnector is away
Sign inAnonymous, or user and password (stored encrypted)
Keep alive, Timeout, Reconnect afterHow often the connection is checked, how long to wait for the broker, and the wait before connecting again after a lost connection
TLSEncrypts the connection. CA certificate: for a broker whose certificate comes from your own CA (empty: the computer's trusted CAs). Client certificate and Client private key: for brokers that sign clients in by certificate (AWS IoT, Azure IoT Hub). Broker certificate: Verify (recommended) or Accept any (self-signed brokers on a closed network). Browse uploads the files from your computer
Discover topics, listens forWhat Browse listens to (# for everything, plant/# for one branch) and for how long

Subscription groups take the place of poll groups: brokers push messages, so there is no rate. A group sets the QoS the tags subscribe with (0, 1 or 2) and Stale after: a value with no new message for that many seconds goes Uncertain (reason Stale); 0 never. Leave it at 0 for topics that only send changes, such as a retained state.

MQTT tag addresses​

The address is the topic, then optional parts separated by ;. Press the i next to the address for examples.

AddressReads
plant/line1/temperatureThe whole message: a number, true/false/on/off/1/0, or text for String tags
plant/line1/data; json=$.temperatureA field of a JSON message {"temperature": 21.5}
plant/line1/data; json=$.values[2]The third item of an array; $['field name'] for names with spaces or dots
sensors/tank1; json=$.level; ts=$.timeWith the time from the message (ISO 8601, or Unix seconds or milliseconds) instead of the time it arrived
plant/pump1/state; write=plant/pump1/cmdWrites publish the value to the command topic
plant/line1/setpoint; retain=trueWrites publish to the same topic as retained messages

A tag reads one topic: wildcards (+, #) are only for Discover. Writes send the value as plain text (21.5, true) with the subscription group's QoS. Before the first message a tag is Bad WaitingForData; when the broker is lost, its tags go Bad CommFailure and WiConnector reconnects. A message that does not fit the tag is Bad BadValue, NotJson or JsonPathNotFound.

Adding tags from a broker (Discover)​

In Tag Management, open a group and press Add from broker (or Browse next to a tag's address). WiConnector listens to the broker for a few seconds and lists every topic that sent a message, with each field of JSON messages as its own entry (plant/line1/status/motor/current becomes plant/line1/status; json=$.motor.current). Tick the topics or fields and press Add tags: each becomes a tag with a guessed type (numbers Float64, true/false Bool, other text String) that you can change. Retained messages show at once; topics that publish rarely only show if they send something while Discover listens, so raise Discover listens for if some are missing.

Sparkplug B (reading edge nodes)​

Sparkplug B settings of a broker

The MQTT client connector is also a Sparkplug B host application: it reads metrics from Sparkplug B edge nodes (Ignition, Cirrus Link, Tahu-based gateways, other WiConnectors with a Sparkplug publisher, and so on) on the same broker. A Sparkplug tag's address starts with spb::

AddressReads
spb:Plant/Edge1:SiteMetric Site of edge node Edge1 in group Plant (a node metric)
spb:Plant/Edge1/Line1:Motor/SpeedMetric Motor/Speed of device Line1 of that edge node

Everything after the colon is the metric name, so names with / work. You rarely type these: Add from broker lists Sparkplug edge nodes under spBv1.0 → group → edge node → device, with each metric's Sparkplug type and the tag type it becomes (Int8/Int16/UInt8 → Int16, Int32 → Int32, Float → Float32, Double/Int64/UInt64/DateTime → Float64, Boolean → Bool, String/Text → String). Discover asks edge nodes that only send data for a rebirth, so their names show.

How values arrive:

  1. Before the edge node's birth a tag is Bad WaitingForBirth. On connect WiConnector asks every edge node its tags read for a rebirth (NCMD Node Control/Rebirth), because births are not retained.
  2. NBIRTH / DBIRTH give each metric's name, alias, type and current value. A metric the birth does not have is Bad NotInBirth.
  3. NDATA / DDATA update the tags, by name or by alias (data usually only carries aliases). The metric's own timestamp is used, and its Quality property: 192 Good, 64 to 191 Uncertain, below 64 Bad. Historical metrics are skipped.
  4. DDEATH makes the device's tags Bad DeviceOffline; NDEATH makes all the edge node's tags Bad EdgeOffline. A death with an older bdSeq than the last birth is ignored (the node is already back).
  5. A gap in seq (lost messages), data before a birth, or an unknown alias asks for a rebirth again (at most every 5 seconds until a birth comes).

Writes to a writable Sparkplug tag send a command: NCMD for node metrics, DCMD for device metrics, with the metric's name, alias and the type from its birth. A write before the birth fails with No birth seen yet.

Each broker has a Sparkplug B section:

SettingMeaning
Rebirth requestsAsk for rebirths (default) as above, or Wait for births: never send commands to edge nodes, wait for their own births
Primary host IDOptional. When set (for example scada), WiConnector publishes the retained Sparkplug 3.0 spBv1.0/STATE/<id> {"online": true, ...} on connect, and online: false as its last will and when it stops. Set it only when edge nodes are told to wait for this host as their primary host

Sparkplug B tags use the subscription group's QoS and stale time like other MQTT tags. The simulator includes an edge node: group Sim, edge node Plant1, device Line1 (Speed, Count, Running, Temperature, and a writable Setpoint).