
OPC DA转MQTT
老工厂里那些OPC DA设备就像倔强的老古董,明明数据就在眼前却死活连不上物联网平台。咱们今
天就手搓个数据搬运工,让这些陈年设备的数据坐上MQTT火箭直冲云端。
先整点硬菜——Python全家桶安排上。OpenOPC库对付OPC DA,paho-mqtt负责MQTT传输,这俩货配合
起来效果拔群:
```python
import OpenOPC
import paho.mqtt.client as mqtt
from time import sleep
opc = OpenOPC.client()
mqtt_client = mqtt.Client()
def connect_devices():
opc.connect('Kepware.KEPServerEX.V6')
mqtt_client.connect('iot.eclipse.org', 1883)
class TagPoller:
def __init__(self, interval=1.0):
self.tags = {
'Channel1.Device1.Tag1': 'factory/temp',
'Channel2.Device3.Tag8': 'press/psi'
}
self.interval = interval
def start_loop(self):
while True:
for opc_tag, mqtt_topic in self.tags.items():
try:
value = opc.read(opc_tag)[0]
mqtt_client.publish(mqtt_topic, str(value))
print(f"{opc_tag} -> {mqtt_topic}: {value}")
except Exception as e: