##!/usr/bin/env python3 # Bei mir läuft ein sparsamer Fujitsu Futro S720 ohne Montor und keyboard, kosten so 20€, mit dem Broker und dem Python Script. from huawei_solar import HuaweiSolar import huawei_solar import paho.mqtt.client as mqttClient from datetime import datetime import requests import time import json import socket import random import os import urllib3 ##sudo chmod +x start-mqtt.sh ##ps aux | grep mqtt-start.py ##Sudo kill ##nohup python3 mqtt-start.py & #sudo nano /etc/rc.local #sudo chmod +x mqtt-start.py #ps aux | grep -v grep | grep -i pythonls dongle = huawei_solar.HuaweiSolar('192.168.0.41', port=502, slave=100) #IP Adresse und Port Dongle dongle.wait = 2 inverter = huawei_solar.HuaweiSolar('192.168.0.41', port=502, slave=1) inverter.wait = 1 def speichern(inhalt,wert): inhalt = (inhalt.replace(".","-")) path = "/home/michael/log/Huawei/" #Pfad zum Speicher der Daten if not os.path.exists(path): os.mkdir("/home/michael/log/Huawei", mode=0o777, dir_fd=None) #Wenn nicht existier, wird er angelegt Scjhreibrechte beachten path = "/home/michael/log/Huawei/" + now.strftime("%Y") if not os.path.exists(path): os.mkdir("/home/michael/log/Huawei/" + now.strftime("%Y"), mode=0o777, dir_fd=None) path = "/home/michael/log/Huawei/" + now.strftime("%Y") + "/" + now.strftime("%m") if not os.path.exists(path): os.mkdir("/home/michael/log/Huawei/" + now.strftime("%Y") + "/" + now.strftime("%m"), mode=0o777, dir_fd=None) #print(now.strftime("%d.%m.%Y")) file = open(path + "/" + now.strftime("%d") + "-" + inhalt + ".txt" ,"a") content = now.strftime("%H:%M") + ' ' + wert +'\n' file.write(content) #print (content) file.close() def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to broker") else: print("Connection failed") def dongleAccess(): #print () global solarleistung,xx,solartag vara = ['total_input_power','grid_power','load_power','total_battery_power','total_active_power'] varb = ['37498','37502','37500','37504','37516'] varc = ['Solar_aktuell','Netz_ein_aus','WR-Leistung','Batterien_ein-aus','Wirkleistung'] x = -1 for i in vara: x=x+1 try: mid = dongle.get(i) wert = str(mid.value) wert = (wert.replace(".","")) if x < 4: if x == 0: solarleistung = solarleistung + int(wert) #print (solarleistung) xx = xx+1 client.publish("103-Solarview/HuaweiX/"+ varc[x] +"/set", wert) print ('Dongle Huawei X ' + str(x) +' Register: ' + varb[x]+ ' ' + vara[x] + ' = ' +varc[x] + ' ' + wert) speichern(varc[x], wert) if x > 3 and x <8: client.publish("103-Solarview/HuaweiC/"+ varc[x] +"/set", wert) print ('Dongle Huawei C ' + str(x) +' Register: ' + varb[x]+ ' ' + vara[x] + ' = ' +varc[x] + ' ' + wert) speichern(varc[x], wert) except: print ('Fehler') wert = '0' def inverterAccess(): global wert1,wert2,summe,zeit,minute vara = ['storage_current_day_charge_capacity','storage_current_day_discharge_capacity','storage_total_charge','storage_total_discharge', 'active_power','solar_power','storage_charge_discharge_power','storage_state_of_capacity', 'phase_A_voltage','phase_B_voltage','phase_C_voltage', 'phase_A_current','phase_B_current','phase_C_current','daily_yield_energy', 'storage_unit_1_state_of_capacity','storage_unit_1_charge_discharge_power','storage_unit_2_state_of_capacity','storage_unit_2_charge_discharge_power'] varb =['37784','37786','37780','32082', '32080','32064','37765','37760', '32069','32070','32071', '32072','32074','32076','32114', '37004','37001','37738','37743'] varc = ['Batterie_Tag_geladen','Batterie_Tag_entladen','Battereie_Total_geladen','Batterie_Total_entladen', 'Wirkleistung','Solarleistung','Batterie_ein_aus','Batterie_Kapazitaet', 'Phase_A_Spannung','Phase_B_Spannung','Phase_C_Spannung', 'Phase_A_Strom','Phase_B_Strom','Phase_C_Strom','Tag_bezogen', 'Batterie_1_Kapazitaet','Batterie_1_Be-Entladung','Batterie_2_Kapazitaet','Batterie_2_Be-Entladung'] x = -1 for i in vara: x=x+1 try: mid = inverter.get(i) wert = str(mid.value) wert = (wert.replace(".","")) if x < 4: print ('Inverter Huawei A ' + str(x) +' Register: ' + varb[x]+ ' ' + vara[x] + ' = ' +varc[x] + ' ' + wert) client.publish("103-Solarview/HuaweiA/"+ varc[x] + "/set", wert) speichern(varc[x], wert) if x > 3 and x <8: print ('Inverter Huawei B ' + str(x) +' Register: ' + varb[x]+ ' ' + vara[x] + ' = ' +varc[x] + ' ' + wert) if ('storage_state_of_capacity' in i and int(wert) > 1000): wert = '1000' client.publish("103-Solarview/HuaweiB/"+ varc[x] + "/set", wert) speichern(varc[x], wert) if x > 7 and x <11: print ('Inverter Huawei C ' + str(x) +' Register: ' + varb[x]+ ' ' + vara[x] + ' = ' +varc[x] + ' ' + wert) client.publish("103-Solarview/HuaweiC/"+ varc[x] + "/set", wert) speichern(varc[x], wert) if x > 10 and x <15: print ('Inverter Huawei D ' + str(x) +' Register: ' + varb[x]+ ' ' + vara[x] + ' = ' +varc[x] + ' ' + wert) client.publish("103-Solarview/HuaweiD/"+ varc[x] + "/set", wert) speichern(varc[x], wert) if x > 14 : print ('Inverter Huawei E ' + str(x) +' Register ' + varb[x]+ ' ' + vara[x] + ' = ' +varc[x] + ' ' + wert) if 'unit_1_state' in i: if int(wert) > 1000: wert = "1000" if 'unit_2_state' in i: if int(wert) > 1000: wert = "1000" client.publish("103-Solarview/HuaweiE/"+ varc[x] + "/set", wert) speichern(varc[x], wert) except: print ('Fehler') wert = '0' broker_address= "192.168.0.103" #Mqtt Broker port = 1883 client = mqttClient.Client("client_32694476_sdongle") #Einmalige Kennung im Netz client.on_connect= on_connect client.username_pw_set("michael","xxxxxxx") #Name und Passwort für Broker client.connect(broker_address, port=port) client.loop_start() #client.loop_forever() wert1 = 0 wert2 = 0 summe = 0 solarleistung = 1539963 xx=1738 temp = 0 solartag=0 stunde=0 try: while True: time.sleep(1) now = datetime.now() zeit = now.strftime("%S") minute = now.strftime("%M") stunde = now.strftime("%H") #mid = dongle.get("total_input_power") #wert = str(mid.value) #wert = (wert.replace(".","")) #lient.publish("103-Solarview/HuaweiX/Solar_aktuell/set", wert) #olarleistung = solarleistung + int(wert) xx = xx+1 if solarleistung > 0: temp = (int(solarleistung/xx)) print('Minute & Sekunde: ' + str(minute)+ str(zeit)) if ("00" in minute and "00" in zeit): #Zur vollen Stunde solarleistung = (int(solarleistung/xx)) solartag = solartag + solarleistung client.publish("103-Solarview/Solartag/set", solartag) solarleistung = 0 xx = 00 if ('00' in stunde): client.publish("Solar/Tagesleistung/set", solartag) solartag = 0 if ("00" in zeit or '20' in zeit or '40' in zeit): #Alle 20 Minuten print ('Solarleistung '+ str(solarleistung) + ' / ' + str(xx) +' = Solarschnitt: ' + str(temp)) client.publish("103-Solarview/Solarleistung/set", temp) dongleAccess() dongle.close_connection() inverterAccess() inverter.close_connection() except KeyboardInterrupt: print("exiting") client.disconnect() client.loop_stop() sys.exit(0)