บทความนี้อธิบายแนวคิดทั่วไปและตัวอย่างเพื่อการเรียนรู้ ไม่ได้อ้างว่าเป็นระบบที่ Xeniel นำไปใช้งานในโรงงาน

  • อ่านข้อมูลจาก 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/TCPModbus
opcua-asyncioเชื่อมต่อ OPC UA ServerOPC UA
snap7อ่าน/เขียน Siemens S7 PLC โดยตรงS7 Comm
melsec-interfaceเชื่อมต่อ Mitsubishi PLC ผ่าน SLMPMC Protocol
influxdb-clientเขียนข้อมูล Time-series ลง InfluxDBHTTP API
pandasวิเคราะห์ข้อมูลตาราง Time-seriesN/A
requestsเรียก LINE Notify (ยุติบริการแล้ว) API, REST APIHTTP
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 ทั่วไปสำหรับการเรียนรู้

ตัวอย่างโครงสร้างทางเทคนิคเพื่อการเรียนรู้ ไม่ใช่ข้อเสนอหรือบริการของ 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

สรุป

Libraryใช้ทำอะไรProtocol/PLC
pymodbusอ่านเขียน Modbus TCP/RTUทุกยี่ห้อที่มี Modbus
python-snap7Connect Siemens S7S7-300/400/1200/1500
asyncuaOPC UA Clientทุกยี่ห้อที่รองรับ OPC UA
pymcprotocolMELSEC ProtocolMitsubishi Q/iQ-R/FX5U
dash / plotlyWeb Dashboard Real-time—
influxdb-clientWrite/Query Time-series DB—
requestsLINE Notify (ยุติบริการแล้ว), Webhook—
pandasวิเคราะห์ Production Data—

สรุป