Python 脚本:查询 Shelly Pro1PM 的电流/电压/功率/能耗
以下脚本查询 Shelly Pro1PM 设备的电流、电压、功率和能耗统计信息。它使用 httpx 库向 Shelly 设备的 API 发送 HTTP GET 请求。
shelly_query.py
import httpx
# Shelly 设备 IP 地址
device_ip = "192.168.33.1"
base_url = f"http://{device_ip}"
# 功率参数端点(假设是带 RPC API 的 Gen2 设备)
power_url = f"{base_url}/rpc/Switch.GetStatus?id=0"
try:
# 向功率端点发送 GET 请求
response = httpx.get(power_url, timeout=5.0)
response.raise_for_status()
data = response.json()
print("Shelly Pro 1PM Power Parameters:")
print(f" Output On: {data.get('output')}")
print(f" Power: {data.get('apower')} W")
print(f" Voltage: {data.get('voltage')} V")
print(f" Current: {data.get('current')} A")
print(f" Power Factor: {data.get('pf')}")
print(f" Frequency: {data.get('freq')} Hz")
print(f" Energy: {data.get('aenergy', {}).get('total', 0)} Wh")
except httpx.RequestError as e:
print(f"Request error: {e}")
except httpx.HTTPStatusError as e:
print(f"HTTP error: {e.response.status_code} - {e.response.text}")
except Exception as e:
print(f"Unexpected error: {e}")示例输出
shelly_output.txt
Shelly Pro 1PM Power Parameters:
Output On: True
Power: -36.8 W
Voltage: 234.3 V
Current: 0.927 A
Power Factor: 0.7
Frequency: 50.0 Hz
Energy: 14169.316 WhIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow