initialer commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.venv
|
||||
data.db
|
||||
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()
|
||||
24
config.json
Normal file
24
config.json
Normal file
@@ -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
|
||||
}
|
||||
18
db.py
Normal file
18
db.py
Normal file
@@ -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))
|
||||
9
rain_sensor.py
Normal file
9
rain_sensor.py
Normal file
@@ -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)
|
||||
9
temp_sensor.py
Normal file
9
temp_sensor.py
Normal file
@@ -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)
|
||||
9
wind_sensor.py
Normal file
9
wind_sensor.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user