
graph TD subgraph HW["🔌 Hardware Layer"] UPS["UPS (Zircon AX)<br/>[USB-B Port]"] end subgraph RPi["🍓 Raspberry Pi 4 (Debian 13 arm64) - Loopback Boundary (127.0.0.1)"] USB["USB HID Interface<br/>(/dev/hidraw0)"] subgraph NUT["Network UPS Tools (NUT)"] Driver["NUT Driver<br/>(nutdrv_qx)"] Server["NUT Server (upsd)<br/>LISTEN 127.0.0.1:3493"] end Exporter["nut_exporter (v3.3.0)<br/>LISTEN 127.0.0.1:9199/ups_metrics"] Alloy["Grafana Alloy<br/>(Scraper & Allowlist Filter)"] end subgraph Central["📊 Telemetry Collection"] Prom["Prometheus-Edge"] end UPS -->|"1. USB Cable (A-to-B)"| USB USB -->|"2. Raw HID Telemetry"| Driver Driver -->|"3. Parse Protocol (q1/hunnox)"| Server Exporter -->|"4. Query TCP 3493 (nut.server)"| Server Server -->|"5. Return NUT Vars (status, voltage, temp)"| Exporter Alloy -->|"6. HTTP GET /ups_metrics"| Exporter Exporter -->|"7. Expose Prometheus Metrics"| Alloy Alloy -->|"8. Filter Metric Allowlist"| Prom style RPi fill:#f4f5f7,stroke:#333,stroke-width:2px style HW fill:#e3f2fd,stroke:#1565c0,stroke-width:2px style Central fill:#fff3e0,stroke:#e65100,stroke-width:2px
คู่มือการพัฒนา Raspberry Pi USB UPS Monitoring ฉบับนี้ สรุปขั้นตอนและข้อกำหนดการตั้งค่าระบบเฝ้าระวังเครื่องสำรองไฟ (UPS) ผ่าน USB บน Raspberry Pi 4 (Debian 13 arm64) ไว้ดังนี้ครับ:
- ขอบเขตและขอบเขตความปลอดภัย (Scope & Safety Boundary)

- การทำงานแบบอ่านอย่างเดียว (Read-only): ระบบถูกออกแบบมาเพื่อ อ่านสถานะเท่านั้น
- ข้อห้ามสำคัญ: ห้ามตั้งค่าการปิดเครื่องอัตโนมัติ (Automatic shutdown),
upsmon,upscmd,upsrwหรือคำสั่งควบคุม UPS ใดๆ โดยเด็ดขาด - การจำกัดเครือข่าย (Loopback Only): บริการทั้งหมด (NUT และ Exporter) ต้องผูกติดกับ
127.0.0.1เท่านั้น ห้ามเปิดพอร์ต3493หรือ9199ออกสู่เครือข่าย LAN

- การจำแนกประเภท UPS (UPS Classification)

ก่อนติดตั้ง ต้องระบุประเภทของ UPS ในไฟล์โปรไฟล์ของ Edge (edge/profiles/<device_id>.json):
- none: ไม่มี UPS ติดตั้ง
- basic: มี UPS ป้องกันไฟ แต่ไม่มีอินเทอร์เฟซส่งข้อมูล (จะไม่ติดตั้ง NUT หรือ Exporter)
- smart: มีอินเทอร์เฟซ USB/Network ที่รองรับ และสามารถส่งข้อมูลค่าต่างๆ ได้
-
ขั้นตอนการติดตั้งและตั้งค่า (Step-by-Step Configuration)
- การเชื่อมต่อกายภาพ (Physical Connection):
- ต่อสายจากพอร์ต USB-A (Raspberry Pi) ไปยังพอร์ต USB-B (UPS)
- ตรวจสอบการเชื่อมต่อด้วยคำสั่ง
lsusbและls -l /dev/hidraw*(อุปกรณ์ทดสอบ Zircon AX พบ Vendor/Product ID เป็น0001:0000)
- ติดตั้งและตั้งค่า Network UPS Tools (NUT):
- ติดตั้งแพ็กเกจ
nut-serverและnut-client - กำหนดค่าใน
/etc/nut/nut.confเป็นMODE=standalone - ตั้งค่าไฟล์
/etc/nut/ups.conf(สำหรับ Zircon AX) โดยใช้ไดรเวอร์nutdrv_qx,subdriver = hunnox,protocol = q1 - จำกัดการดักฟังของ NUT ใน
/etc/nut/upsd.confให้ฟังเฉพาะLISTEN 127.0.0.1 3493
- ติดตั้งแพ็กเกจ
- เปิดเฉพาะบริการที่จำเป็น:
- ปิด บริการ
nut-monitor.service - เปิด บริการ
nut-driver@zircon_ax.serviceและnut-server.service
- ปิด บริการ
- ตรวจสอบการอ่านค่า NUT:
- ทดสอบอ่านค่าด้วยคำสั่ง
upsc [email protected] - ตรวจสอบสถานะ
ups.status:- OL: ไฟบ้านปกติ (Online)
- OB: ทำงานบนแบตเตอรี่ (On Battery)
- DISCHRG: ตัวบ่งชี้การคายดิสชาร์จ
- ทดสอบอ่านค่าด้วยคำสั่ง
- ติดตั้ง Prometheus Exporter (nut_exporter):
- ติดตั้งไฟล์ไบนารี
nut_exporter(v3.3.0) ไว้ที่/usr/local/bin/nut_exporter - สร้างบริการ systemd
nut-exporter.serviceให้รับค่าจาก NUT และเปิดปลายทาง Metrics ที่127.0.0.1:9199/ups_metrics
- ติดตั้งไฟล์ไบนารี
- ตั้งค่า Alloy Allowlist:
- กรองดึงเฉพาะค่า Metrics ที่ได้รับอนุมัติ ได้แก่:
network_ups_tools_battery_voltagenetwork_ups_tools_ups_temperaturenetwork_ups_tools_ups_status(รองรับ flagOL,OB,DISCHRG)
- กรองดึงเฉพาะค่า Metrics ที่ได้รับอนุมัติ ได้แก่:
- การเชื่อมต่อกายภาพ (Physical Connection):
sequenceDiagram autonumber participant UPS as 🔌 UPS (Hardware) participant Driver as ⚙️ NUT Driver (nutdrv_qx) participant Server as 🖥️ NUT Server (upsd :3493) participant Exporter as 📤 nut_exporter (:9199) participant Alloy as 🔀 Grafana Alloy participant Prom as 📈 Prometheus-Edge Note over UPS, Exporter: 🔒 Localhost Boundary (127.0.0.1 Only) UPS->>Driver: ส่งสัญญาณผ่านสาย USB (USB HID /dev/hidraw0) Driver->>Server: แปลงโปรโตคอล (q1/hunnox) เก็บค่าสถานะ (OL/OB, Voltage, Temp) loop Scrape Cycle Exporter->>Server: สอบถามค่าผ่านพอร์ต 3493 (nut.vars_enable) Server-->>Exporter: ส่งค่า NUT Variables ตอบกลับ Alloy->>Exporter: HTTP GET Request ไปที่ 127.0.0.1:9199/ups_metrics Exporter-->>Alloy: แปลงค่าส่งกลับเป็น Prometheus Metrics Format Note over Alloy: กรองเอาเฉพาะ Metrics ใน Allowlist<br/>(battery_voltage, ups_temperature, ups_status) Alloy->>Prom: Remote Write / Forward Metrics ไปยัง Prometheus-Edge end
- ผลลัพธ์จากการทดสอบจริงและการยกเลิกการตั้งค่า (Results & Rollback)
- ผลลัพธ์การทดสอบ (Zircon AX บน Pi 4 / Debian 13): สามารถอ่านค่าแรงดันไฟฟ้า (Voltage), ความถี่ (Frequency), โหลด (Load), อุณหภูมิ (Temperature) และสถานะได้ปกติ แต่ไม่สามารถอ่านเปอร์เซ็นต์แบตเตอรี่หรือเวลาคงเหลือที่น่าเชื่อถือได้ จึงต้องกำหนด
battery_charge_percent: "unavailable"ไว้ในโปรไฟล์ - การ Rollback: หากระบบเฝ้าระวังมีปัญหา ให้สั่งหยุดเฉพาะบริการเฝ้าระวัง (
nut-exporter,nut-server,nut-driver) โดยไม่ต้องถอดสายจ่ายไฟหลักของ UPS