Ich bin ja kein Programmierer, aber so vielleicht:
Python
#!/usr/bin/env python3
import pygame
import RPi.GPIO as GPIO
from time import sleep
pygame.mixer.init()
open = pygame.mixer.Sound('/home/pi/Music/Creaking Door.wav')
open.set_volume(1.0)
GPIO.setmode(GPIO.BCM)
PIR_SENSOR = 24
RELAIS1_UP = 13
RELAIS2_DOWN = 22
RELAIS3_NEBEL = 12
RELAIS4_LICHT = 23
GPIO.setup(PIR_SENSOR, GPIO.IN)
GPIO.setup(RELAIS1_UP, GPIO.OUT)
GPIO.setup(RELAIS2_DOWN, GPIO.OUT)
GPIO.setup(RELAIS3_NEBEL, GPIO.OUT)
GPIO.setup(RELAIS4_LICHT, GPIO.OUT)
GPIO.output(RELAIS1_UP, GPIO.HIGH)
GPIO.output(RELAIS2_DOWN, GPIO.HIGH)
GPIO.output(RELAIS3_NEBEL, GPIO.HIGH)
GPIO.output(RELAIS4_LICHT, GPIO.HIGH)
INTERVALL = 180
def main():
while True:
if GPIO.input(PIR_SENSOR) == GPIO.LOW:
print("Keine Bewegung erkannt")
sleep(1)
if GPIO.input(PIR_SENSOR) == GPIO.HIGH:
print("Bewegung erkannt")
sleep(2)
GPIO.output(RELAIS3_NEBEL, GPIO.LOW) # Nebel an
print("Nebel an")
sleep(4)
GPIO.output(RELAIS3_NEBEL, GPIO.HIGH) # Nebel aus
print("Nebel aus")
for i in range(2):
GPIO.output(RELAIS1_UP, GPIO.LOW)
print("Relais1 an")
sleep(0.3) # Ausfahren
GPIO.output(RELAIS1_UP, GPIO.HIGH)
print("Relais1 aus")
sleep(0.15)
GPIO.output(RELAIS2_DOWN, GPIO.LOW)
print("Relais1 an")
sleep(0.3) # Einfahren
GPIO.output(RELAIS2_DOWN , GPIO.HIGH)
print("Relais1 aus")
sleep(1)
GPIO.output(RELAIS1_UP, GPIO.LOW)
print("Relais1 an")
pygame.mixer.Sound.play(open)
print("Sound spielt")
sleep(4) # Linearantrieb Ausfahren
GPIO.output(RELAIS1_UP, GPIO.HIGH)
print("Relais1 aus")
sleep(5)
GPIO.output(RELAIS4_LICHT, GPIO.HIGH) # Licht an
print("Licht an")
GPIO.output(RELAIS2_DOWN, GPIO.LOW)
print("Relais2 an")
sleep(4) # Linearantrieb einfahren
GPIO.output(RELAIS2_DOWN, GPIO.HIGH)
print("Relais2 aus")
GPIO.output(RELAIS4_LICHT, GPIO.LOW) # Licht aus
print("Licht aus")
print("Warte {} Sekunden".format(INTERVALL))
sleep(INTERVALL)
else:
sleep(1)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Abbruch durch Benutzer.")
finally:
print("Räume GPIOs auf.")
GPIO.cleanup()
print("Bye bye")
Alles anzeigen
Ausgabe:
Code
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Bewegung erkannt
Nebel an
Nebel aus
Relais1 an
Relais1 aus
Relais1 an
Relais1 aus
Relais1 an
Sound spielt
Relais1 aus
Licht an
Relais2 an
Relais2 aus
Licht aus
Warte 180 Sekunden
Relais1 an
Relais1 aus
Relais1 an
Relais1 aus
Relais1 an
Sound spielt
Relais1 aus
Licht an
Relais2 an
Relais2 aus
Licht aus
Warte 180 Sekunden
^CAbbruch durch Benutzer.
Räume GPIOs auf.
Bye bye
Alles anzeigen
Ohne Bewegung:
Code
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
Keine Bewegung erkannt
^CAbbruch durch Benutzer.
Räume GPIOs auf.
Bye bye
Alles anzeigen