Evaluate the AutomationView desktop suite free for 30 days. No credit card required. Claim trial key →
arrow_back Back to Articles

Deep Dive: Implementing MQTT Protocol for PLC-to-Cloud Messaging

calendar_month
person Carvalho Raphael

Deep Dive: Implementing MQTT Protocol for PLC-to-Cloud Messaging

Learning
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    classDef default fill:#1E293B,stroke:#475569,stroke-width:2px,color:#F8FAFC
    classDef highlight fill:#3B82F6,stroke:#2563EB,color:#FFFFFF

    A["PLC (Publisher)"]:::highlight -->|Publish Data| B("MQTT Broker")
    B -->|Subscribe & Forward| C["Cloud Analytics"]
    C -->|Publish Command| B
    B -->|Subscribe & Receive| A
AutomationView Icon AutomationView

Transitioning from traditional polling protocols to event-driven architectures is a major hurdle for industrial engineers connecting the shop floor to the cloud. While Modbus TCP and OPC UA excel in deterministic, local networks, they often struggle with latency and bandwidth overhead when scaled to wide-area or cellular networks. PLC MQTT integration solves this by introducing a lightweight, publish-subscribe model specifically designed for high-latency, low-bandwidth environments.

Unlike cyclic data exchange, MQTT only transmits data when a change of state occurs, drastically reducing network traffic. This shift in paradigm, however, requires a different approach to structuring PLC tags, handling connection drops, and securing payloads.

The Publish-Subscribe Paradigm Shift

In a standard Client-Server architecture (like Modbus), the client continuously asks the server for data. If a sensor value changes once an hour, the client might still poll it 3600 times during that hour, wasting bandwidth. With the MQTT publish-subscribe model, the PLC acts as a Publisher. It sends data to a central MQTT Broker only when the value changes or on a specific trigger.

Clients (such as SCADA systems, Historians, or Cloud databases) act as Subscribers. They tell the Broker, “Send me data from this specific topic.” The Broker then routes the published messages to all interested subscribers.

flowchart TD
    classDef standard fill:#0f172a,stroke:#334155,stroke-width:2px,color:#e2e8f0
    classDef broker fill:#0369a1,stroke:#0284c7,stroke-width:2px,color:#ffffff

    subgraph "Polling (Modbus/OPC DA)"
        C1["SCADA (Client)"] -->|Continuous Requests| S1["PLC (Server)"]
    end

    subgraph "Event-Driven (MQTT)"
        P1["PLC (Publisher)"] -->|Publish on Change| B["MQTT Broker"]:::broker
        B -->|Push Data| S2["Cloud App (Subscriber)"]
        B -->|Push Data| S3["Historian (Subscriber)"]
    end

Structuring MQTT Topics in the PLC

A crucial step in PLC MQTT integration is designing a logical topic namespace. Topics are hierarchical strings separated by slashes, similar to a file path. A poorly designed topic structure leads to chaotic data management at the enterprise level.

  • Use a consistent hierarchy: Start broad and get specific. Example: FactoryA/Line1/Cell2/PLC1/Temperature.
  • Keep topics short but descriptive: Every byte counts over cellular connections.
  • Leverage wildcards carefully: Subscribers can use + (single level) or # (multi-level) wildcards. Designing the hierarchy to group related tags simplifies subscriptions.

Handling Payloads: JSON vs. Raw Data

Once the topic is established, you must decide how to format the data payload. Modern PLCs often include built-in JSON serialization blocks, making it easier to parse data in cloud environments.

Format Advantages Disadvantages Best For
Raw String/Integer Extremely lightweight, minimal PLC processing required. Lacks context. The subscriber must know exactly what the data means. Low-bandwidth networks, simple single-value topics.
JSON (Key-Value) Self-describing, easily ingested by cloud databases and web apps. Larger payload size, requires string manipulation/JSON generation in the PLC. Complex data structures, multi-variable topics, enterprise integration.
Sparkplug B Standardized payload format for industrial data, includes state management. Requires specialized libraries in the PLC, more complex implementation. Large-scale IIoT deployments, standardizing data models across vendors.

Managing Quality of Service (QoS) and State

Industrial environments cannot afford lost data during temporary network outages. MQTT offers three Quality of Service (QoS) levels to guarantee message delivery.

  • QoS 0 (At most once): Fire and forget. The PLC sends the message and does not wait for an acknowledgment. Best for high-frequency data where occasional loss is acceptable.
  • QoS 1 (At least once): The PLC waits for an acknowledgment from the Broker. If none is received, it resends the message. This can lead to duplicates, so the subscriber application must handle idempotency.
  • QoS 2 (Exactly once): A four-step handshake ensures the message arrives exactly once. This is the safest but slowest and most bandwidth-intensive option.

The Last Will and Testament (LWT)

How does the cloud know if the PLC goes offline? In traditional polling, a timeout triggers an alarm. In MQTT, the PLC configures a “Last Will and Testament” (LWT) message upon connection. If the PLC disconnects ungracefully (e.g., power loss), the Broker automatically publishes the LWT message (e.g., {"status": "offline"}) to a specific topic, alerting all subscribers.

Field Implementation Nuances

When executing a PLC MQTT integration, engineers often run into practical limitations. Not all PLCs have native MQTT clients. In legacy systems, you might need an edge gateway (like an IoT router or a dedicated protocol converter) to poll the PLC via Modbus or Ethernet/IP and then publish via MQTT.

Furthermore, managing certificates for secure TLS/SSL connections directly on a PLC can be cumbersome. Some older PLCs lack the processing power for heavy encryption, necessitating an edge device to handle the secure MQTT connection to the cloud while communicating in the clear on the local, isolated network.

Conclusion

Mastering PLC MQTT integration is essential for modernizing industrial data pipelines. By shifting from continuous polling to an event-driven, publish-subscribe architecture, engineers can drastically reduce network overhead and seamlessly bridge the gap between the plant floor and cloud analytics platforms. Focus on logical topic structures, appropriate QoS levels, and robust state management via LWT to ensure a reliable IIoT deployment.

FAQ

Can MQTT replace OPC UA?

Not entirely. OPC UA is highly structured and excellent for complex, object-oriented data models and deterministic local networks. MQTT is better suited for lightweight, wide-area telemetry and cloud ingestion. Often, the two are used together, with OPC UA on the edge and MQTT for cloud backhaul.

Is MQTT secure for industrial control?

MQTT itself does not encrypt data, but it is typically deployed over TLS/SSL (MQTTS). When properly configured with certificates and secure authentication, it is highly secure. However, it is generally recommended to use MQTT for data monitoring rather than critical, real-time control loops.

What if my PLC doesn’t support MQTT?

You can use an industrial edge gateway or an IPC. The gateway polls the legacy PLC using its native protocol (like Modbus TCP or PROFINET) and translates the data into MQTT messages for the broker.

Ready to build smarter control systems? Check out our AutomationView Store for tools to streamline your engineering workflow.

Share this article

Stay Updated with Learning

Get the latest articles and news delivered directly to your inbox.

Log in to Subscribe

You must be registered and logged in to manage subscriptions.

Tags:

Recommended for you

5 Essential OPC UA Integration Tips for Secure PLC Success

Learning
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    A[OPC UA Setup] --> B[Use Subscriptions]
    A --> C[Enforce Security]
    A --> D[Optimize Intervals]
    A --> E[Standardize Models]
AutomationView Icon AutomationView
calendar_month

5 Essential OPC UA Integration Tips for Secure PLC Success

Configuring a PLC to talk to a plant-level network often exposes unexpected bottlenecks—especially when polling rates overwhelm the controller’s CPU. While OPC UA Integration provides the standard for bridging operational technology (OT) and enterprise systems, a naive implementation will quickly cause network congestion and security vulnerabilities. Many engineers connect their PLCs using default settings, resulting […]

Read Article arrow_forward

FactoryTalk Orchestration: 5 Concrete Manufacturing Benefits

Automation News
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    classDef optix fill:#0d47a1,stroke:#0d47a1,stroke-width:2px,color:#ffffff;
    classDef it fill:#c62828,stroke:#b71c1c,stroke-width:2px,color:#ffffff;
    classDef ot fill:#ef6c00,stroke:#e65100,stroke-width:2px,color:#ffffff;
    classDef orchestrator fill:#2e7d32,stroke:#1b5e20,stroke-width:2px,font-weight:bold,color:#ffffff;

    ERP["Enterprise (MES/ERP)"]:::it
    FTOrch["FactoryTalk Orchestration"]:::orchestrator
    Optix["Optix Runtime Engine"]:::optix
    PLCs["PLCs and Controllers"]:::ot
    Robots["Robots and AMRs"]:::ot

    ERP --> FTOrch
    FTOrch --> Optix
    FTOrch --> PLCs
    FTOrch --> Robots
AutomationView Icon AutomationView
calendar_month

FactoryTalk Orchestration: 5 Concrete Manufacturing Benefits

Coordinating an Enterprise Resource Planning (ERP) system with a fleet of Autonomous Mobile Robots (AMRs) and a dozen PLCs usually means writing brittle point-to-point TCP socket logic or maintaining thousands of lines of custom C#. Rockwell Automation’s FactoryTalk Orchestration tackles this specific headache. Instead of hardcoding logic across separate hardware layers, it provides a centralized, […]

Read Article arrow_forward

Deep Dive: Architecting IO-Link Networks for Smart Sensor Connectivity

Learning
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    PLC["PLC (Profinet/EtherNet/IP)"] -- "Fieldbus" --> Master["IO-Link Master"]
    Master -- "Point-to-Point (3-Wire)" --> Sensor["IO-Link Smart Sensor"]

    style PLC fill:#0d47a1,stroke:#0d47a1,stroke-width:2px,color:#ffffff
    style Master fill:#1565c0,stroke:#1565c0,stroke-width:2px,color:#ffffff
    style Sensor fill:#1976d2,stroke:#1976d2,stroke-width:2px,color:#ffffff
AutomationView Icon AutomationView
calendar_month

Deep Dive: Architecting IO-Link Networks for Smart Sensor Connectivity

Executive TL;DR: IO-Link requires a gateway (Master) and is a point-to-point standard, not a fieldbus. Cyclic process data needs manual byte mapping, unlike standard analog signals. Acyclic communication (ISDU) unlocks on-the-fly parameterization, reducing changeover times. I still remember commissioning a sprawling conveyor line where over 100 discrete 24V sensors were hardwired back to massive remote […]

Read Article arrow_forward