initialer commit
This commit is contained in:
87
app.py
Normal file
87
app.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user