Hallo,
ich habe mir das beigefügtes Script zusammengebastelt.
Ich möchte damit einen Taster (Schalter abfragen) und damit ein paar 433Mz Steckdosen schalten.
Ich habe das Script bereits in abgewandelter Form für einen Bewegungsmelder zur Ab- und Abschaltung des Monitor in meinem Spiegel. Dort funktioniert es auch mit ca. 5 % CPU Auslastung wunderbar.
Das Script funktioniert auch für den Taster, allerdings laste ich damit die CPU (bzw. einen Core komplett aus).
Ich finde leider keinen Fehler, mit dem ich das (Performance-)Problem lösen könnte.
Findet Ihr etwas, oder gibt es einen besseren Weg, die Funktionen abzubilden. (gibt´s garantiert)
Ich danke euch schon einmal im Voraus.
Python
#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# taster-control.py
# Taster Control
#
## Import required Python libraries ##
import RPi.GPIO as GPIO
import time
import subprocess
import sys
## Define GPIO to use on Pi ##
GPIO.setmode(GPIO.BCM)
GPIO_Taster = 22
## Set pin as input ##
GPIO.setup(GPIO_Taster,GPIO.IN)
Current_Taster_State = 0
Previous_Taster_State = 0
## Switch off Fuse 1 ##
switch = subprocess.Popen("/script/taster/taster1off.sh", stdout=subprocess.PIPE, shell=True)
(switch_output, err) = switch.communicate()
switch_p_status = switch.wait()
## print "Command output : ", switch_output
Switch_State = 0
try:
## Loop until Taster output is 0 ##
while GPIO.input(GPIO_Taster)==1:
Current_Taster_State = 0
## Loop until users quits with CTRL-C ##
while True :
## Read state ##
Current_Taster_State = GPIO.input(GPIO_Taster)
if Current_Taster_State==1 and Previous_Taster_State==0:
## Taster pressed ##
## print " Taster pressed"
if Switch_State==0:
switch = subprocess.Popen("/script/taster/taster1on.sh", stdout=subprocess.PIPE, shell=True)
(switch_output, err) = switch.communicate()
switch_p_status = switch.wait()
## print "Command output : ", switch_output
Switch_State = 1
elif Switch_State==1:
switch = subprocess.Popen("/script/taster/taster1off.sh", stdout=subprocess.PIPE, shell=True)
(switch_output, err) = switch.communicate()
switch_p_status = switch.wait()
## print "Command output : ", switch_output
Switch_State = 0
## Record previous state ##
Previous_Taster_State=1
elif Current_Taster_State==0 and Previous_Taster_State==1:
Previous_Taster_State=0
## Check Interval ##
time.sleep(0.10)
except KeyboardInterrupt:
## Reset GPIO settings ##
GPIO.cleanup()
Alles anzeigen