Hallo Python Experten,
ich (Windows-Mensch), hat bedingt Ahnung von Linux und Python. möchte aber meinen PIR
dazu bringen, nach einer Zeit X den Bildschirm auszuschalten und bei Abstand unter 120cm vor dem PIR wieder einzuschalten.
Dazu benötige ich (warum auch immer) diese Befehle unter Bullseye:
Display DISPLAY=:0 xrandr --output HDMI-1 --auto --rotate left
DISPLAY=:0 xrandr --output HDMI-1 --off
Wenn ich diese 2 Befehle als User pi auf der Console ausführe, dann funktioniert das einwandfrei.
Nun habe ich gelesen, dass man Python Scripte auch so starten kann
DISPLAY=:0 python3 pir.py
Das Script startet oder Fehlermeldungen, aber schaltet den Bildschirm weder aus noch an ...
Wo liegt mein Fehler?
Am Ende soll das Script über /etc/rc.local beim Booten aus der /etc/rc.local gestartet werden.
Inhalt /etc/rc.local:
#!/bin/sh -e
#
# rc.local
#
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
# Start at Boot
/bin/sleep 60 && python3 /home/pi/MagicMirror/pir.py &
exit 0
Alles anzeigen
Und hier das Python Script:
#Bibliotheken einbinden
from subprocess import call
import RPi.GPIO as GPIO
import time
import logging
import subprocess # for command execution
def myfunc_Monitor_ON():
CONTROL = "xrandr" # command to turn the screen on
CONTROL_UNBLANK = [CONTROL, " --output HDMI-1", "--auto --rotate left"] # command to turn the screen on
subprocess.call(CONTROL_UNBLANK) # command to turn the screen on
def myfunc_Monitor_OFF():
CONTROL = "xrandr" # command to turn the screen off
CONTROL_UNBLANK = [CONTROL, " --output HDMI-1", "--off"] # command to turn the screen off
subprocess.call(CONTROL_UNBLANK) # command to turn the screen off
#GPIO Modus (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
#GPIO Pins zuweisen
GPIO_TRIGGER = 23
GPIO_ECHO = 24
Monitor_State = 0
Counter = 0
#Richtung der GPIO-Pins festlegen (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
#Monitor erst einmal ausschalten
myfunc_Monitor_OFF
# subprocess.call(CONTROL_OFF) # command to turn the screen off
def distanz():
# setze Trigger auf HIGH
GPIO.output(GPIO_TRIGGER, True)
# setze Trigger nach 0.01ms aus LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartZeit = time.time()
StopZeit = time.time()
# speichere Startzeit
while GPIO.input(GPIO_ECHO) == 0:
StartZeit = time.time()
# speichere Ankunftszeit
while GPIO.input(GPIO_ECHO) == 1:
StopZeit = time.time()
# Zeit Differenz zwischen Start und Ankunft
TimeElapsed = StopZeit - StartZeit
# mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
# und durch 2 teilen, da hin und zurueck
distanz = (TimeElapsed * 34300) / 2
return distanz
if __name__ == '__main__':
try:
while True:
abstand = distanz()
if abstand < 120 and Monitor_State == 0:
Monitor_State=1
Counter = 0
myfunc_Monitor_ON
elif abstand < 120 and Monitor_State == 1:
Counter = 0
elif abstand >= 120 and Monitor_State == 1 and Counter <= 10:
Counter += 1
elif abstand >= 100 and Counter >= 11:
Monitor_State=0
Counter = 0
myfunc_Monitor_OFF
time.sleep(1)
# Beim Abbruch durch STRG+C resetten
except KeyboardInterrupt:
GPIO.cleanup()
Alles anzeigen
Ich bin für jeden Tipp dankbar!