Python · Industrial Automation

Python MQTT for Industrial Automation: Publish PLC & SQL Data

Learn Python MQTT for industrial automation using paho-mqtt. Publish process data as JSON, subscribe to IIoT topics, use QoS and connect plant data to brokers safely.

Practical Python Automation Context Copy-Paste Examples Code Architecture

Learning Overview

Python Learning SeriesPython for Industrial AutomationPractical

Learning Goal

  • Learn Python MQTT for industrial automation using paho-mqtt. Publish process data as JSON, subscribe to IIoT topics, use QoS and connect plant data to brokers safely.
  • Run the examples in VS Code using the project environment prepared in earlier topics.
  • Understand where this topic fits in the complete SQL, reporting and Industry 4.0 workflow.
Quick answer

Learn Python MQTT for industrial automation using paho-mqtt. Publish process data as JSON, subscribe to IIoT topics, use QoS and connect plant data to brokers safely.

Architecture: Python MQTT for IIoT

Code-Based Architecture

Python MQTT for IIoT

1

Plant Data

PLC/SQL/process values

Temp_Act = 850.2
2

Python

Build payload

dict → JSON
3

MQTT Client

paho-mqtt

Client()
4

Broker

Route messages by topic

factory/sqf02/process
5

Subscriber

Dashboard / historian / app

subscribe(...)
6

Monitor

Log delivery/reconnect

on_connect / logging
Plant DataPythonMQTT ClientBrokerSubscriberMonitor

1. Install paho-mqtt

python -m pip install paho-mqtt

2. Create a JSON payload

import json
from datetime import datetime payload = { "DT": datetime.now().isoformat(timespec="seconds"), "SQF_No": 2, "Temp_Act": 850.2, "Fan_Status": True,
}
message = json.dumps(payload)
print(message)

3. Publish a message

import paho.mqtt.client as mqtt client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.connect("mqtt.example.local", 1883, 60)
client.publish("factory/sqf02/process", message, qos=1)
client.disconnect()

4. Subscribe to a topic

def on_message(client, userdata, msg): print(msg.topic, msg.payload.decode("utf-8")) client.on_message = on_message
client.subscribe("factory/sqf02/#", qos=1)

5. QoS comparison

QoSMeaningTypical use
0At most onceFast non-critical telemetry
1At least onceCommon industrial telemetry when duplicates can be handled
2Exactly once protocol flowUse only when required because it adds overhead

6. Industrial MQTT checks

  • Use TLS/authentication when required
  • Design stable topic names
  • Timestamp payloads
  • Handle reconnects and duplicate QoS 1 messages
  • Do not expose control topics without authorization and validation

Frequently Asked Questions

What is MQTT used for in industrial automation?

MQTT is a lightweight publish/subscribe protocol commonly used to move telemetry between edge devices, brokers, applications and cloud/IIoT platforms.

Why serialize data as JSON?

JSON provides readable structured key/value data that many MQTT consumers can parse easily.

Which QoS should I use?

Choose based on delivery requirements and system design; QoS 1 is common for telemetry where duplicates can be handled.

Reviewed by Bhawesh Kumar SinghIndustrial Automation Trainer and Industry 4.0 Consultant · Softwell Automation · 21+ years industry experience
Complete Python for Industrial Automation Learning Path

Use Previous / Next for sequential training, or open any topic directly.

Complete

Continue with the next topic in the Python for Industrial Automation learning path.

Open Learning Path
Python for Industrial Automation

Python MQTT for IIoT

Learn Python MQTT for industrial automation using paho-mqtt. Publish process data as JSON, subscribe to IIoT topics, use QoS and connect plant data to brokers safely.

Content reviewed: 14 September 2026

☎ Call WhatsApp ✉ Email Enquire Now