Hmm, ich dachte, dass ich das ausreichend genug dargestellt hätte, aber dann mache ich das noch einmal komplett
Aktuell wird das Skript hier gestartet: /etc/rc.local
"Ja", es scheint auch andere ggf. bessere Orte zu geben, aber das wäre eine nachgelagerte Optimierung
Inhalt des rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
# Start at Boot
#DISPLAY=:0 python3 pirx.py
/bin/sleep 60 && python3 /home/pi/MagicMirror/pirx.py &
exit 0
Alles anzeigen
Inhalt des Python Skripts, welches hier liegt: /home/pi/MagicMirror/pirx.py
Also da, wo vermutet und aufgerufen ...
#Bibliotheken einbinden
from subprocess import call
import RPi.GPIO as GPIO
import time
import logging
import subprocess # for command execution
# xrandr -d :0 --output
def myfunc_Monitor_ON():
bashCommand = "xrandr -d :0 --output HDMI-1 --auto --rotate left"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
logging.info('MM ON')
def myfunc_Monitor_OFF():
bashCommand = "xrandr -d :0 --output HDMI-1 --off"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
logging.info('MM OFF')
def myfunc_Monitor_SLEEP():
CONTROL = "/bin/sleep"
CONTROL_UNBLANK = [CONTROL, "10"]
logging.info('MM SLEEP')
subprocess.call(CONTROL_UNBLANK)
#Einstellungen fr das Logging
logging.basicConfig(level=logging.INFO,
format='%(message)s',
datefmt='%m-%d %H:%M',
filename='/home/pi/MagicMirror/PIR.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
#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)
Text = (time.strftime("%d.%m.%Y %H:%M:%S")) + (" Messung vom User gestartet")
logging.info('Gestartet')
# Bildschirm erst einmal ausschalten :-)
myfunc_Monitor_OFF
print("Bildschirm erst einmal ausschalten")
myfunc_Monitor_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 < 100 and Monitor_State == 0:
Monitor_State=1
Counter = 0
Ausgabe = " Monitor gestartet : Counter = " + str(Counter) + (" - %.1f cm" % abstand)
logging.info(Ausgabe)
myfunc_Monitor_ON()
elif abstand < 100 and Monitor_State == 1:
Ausgabe = " Counter reset " + (" - %.1f cm" % abstand)
logging.info(Ausgabe)
Counter = 0
elif abstand >= 100 and Monitor_State == 1 and Counter <= 10:
Counter += 1
Ausgabe = " Counter = " + str(Counter) + (" - %.1f cm" % abstand)
logging.info(Ausgabe)
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:
# print(time.strftime("%d.%m.%Y %H:%M:%S")) + (" Messung vom User gestoppt")
# print("Messung vom User gestoppt")
GPIO.cleanup()
Alles anzeigen
Ohne weitere User Anmeldung über RDP reagiert der PIR nicht, da es (scheinbar) unter Bullseye mit dem DISPLAY Probleme gibt.
Wenn ich mit die Prozesse unter htop anschaue, dann läuft der Job unter dem User root.
Irgendwo habe ich gelesen, das man kms? in einer /boot/command.txt deaktivieren kann und so vcgencmd wieder verfügbar wäre.
Aber:
- ich habe keine /boot/command.txt auf meinem Raspi 4 / Bullseye
Wenn ich die Datei hätte, oder das sicher irgendwo anders gespeichert ist:
- ich weiß nicht, welche Nebeneffekte das mit sich bringen könnte
Jede Hilfe ist willkommen (habt Rücksicht mit mir - bin normalerweise Windows Mensch )