initial version
This commit is contained in:
parent
ea2951da08
commit
5fc95e66a6
74
solar_edge_main.py
Normal file
74
solar_edge_main.py
Normal file
@ -0,0 +1,74 @@
|
||||
import sunspec2.modbus.client as client
|
||||
import paho.mqtt.client as mqtt
|
||||
import time
|
||||
import logging
|
||||
from constants import mqtt_username, mqtt_password
|
||||
|
||||
# --- Configuratie ---
|
||||
INVERTER_IP = "192.168.200.65"
|
||||
INVERTER_PORT = 502 # Probeer 1502 als 502 niet werkt
|
||||
MQTT_BROKER = "10.0.0.3"
|
||||
TOPIC_PREFIX = "solaredge/inverter"
|
||||
|
||||
# Logging instellen voor foutopsporing
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger("SolarEdgeService")
|
||||
|
||||
|
||||
def connect_mqtt():
|
||||
c = mqtt.Client()
|
||||
c.username_pw_set(mqtt_username, mqtt_password)
|
||||
try:
|
||||
c.connect(MQTT_BROKER, 1883, 60)
|
||||
return c
|
||||
except Exception as e:
|
||||
logger.error(f"MQTT Verbinding mislukt: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def read_and_publish():
|
||||
inv_device = None
|
||||
mqtt_client = connect_mqtt()
|
||||
|
||||
while True:
|
||||
try:
|
||||
# 1. Verbind met Inverter als dat nog niet is gebeurd
|
||||
if inv_device is None:
|
||||
logger.info("Verbinden met SolarEdge...")
|
||||
inv_device = client.SunSpecModbusClientDeviceTCP(
|
||||
slave_id=1, ipaddr=INVERTER_IP, ipport=INVERTER_PORT, timeout=5
|
||||
)
|
||||
inv_device.scan()
|
||||
|
||||
# 2. Data lezen
|
||||
# Model 101/103 bevatten de inverter data
|
||||
# We zoeken dynamisch naar het juiste model
|
||||
itf = inv_device.inverter[0]
|
||||
itf.read()
|
||||
|
||||
power_w = itf.W.get_value()
|
||||
energy_total = itf.WH.get_value()
|
||||
status = itf.St.get_value()
|
||||
|
||||
# 3. Publiceren naar MQTT
|
||||
if mqtt_client:
|
||||
mqtt_client.publish(f"{TOPIC_PREFIX}/power", power_w)
|
||||
mqtt_client.publish(f"{TOPIC_PREFIX}/total_energy", energy_total)
|
||||
mqtt_client.publish(f"{TOPIC_PREFIX}/status", status)
|
||||
logger.info(f"Update verzonden: {power_w}W total_energy: {energy_total}Wh status: {status}")
|
||||
else:
|
||||
mqtt_client = connect_mqtt() # Herstel MQTT verbinding
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Fout tijdens uitlezen: {e}")
|
||||
# Forceer nieuwe verbinding bij de volgende poging
|
||||
if inv_device:
|
||||
inv_device.close()
|
||||
inv_device = None
|
||||
time.sleep(5) # Korte pauze bij fout om CPU te sparen
|
||||
|
||||
time.sleep(15) # Wacht 15 seconden voor de volgende update
|
||||
|
||||
if __name__ == "__main__":
|
||||
read_and_publish()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user