Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 ยท Communication Protocols

OPC UA and MQTT for PLC Data

Learn how OPC UA and MQTT move PLC data to SCADA, MES and the cloud: nodes, browsing, subscriptions, topics, QoS, retained messages, with runnable C models.

Older protocols such as Modbus send raw numbers with no context: register 40012 might be a temperature or a counter, and only the manual knows. Modern plants want data that describes itself and flows securely to SCADA, MES, historians and the cloud. Two protocols dominate that job: OPC UA and MQTT. They solve related problems in different ways, and many PLCs support both.

Neither an OPC UA server nor an MQTT broker is available on this site. The lesson shows real structures read-only and uses C stand-ins to model the core ideas: an address space tree and topic matching.

OPC UA in brief

OPC UA (Unified Architecture) is a platform-independent standard for industrial data exchange. Its main ideas:

  • Address space. The server exposes a tree of nodes: objects, variables and methods. A node has an identifier (ns=2;s=Tank1.Level), a browse name, a data type and attributes such as value, engineering unit and range.
  • Self-description. A client can browse the tree without a manual and discover what is available, including units and data types.
  • Services. Read, write, browse, call a method, and subscribe.
  • Subscriptions and monitored items. The client asks to be told when a value changes by more than a deadband, at a sampling interval. This is the report-by-exception idea from SCADA.
  • Security built in. Certificates, signing and encryption, user authentication and authorization.
  • Transport. Binary over TCP (opc.tcp, port 4840 by default) or HTTPS; a publish/subscribe extension (PubSub) also exists.
flowchart LR
    PLC["PLC with<br/>OPC UA server"] -- "browse, read, subscribe" --> C1["SCADA client"]
    PLC --> C2["MES client"]
    PLC --> C3["Historian client"]

Clients connect to the server and ask for what they need.

Modelling an address space

The next program stores a small tree of nodes and a browse function that prints it, like a client browsing a server. It is a stand-in for the concept, not the protocol.

C C stand-in: browse a tree of nodes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The client learns names, kinds, units and values from the server itself. That is what makes OPC UA data "self-describing".

MQTT in brief

MQTT is a lightweight publish/subscribe protocol. Instead of clients connecting to each device, everyone connects to a broker:

  • A device publishes a message to a topic, such as plant1/line2/filler/temperature.
  • Any client that subscribes to a matching topic receives it.
  • Publisher and subscriber never know about each other, which makes it easy to add consumers.

Topic filters use wildcards: + matches exactly one level, # matches all remaining levels. So plant1/+/filler/temperature matches temperature on every line, while plant1/# matches everything under plant1.

Other features that matter for plants:

  • QoS 0 (at most once), 1 (at least once), 2 (exactly once).
  • Retained messages: the broker keeps the last message of a topic and gives it to new subscribers immediately, so a dashboard shows the current value at once.
  • Last will: a message the broker publishes if a client disconnects unexpectedly, handy for "device offline".
  • Payload: MQTT does not define it. Sparkplug B is a popular convention that standardizes topics and payloads for industrial use.

Topic matching in C

A subscriber's filter is matched against each published topic. This stand-in implements + and # matching for a few topics.

C C stand-in: MQTT topic filter matching

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Which one when?

OPC UA MQTT
Model Client/server with a rich address space Publish/subscribe through a broker
Strength Structure, types, methods, security, interoperability Lightweight, scales to many devices, easy cloud links
Typical use PLC to SCADA, MES, between machines Edge to cloud, many sensors, dashboards
Data meaning Defined by the information model Defined by your topic and payload convention

They are often combined: an edge gateway reads PLC data via OPC UA and publishes it over MQTT to the cloud.

Practical advice

  • Expose only what is needed, with read-only access where possible.
  • Use certificates and encryption for both; never send plant data over plain MQTT across the internet.
  • Agree on a naming scheme and units early.
  • Consider the load: subscribing to thousands of nodes at 10 ms intervals can overload a small PLC. Use deadbands and sensible sampling.
  • Network hardware and the lower layers are covered in the overview; the real-time Ethernet protocols come next in PROFINET, EtherNet/IP and I/O mapping.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does it mean that OPC UA is self-describing?

    The address space lets clients discover what exists and how to interpret it, without a separate manual.
  2. 2. In MQTT, what does the filter plant1/+/temperature match?

    The + wildcard matches exactly one level between plant1 and temperature.
  3. 3. What does a retained MQTT message do?

    Retained messages let a newly connected dashboard see the current value without waiting for the next publish.
  4. 4. Which pairing is most typical?

    OPC UA gives structured access near the machine; MQTT scales out to many consumers and the cloud.

Summary

  • OPC UA offers a browsable address space, typed nodes, subscriptions and built-in security.
  • MQTT is a lightweight publish/subscribe protocol with topics, wildcards, QoS, retained messages and last will.
  • OPC UA suits structured access to controllers; MQTT suits scale and cloud links; gateways combine them.
  • Secure both, agree on naming and units, and keep subscription load reasonable.
  • The C programs are stand-ins modelling nodes and topic matching.

Next: PROFINET, EtherNet/IP and I/O Mapping