Ich habe es nun mit folgendem Script auf der Version2 am laufen und teste aktuell noch ob das alles so weit passt:
Auf dem Pi muss noch die aktuelle Python installiert werden bei mir läuft es mit 3.10
Außerdem muss paho und numpy nachinstalliert werden das geht mit folgenden Befehlen in der Shell:
Code: Alles auswählen
import time
import asyncio
import paho.mqtt.publish as publish
import numpy as np
import json
from huawei_solar import HuaweiSolarBridge, register_names as rn, register_values as rv
loop = asyncio.new_event_loop()
# openWB IP:
mqtthost = "192.168.178.11"
mqttclient = "PVImporter"
# Dictionary to map register names to MQTT topics
topic_mapping = {
'input_power': "openWB/set/pv/18/get/power",
'storage_state_of_capacity': "openWB/set/bat/17/get/soc",
'grid_accumulated_energy': "openWB/set/counter/16/get/imported",
'grid_exported_energy': "openWB/set/counter/16/get/exported",
'storage_charge_discharge_power': "openWB/set/bat/17/get/power",
'accumulated_yield_energy': "openWB/set/pv/get/exported",
'daily_yield_energy': "openWB/set/pv/18/get/exported",
'storage_current_day_charge_capacity': "openWB/set/bat/17/get/imported",
'storage_current_day_discharge_capacity': "openWB/set/bat/17/get/exported",
'power_meter_active_power': "openWB/set/counter/16/get/power",
'grid_frequency': "openWB/set/counter/16/get/frequency",
'active_grid_power_factor': "openWB/set/counter/16/get/power_factors",
}
def publish2openWB(mqtttopic, message):
publish.single(mqtttopic, payload=message, qos=0, retain=False, hostname=mqtthost, client_id=mqttclient)
async def huaweiReadValues(topic_mapping):
bridge = await HuaweiSolarBridge.create(host="192.168.200.1", port=6607)
# Register names
registers_evu = ['grid_frequency', 'power_meter_active_power', 'active_grid_A_power',
'active_grid_B_power', 'active_grid_C_power', 'grid_A_voltage', 'grid_B_voltage',
'grid_C_voltage', 'active_grid_A_current', 'active_grid_B_current', 'active_grid_C_current',
'grid_exported_energy', 'grid_accumulated_energy', 'active_power', 'grid_voltage', 'active_grid_power_factor']
registers_wr = ['input_power', 'accumulated_yield_energy', 'active_power', 'daily_yield_energy']
registers_bat = ['storage_state_of_capacity', 'storage_charge_discharge_power', 'storage_current_day_charge_capacity', 'storage_current_day_discharge_capacity']
registers = registers_evu + registers_wr + registers_bat
# Main Tasks:
while True:
try:
if not bridge:
bridge = await HuaweiSolarBridge.create(host="192.168.200.1", port=6607)
# Dictionary to store the register values
register_values = {}
# Initialize an empty NumPy array for grid currents
array_currents_grid = np.array([])
# Initialize an empty NumPy array for grid voltages
array_voltages_grid = np.array([])
# Initialize an empty NumPy array for grid voltages
array_powers_grid = np.array([])
# Initialize an empty NumPy array for grid voltages
array_powerfactors_grid = np.array([])
for i in registers:
try:
result = await bridge.client.get(i)
if result[0] is not None and result[0] != 0:
mqtttopic = False
if str(i) == "input_power":
mqtttopic = "openWB/set/pv/18/get/power"
message = result[0] * -1
elif str(i) == "storage_state_of_capacity":
mqtttopic = "openWB/set/bat/17/get/soc"
message = result[0]
elif str(i) == "grid_accumulated_energy":
mqtttopic = "openWB/set/counter/16/get/imported"
message = result[0]
elif str(i) == "grid_exported_energy":
mqtttopic = "openWB/set/counter/16/get/exported"
message = result[0]
elif str(i) == "storage_charge_discharge_power":
mqtttopic = "openWB/set/bat/17/get/power"
message = result[0]
elif str(i) == "daily_yield_energy":
mqtttopic = "openWB/set/pv/18/get/exported"
message = result[0] * 1000
elif str(i) == "storage_current_day_charge_capacity":
mqtttopic = "openWB/set/bat/17/get/imported"
message = result[0] * 1000
elif str(i) == "storage_current_day_discharge_capacity":
mqtttopic = "openWB/set/bat/17/get/exported"
message = result[0] * 1000
elif str(i) == "power_meter_active_power":
mqtttopic = "openWB/set/counter/16/get/power"
message = result[0]
elif str(i) == "active_grid_power_factor":
mqtttopic = "openWB/set/counter/16/get/power_factors"
message = result[0]
elif str(i) == "grid_frequency":
mqtttopic = "openWB/set/counter/16/get/frequency"
message = result[0]
# elif str(i) == "grid_voltage":
# mqtttopic = "openWB/counter/16/get/voltages"
# message = result[0]
# Handle other registers (in this case, the grid currents)
# Append numerical grid currents to the array
elif i in ['active_grid_A_current', 'active_grid_B_current', 'active_grid_C_current']:
mqtttopic = "openWB/set/counter/16/get/currents"
array_currents_grid = np.append(array_currents_grid, result[0])
# Handle other registers (in this case, the grid voltages)
# Append numerical grid voltages to the array
elif i in ['grid_A_voltage', 'grid_B_voltage', 'grid_C_voltage']:
mqtttopic = "openWB/set/counter/16/get/voltages"
array_voltages_grid = np.append(array_voltages_grid, result[0])
# Append numerical grid voltages to the array
elif i in ['active_grid_A_power', 'active_grid_B_power', 'active_grid_C_power']:
mqtttopic = "openWB/set/counter/16/get/powers"
array_powers_grid = np.append(array_powers_grid, result[0])
# elif i in ['active_grid_power_factor', 'active_grid_power_factor', 'active_grid_power_factor']:
# mqtttopic = "openWB/set/counter/16/get/power_factors"
# array_powerfactors_grid = np.append(array_powerfactors_grid, result[0])
if mqtttopic is not False:
register_values[i] = message
except:
await bridge.stop()
bridge = False
pass
# Publish the values to their respective topics
for register, value in register_values.items():
mqtttopic = topic_mapping.get(register)
if mqtttopic:
try:
if register == "active_grid_currents":
# Publish the current values as an array
publish.single(mqtttopic, payload=value, qos=0, retain=False, hostname=mqtthost,
client_id=mqttclient)
if register == "active_grid_voltages":
# Publish the voltage values as an array
publish.single(mqtttopic, payload=value, qos=0, retain=False, hostname=mqtthost,
client_id=mqttclient)
if register == "active_grid_powers":
# Publish the voltage values as an array
publish.single(mqtttopic, payload=value, qos=0, retain=False, hostname=mqtthost,
client_id=mqttclient)
# if register == "active_grid_powerfactors":
# # Publish the voltage values as an array
# publish.single(mqtttopic, payload=value, qos=0, retain=False, hostname=mqtthost,
# client_id=mqttclient)
else:
# Publish other values directly
publish.single(mqtttopic, payload=value, qos=0, retain=False, hostname=mqtthost,
client_id=mqttclient)
except:
pass
# Publish the grid currents as an array
if array_currents_grid.size > 0:
mqtttopic = "openWB/set/counter/16/get/currents"
payload_json = json.dumps(array_currents_grid.tolist()) # Convert to JSON string
publish.single(mqtttopic, payload=payload_json, qos=0, retain=False, hostname=mqtthost, client_id=mqttclient)
if array_voltages_grid.size > 0:
mqtttopic = "openWB/set/counter/16/get/voltages"
payload_json = json.dumps(array_voltages_grid.tolist()) # Convert to JSON string
publish.single(mqtttopic, payload=payload_json, qos=0, retain=False, hostname=mqtthost, client_id=mqttclient)
if array_powers_grid.size > 0:
mqtttopic = "openWB/set/counter/16/get/powers"
payload_json = json.dumps(array_powers_grid.tolist()) # Convert to JSON string
publish.single(mqtttopic, payload=payload_json, qos=0, retain=False, hostname=mqtthost, client_id=mqttclient)
# if array_powers_grid.size > 0:
# mqtttopic = "openWB/set/counter/16/get/power_factors"
# payload_json = json.dumps(array_powerfactors_grid.tolist()) # Convert to JSON string
# publish.single(mqtttopic, payload=payload_json, qos=0, retain=False, hostname=mqtthost, client_id=mqttclient)
time.sleep(0.5)
except KeyboardInterrupt:
await bridge.stop()
break
await bridge.stop()
loop.run_until_complete(huaweiReadValues(topic_mapping))