Python กลายเป็นเครื่องมือที่วิศวกร Automation ใช้บ่อยขึ้นเรื่อยๆ สำหรับงาน SCADA และ Data Analysis เพราะ Library ที่ครบและ Community ขนาดใหญ่ บทความนี้อธิบายว่า Python ช่วยงาน SCADA ได้อย่างไรบ้าง พร้อมตัวอย่าง Code ที่ใช้จริงในโรงงานชลบุรีและระยอง
Python ช่วยงาน SCADA ได้อะไรบ้าง?
- อ่านข้อมูลจาก PLC ผ่าน Modbus TCP, OPC UA, S7 Protocol โดยตรง
- เขียนข้อมูลลง Database เช่น InfluxDB, MySQL, PostgreSQL สำหรับ Historical Analysis
- ส่ง LINE Notify / Email Alert อัตโนมัติเมื่อ Condition ตรงตามที่กำหนด
- สร้าง Dashboard ด้วย Grafana (ดึงข้อมูลจาก Python), Dash (Plotly), หรือ Streamlit
- วิเคราะห์ข้อมูล เช่น หา Downtime Pattern, Predict Failure ด้วย Machine Learning
- Report อัตโนมัติ สร้าง Excel/PDF Report และส่ง Email ทุกเช้า
Library Python ที่ต้องรู้สำหรับงาน Industrial
| Library | ใช้ทำอะไร | Protocol |
|---|---|---|
| pymodbus | อ่าน/เขียน PLC ผ่าน Modbus RTU/TCP | Modbus |
| opcua-asyncio | เชื่อมต่อ OPC UA Server | OPC UA |
| snap7 | อ่าน/เขียน Siemens S7 PLC โดยตรง | S7 Comm |
| melsec-interface | เชื่อมต่อ Mitsubishi PLC ผ่าน SLMP | MC Protocol |
| influxdb-client | เขียนข้อมูล Time-series ลง InfluxDB | HTTP API |
| pandas | วิเคราะห์ข้อมูลตาราง Time-series | N/A |
| requests | เรียก LINE Notify API, REST API | HTTP |
| schedule | ตั้งเวลาให้ Script ทำงานอัตโนมัติ | N/A |
ตัวอย่าง: อ่านข้อมูลจาก Mitsubishi PLC ด้วย Python
# pip install pymcprotocol
import pymcprotocol
plc = pymcprotocol.Type3E()
plc.connect("192.168.1.10", 5007) # IP, Port ของ PLC
# อ่านค่า D100 ถึง D104
_, word_values = plc.batchread_wordunits(headdevice="D100", readsize=5)
print(f"D100-D104: {word_values}")
# อ่าน Bit M0 ถึง M7
_, bit_values = plc.batchread_bitunits(headdevice="M0", readsize=8)
print(f"M0-M7: {bit_values}")
plc.close()
ตัวอย่าง: ส่ง LINE Notify อัตโนมัติจาก PLC
import requests, time, pymcprotocol
TOKEN = "your_line_token_here"
def send_line(msg):
requests.post("https://notify-api.line.me/api/notify",
headers={"Authorization": f"Bearer {TOKEN}"},
data={"message": msg})
plc = pymcprotocol.Type3E()
plc.connect("192.168.1.10", 5007)
while True:
_, vals = plc.batchread_wordunits("D200", 1)
temp = vals[0] / 10 # D200 เก็บอุณหภูมิ × 10
if temp > 80.0:
send_line(f"⚠️ อุณหภูมิ Motor เกิน: {temp}°C")
time.sleep(10)
Architecture ระบบ Python SCADA ในโรงงาน
โครงสร้างที่ Xeniel แนะนำสำหรับโรงงานขนาดกลาง:
- Raspberry Pi / Industrial PC — รัน Python Script เก็บข้อมูลจาก PLC
- InfluxDB — เก็บ Time-series Data (ฟรีและเร็วมาก)
- Grafana — Dashboard แสดงผล Real-time และ Historical (ฟรีและสวย)
- LINE Notify — Alert เมื่อมี Alarm หรือ KPI ผิดปกติ
- Python Schedule — Daily Report ส่ง Email ทุกเช้า
สิ่งที่ต้องระวัง: Python ไม่ใช่ Realtime OS ไม่เหมาะกับงาน Control ที่ต้องการ Deterministic Timing ใช้เฉพาะงาน Monitoring และ Analytics เท่านั้น
ควรเรียกผู้เชี่ยวชาญเมื่อไหร่?
- ต้องการ Full Stack IIoT Solution ที่รวม PLC Collection, Database, Dashboard
- ต้องการ Predictive Maintenance ด้วย Machine Learning
- มีโปรเจกต์ Python ที่เขียนเองแล้วมีปัญหา ต้องการ Code Review
สรุป
Python เป็นเครื่องมือที่ทรงพลังมากสำหรับงาน SCADA Data Collection, Alert และ Analytics ราคาถูก (ส่วนใหญ่ฟรี) และ Flexible สูง ทีม Xeniel Automation รับออกแบบและพัฒนา Python-based IIoT Solution สำหรับโรงงานในชลบุรีและระยอง
ต้องการ Python IIoT Solution สำหรับโรงงาน?
Xeniel Automation รับพัฒนา Python SCADA Data Collection Dashboard LINE Alert สำหรับโรงงานทุกขนาด บริการนิคมชลบุรีและระยอง
ปรึกษาวิศวกรฟรี ทันที!โทร 098-965-3845 | LINE: @xeniel.24
ตาราง Python Library สำหรับ SCADA ครบ
| Library | ใช้ทำอะไร | Protocol/PLC |
|---|---|---|
| pymodbus | อ่านเขียน Modbus TCP/RTU | ทุกยี่ห้อที่มี Modbus |
| python-snap7 | Connect Siemens S7 | S7-300/400/1200/1500 |
| asyncua | OPC UA Client | ทุกยี่ห้อที่รองรับ OPC UA |
| pymcprotocol | MELSEC Protocol | Mitsubishi Q/iQ-R/FX5U |
| dash / plotly | Web Dashboard Real-time | — |
| influxdb-client | Write/Query Time-series DB | — |
| requests | LINE Notify, Webhook | — |
| pandas | วิเคราะห์ Production Data | — |
สรุป
Python SCADA ให้ความยืดหยุ่นสูง ต้นทุนต่ำ เหมาะ SME ที่มีทีม IT Xeniel Automation รับออกแบบ Python SCADA สำหรับโรงงานในชลบุรีและระยอง
ต้องการ SCADA Python? Xeniel Setup ให้
Xeniel Automation รับออกแบบ Python SCADA + LINE Notify + OEE Dashboard สำหรับโรงงานชลบุรีและระยอง
ปรึกษาฟรีทาง LINE โทร 098-965-3845