diff --git a/__pycache__/data_base_csv.cpython-312.pyc b/__pycache__/data_base_csv.cpython-312.pyc new file mode 100644 index 0000000..a9f0803 Binary files /dev/null and b/__pycache__/data_base_csv.cpython-312.pyc differ diff --git a/__pycache__/data_base_influx.cpython-312.pyc b/__pycache__/data_base_influx.cpython-312.pyc new file mode 100644 index 0000000..2cc1072 Binary files /dev/null and b/__pycache__/data_base_influx.cpython-312.pyc differ diff --git a/__pycache__/heat_pump.cpython-312.pyc b/__pycache__/heat_pump.cpython-312.pyc new file mode 100644 index 0000000..589611a Binary files /dev/null and b/__pycache__/heat_pump.cpython-312.pyc differ diff --git a/__pycache__/shelly_pro_3m.cpython-312.pyc b/__pycache__/shelly_pro_3m.cpython-312.pyc new file mode 100644 index 0000000..7c6e8f1 Binary files /dev/null and b/__pycache__/shelly_pro_3m.cpython-312.pyc differ diff --git a/data/.~lock.shelly_pro_3m_registers.xlsx# b/data/.~lock.shelly_pro_3m_registers.xlsx# new file mode 100644 index 0000000..fda7e61 --- /dev/null +++ b/data/.~lock.shelly_pro_3m_registers.xlsx# @@ -0,0 +1 @@ +,nils,nils-ThinkPad-P52,26.05.2025 20:45,file:///home/nils/.config/libreoffice/4; \ No newline at end of file diff --git a/data/ModBus TCPIP 1.17(1).xlsx b/data/heat_pump_registers.xlsx similarity index 100% rename from data/ModBus TCPIP 1.17(1).xlsx rename to data/heat_pump_registers.xlsx diff --git a/data/shelly_pro_3m_registers.xlsx b/data/shelly_pro_3m_registers.xlsx new file mode 100644 index 0000000..fc2bc02 Binary files /dev/null and b/data/shelly_pro_3m_registers.xlsx differ diff --git a/data_base_influx.py b/data_base_influx.py index 095fc78..ad0adfb 100644 --- a/data_base_influx.py +++ b/data_base_influx.py @@ -10,8 +10,8 @@ class DataBaseInflux: self.client = InfluxDBClient(url=self.url, token=self.token, org=self.org) self.write_api = self.client.write_api() - def store_data(self, data: dict): - measurement = "messungen" # Fest auf "messungen" gesetzt + def store_data(self, device_name: str, data: dict): + measurement = device_name # Fest auf "messungen" gesetzt point = Point(measurement) @@ -25,9 +25,4 @@ class DataBaseInflux: # Punkt in InfluxDB schreiben self.write_api.write(bucket=self.bucket, org=self.org, record=point) -db = DataBaseInflux( - url="http://localhost:8086", - token="Cw_naEZyvJ3isiAh1P4Eq3TsjcHmzzDFS7SlbKDsS6ZWL04fMEYixWqtNxGThDdG27S9aW5g7FP9eiq5z1rsGA==", - org="allmende", - bucket="allmende_db" -) + diff --git a/heat_pump.py b/heat_pump.py index 951518d..92b7dc7 100644 --- a/heat_pump.py +++ b/heat_pump.py @@ -3,7 +3,8 @@ import pandas as pd import time class HeatPump: - def __init__(self, ip_address: str): + def __init__(self, device_name: str, ip_address: str): + self.device_name = device_name self.ip = ip_address self.client = None self.connect_to_modbus() @@ -25,7 +26,7 @@ class HeatPump: def get_registers(self): # Excel-Datei mit den Input-Registerinformationen - excel_path = "data/ModBus TCPIP 1.17(1).xlsx" + excel_path = "data/heat_pump_registers.xlsx" xls = pd.ExcelFile(excel_path) df_input_registers = xls.parse('04 Input Register') diff --git a/main.py b/main.py index bc5d29b..72989c0 100644 --- a/main.py +++ b/main.py @@ -3,22 +3,24 @@ from datetime import datetime from data_base_csv import DataBaseCsv from data_base_influx import DataBaseInflux from heat_pump import HeatPump +from shelly_pro_3m import ShellyPro3m -interval = 10 # z.B. alle 10 Sekunden +interval = 10 -#db = DataBaseCsv('modbus_log.csv') db = DataBaseInflux( url="http://localhost:8086", token="Cw_naEZyvJ3isiAh1P4Eq3TsjcHmzzDFS7SlbKDsS6ZWL04fMEYixWqtNxGThDdG27S9aW5g7FP9eiq5z1rsGA==", org="allmende", bucket="allmende_db" ) -hp = HeatPump(ip_address='10.0.0.10') + +hp = HeatPump(device_name='hp_master', ip_address='10.0.0.10') +shelly = ShellyPro3m(device_name='wohnung_2_6', ip_address='192.168.1.121') while True: now = datetime.now() if now.second % interval == 0 and now.microsecond < 100_000: - db.store_data(hp.get_data()) - + db.store_data(hp.device_name, hp.get_data()) + db.store_data(shelly.device_name, shelly.get_data()) time.sleep(0.1) diff --git a/shelly_pro_3m.py b/shelly_pro_3m.py new file mode 100644 index 0000000..581cf99 --- /dev/null +++ b/shelly_pro_3m.py @@ -0,0 +1,60 @@ +from pymodbus.client import ModbusTcpClient +import pandas as pd +import time + +class ShellyPro3m: + def __init__(self, device_name: str, ip_address: str): + self.device_name = device_name + self.ip = ip_address + self.client = None + self.connect_to_modbus() + self.registers = None + self.get_registers() + + def connect_to_modbus(self): + port = 502 + self.client = ModbusTcpClient(self.ip, port=port) + try: + if not self.client.connect(): + print("Verbindung zur Wärmepumpe fehlgeschlagen.") + exit(1) + print("Verbindung zur Wärmepumpe erfolgreich.") + except KeyboardInterrupt: + print("Beendet durch Benutzer (Ctrl+C).") + finally: + self.client.close() + + def get_registers(self): + # Excel-Datei mit den Input-Registerinformationen + excel_path = "data/shelly_pro_3m_registers.xlsx" + xls = pd.ExcelFile(excel_path) + df_input_registers = xls.parse() + + # Relevante Spalten bereinigen + df_clean = df_input_registers[['MB Adresse', 'Beschreibung', 'Variabel Typ']].dropna() + df_clean['MB Adresse'] = df_clean['MB Adresse'].astype(int) + + # Dictionary aus Excel erzeugen + self.registers = { + row['MB Adresse']: { + 'desc': row['Beschreibung'], + 'type': 'REAL' if row['Variabel Typ'] == 'REAL' else 'INT' + } + for _, row in df_clean.iterrows() + } + + def get_data(self): + data = {} + data['Zeit'] = time.strftime('%Y-%m-%d %H:%M:%S') + for address, info in self.registers.items(): + reg_type = info['type'] + result = self.client.read_input_registers(address, count=2 if reg_type == 'REAL' else 1) + if result.isError(): + print(f"Fehler beim Lesen von Adresse {address}: {result}") + continue + + value = result.registers[0] / 10.0 + + print(f"Adresse {address} - {info['desc']}: {value}") + data[f"{address} - {info['desc']}"] = value + return data