From 0f8507284fff2ab43ab89ac88d84c252099d0875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Br=C3=BC=C3=9Fler?= Date: Mon, 9 Jun 2025 13:18:00 +0200 Subject: [PATCH] initialer commit --- .gitignore | 2 ++ app.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ config.json | 24 ++++++++++++++ db.py | 18 +++++++++++ rain_sensor.py | 9 ++++++ temp_sensor.py | 9 ++++++ wind_sensor.py | 9 ++++++ 7 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 config.json create mode 100644 db.py create mode 100644 rain_sensor.py create mode 100644 temp_sensor.py create mode 100644 wind_sensor.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..74a3340 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +data.db \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..33ca23c --- /dev/null +++ b/app.py @@ -0,0 +1,87 @@ +import sys +import json +import RPi.GPIO as GPIO +from rain_sensor import RainSensor +from temp_sensor import TempSensor +from wind_sensor import WindSensor +from db import log_to_db + +def calc_average_temp(sensor_list): + total = 0 + count = 0 + for sensor in sensor_list: + try: + value = float(sensor.read_data()) + total += value + count += 1 + except Exception: + continue + return (total / count) if count > 0 else None + + + + + +GPIO.setmode(GPIO.BCM) + +# Konfigration laden +config = None +with open("config.json") as f: + config = json.load(f) + +if not config: + sys.exit("Konfigurationsdatei nicht gefunden.") + + + +# Sensoren initialisieren +rain_config = config["rain_sensors"] +temp_config = config["temp_sensors"] +wind_config = config["wind_sensors"] + +rain_sensors = [] +for entry in rain_config: + sensor = RainSensor(int(entry["pin"])) + rain_sensors.append(sensor) + +temp_sensors = [] +for entry in temp_config: + sensor = TempSensor(int(entry["pin"])) + temp_sensors.append(sensor) + +wind_sensors = [] +for entry in wind_config: + sensor = WindSensor(int(entry["pin"])) + wind_sensors.append(sensor) + + + + +# Temperatur auslesen und Mittel bestimmen +temp = calc_average_temp(temp_sensors) +rain = None # Logik für die beiden hier noch implementieren, ggf Temp Logik anpassen +wind = None + + +# Bedingungen für den Actuator +threshold = int(config["temp_threshold"]) + +roof_should_open = ( + # rain == 0 and # falls 0/1 für trocken / nass + # wind < 1 and # ebenso für Wind + temp >= threshold +) + +# Actuator +act_pin = int(config["actuator_pin"]) +GPIO.setup(act_pin, GPIO.OUT) + +if roof_should_open: + GPIO.output(act_pin, GPIO.HIGH) +else: + GPIO.output(act_pin, GPIO.LOW) + +# in die DB schreiben +log_to_db(temp, rain, wind, int(roof_should_open)) + +GPIO.cleanup() diff --git a/config.json b/config.json new file mode 100644 index 0000000..c12f1e9 --- /dev/null +++ b/config.json @@ -0,0 +1,24 @@ +{ + "rain_sensors": [ + { + "name": "rain 1", + "pin": 0 + } + ], + + "temp_sensors": [ + { + "name": "temp 1", + "pin": 0 + } + ], + + "wind_sensors": [ + { + "name": "wind 1", + "pin": 0 + } + ], + "temp_threshold": 20, + "actuator_pin": 0 +} \ No newline at end of file diff --git a/db.py b/db.py new file mode 100644 index 0000000..b132b3e --- /dev/null +++ b/db.py @@ -0,0 +1,18 @@ +import sqlite3 +from datetime import datetime + +def log_to_db(temp=None, rain=None, wind=None, roof_state=None): + with sqlite3.connect("data.db") as conn: + cur=conn.cursor() + setup = ''' + CREATE TABLE IF NOT EXISTS logs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + timestamp TEXT NOT NULL, + temp INTEGER, + rain INTEGER, + wind INTEGER, + roof_open INTEGER + )''' + cur.execute(setup) + query = 'INSERT INTO logs (timestamp, temp, rain, wind, roof_open) VALUES (?, ?, ?, ?, ?)' + cur.execute(query, (datetime.now(), temp, rain, wind, roof_state)) diff --git a/rain_sensor.py b/rain_sensor.py new file mode 100644 index 0000000..5e150df --- /dev/null +++ b/rain_sensor.py @@ -0,0 +1,9 @@ +import RPi.GPIO as GPIO + +class RainSensor: + def __init__(self, pin): + self.pin = pin + GPIO.setup(pin, GPIO.IN) + + def read_data(self): + return GPIO.input(self.pin) \ No newline at end of file diff --git a/temp_sensor.py b/temp_sensor.py new file mode 100644 index 0000000..046723a --- /dev/null +++ b/temp_sensor.py @@ -0,0 +1,9 @@ +import RPi.GPIO as GPIO + +class TempSensor: + def __init__(self, pin): + self.pin = pin + GPIO.setup(pin, GPIO.IN) + + def read_data(self): + return GPIO.input(self.pin) \ No newline at end of file diff --git a/wind_sensor.py b/wind_sensor.py new file mode 100644 index 0000000..605ddd9 --- /dev/null +++ b/wind_sensor.py @@ -0,0 +1,9 @@ +import RPi.GPIO as GPIO + +class WindSensor: + def __init__(self, pin): + self.pin = pin + GPIO.setup(pin, GPIO.IN) + + def read_data(self): + return GPIO.input(self.pin) \ No newline at end of file