Skip to content

OPC UA and MQTT: how to collect data from machines

B2Bnetwork team · · 6 min

IT in manufacturing

In brief

OPC UA is a standard for exchanging data with machines and controllers, published as IEC 62541, with an information model (every variable has a name, type, unit and place in the machine's structure) and built-in security. MQTT is a lightweight publish/subscribe protocol that runs through a broker, an OASIS standard at version 5.0 since 2019, well suited to moving data from many gateways to a platform. The two do not compete: OPC UA usually reads data from the machine, MQTT carries it onward, and OPC UA PubSub can use MQTT as its transport.

What OPC UA is

OPC UA (Open Platform Communications Unified Architecture) is developed by the OPC Foundation and published as an international standard in the IEC 62541 series. It most often runs in a client/server model: an OPC UA server in the controller or the machine's PC exposes data, and a client (a gateway, SCADA, MES) reads it, subscribes to changes or calls methods.

The most important feature of OPC UA is its information model. The client does not receive a raw register at an address, but an object “Injection molding machine 3” with nodes “Zone 2 temperature” in degrees Celsius, “Cycle counter” and “State”. The client can browse this structure like a directory, without documentation from the vendor. The OPC Foundation lists more than 150 companion specifications that describe common models for whole groups of machines, such as injection molding machines or machine tools. Thanks to them, machines from different vendors expose the same data under the same names.

Security is part of the protocol: every client and server has an X.509 certificate, the session can be signed and encrypted, and the user authenticated. Having these mechanisms is not enough on its own, because they have to be switched on, as discussed below.

What MQTT is

MQTT is a publish/subscribe protocol. Devices do not talk to each other directly. They publish messages on topics at a broker, for example `plant1/hall2/line4/press3/temperature`, and consumers subscribe to the topics they care about. The publisher does not need to know who reads its data or how many systems do.

The OASIS MQTT 5.0 specification defines three delivery quality levels (QoS):

  • QoS 0, “at most once”: the message may be lost, fine for frequent measurements where losing one does not matter;
  • QoS 1, “at least once”: the message will arrive but may arrive twice, so the consumer must be able to discard duplicates;
  • QoS 2, “exactly once”: the slowest, for events that must not be lost or duplicated.

Two other features are useful in a plant: retained messages, which give a new subscriber the last known state right away, and the Will message, which the broker publishes when a gateway disconnects abnormally. Version 5.0 added, among other things, session and message expiry, shared subscriptions, reason codes in acknowledgements and user properties.

MQTT says nothing about the meaning of the message content. Whether the payload is a number, JSON or a binary format, and what field `t` means, is up to the team to agree.

OPC UA or MQTT: a comparison

FeatureOPC UAMQTT
Communication modelclient/server, optionally PubSubpublish/subscribe through a broker
Data modelrich, with types, units and structurenone, the team defines the content
Securitycertificates, signing, encryption, users in the protocolTLS and authentication at the broker
Typical placemachine, controller, SCADA, MESgateway, data platform, cloud
Poor quality linksweaker, client/server sessiongood, small header and persistent sessions
Many consumers of the same dataeach client connects to the server separatelythe broker delivers to all subscribers

A typical data collection architecture

In a mid-sized plant a layered setup usually works best:

  1. Machine: a controller with an OPC UA server, or a gateway that translates the vendor protocol into OPC UA.
  2. Line gateway: an OPC UA client subscribes to the variables it needs, adds context (order, item) and buffers data when the link drops.
  3. MQTT broker in the plant: gateways publish in an agreed topic hierarchy, and MES, the OEE system and the historian subscribe to what they need.
  4. Data platform: data from the broker goes into a time series database or an event stream, for example Apache Kafka.

Part 14 of the OPC UA specification, PubSub, defines a mapping to MQTT with JSON or binary UADP encoding. That gives you the OPC UA information model inside messages carried by an ordinary MQTT broker. Before choosing this route, check that the controllers and the platform on the other side support it.

What to compute on the gateway and what further up is covered in the article on edge computing in manufacturing. Data collected this way feeds, among other things, predictive maintenance.

What to watch out for during rollout

  • No security mode. OPC UA servers often allow connections in mode None, without signing or encryption. Turn it off and manage certificates, including their expiry.
  • A broker without authentication. Each device should have its own credentials, connect over TLS and have permissions only for its own topics.
  • Writing to the controller. Data collection only needs read access. Grant permission to write setpoints deliberately and log every change.
  • Frequency. Reading a hundred variables every 100 ms on every machine loads the controller and the network. Decide for each signal how often it is really needed.
  • Names and units. One topic hierarchy and one naming dictionary for the whole plant, written down before the first machine is connected.
  • Broker load. Test the broker for performance with the target number of devices and encryption on, as in performance testing of a system with encrypted device communication.

The network where the gateways and broker run should be separated from the office network, as we describe in the article OT security in the plant.

Questions and answers

Will MQTT replace OPC UA?

No, because they solve different problems. OPC UA describes what machine data means and lets you read it securely. MQTT efficiently distributes messages between many publishers and consumers. In a typical plant a gateway reads data over OPC UA and publishes it over MQTT. Dropping OPC UA means you have to build and maintain the data model yourself.

Which protocol should I choose for older machines?

Older machines usually have neither OPC UA nor MQTT. A gateway with the right communication driver reads the vendor protocol or digital signals and exposes them as OPC UA or publishes them over MQTT. The choice depends on what the target system accepts. When the plant already has a broker, publishing straight to MQTT is often simpler.

Is MQTT secure?

The protocol itself does not encrypt data, but it runs over TLS, and the broker can authenticate clients and restrict access to topics. Security therefore depends on configuration: encrypted connections, separate credentials for each device, topic permissions and a broker on a segregated network. A broker reachable from the internet without a password is a common mistake.

What is OPC UA PubSub?

It is Part 14 of the OPC UA specification, which adds publish/subscribe communication to the client/server model. A publisher sends data sets with the OPC UA model and subscribers receive them, including through an MQTT broker. PubSub is useful when many systems need the same machine data and you do not want each of them connecting to the server in the controller.

What it looks like in a project

Connecting the shop floor with your business systems? Talk to an engineer who has worked on MES and OT.

Let's talk

Read more: IT in manufacturing