Python-скрипт для запроса текущих значений тока/напряжения/мощности/потребления Shelly Pro1PM
Следующий скрипт запрашивает у устройства Shelly Pro1PM статистику тока, напряжения, мощности и энергопотребления. Он использует библиотеку httpx для выполнения HTTP GET-запросов к API устройства Shelly.
shelly_query.py
import httpx
# IP-адрес устройства Shelly
device_ip = "192.168.33.1"
base_url = f"http://{device_ip}"
# Эндпоинт для параметров мощности (предполагается устройство Gen2 с RPC API)
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