#!/usr/bin/python #coding =utf-8 import time import threading import RPi.GPIO as GPIO import os, sys timestamp_lock = threading.Lock() pipeName = '/var/www/newpipe' last_timestamp = time.time() aktiv = 0 GPIO.setmode (GPIO.BOARD) pwm=[0 for i in range(41)] def check_heartbeat(): global last_timestamp global timestamp_lock global pwm global aktiv while True: timestamp_lock.acquire() my_last_timestamp = last_timestamp timestamp_lock.release() if (((time.time() - my_last_timestamp) > 2) and ( aktiv != 0)):# die zahl ist die wartezeit auf das signal von der fernsteuerung #verbindung ist unterbrochen. GPIO.setup(12, GPIO.OUT) GPIO.setup(8, GPIO.OUT) GPIO.setup(10, GPIO.OUT) pwm[12] = GPIO.PWM(12, 50 ) pwm[8] = GPIO.PWM(8, 1 ) pwm[10] = GPIO.PWM(10, 1 ) pwm[12].stop()#motor ausschalten pwm[8].stop()#blinker rects ausschalten pwm[10].stop()#blinker links ausschalten pwm[8].start(50)#blinker rects einschalten pwm[10].start(50)#blinker links einschalten print ("Motor ausgeschaltet wegen verbindungsproblem") t.kill() time.sleep(0.5) t = threading.Thread(target=check_heartbeat, args=()) t.start() try: print("hi") #os.unlink(pipeName) except: print("unlink failed") pass try: os.mkfifo(pipeName) except: print("mkfifo failed") pass pipe = open(pipeName, 'r') data_alt = "" while True: data = pipe.readline()#erwartetes format: zustand, pin, frequenz, dc if (data != '' and data != data_alt): data_alt = data #print (repr(data), 'aaa') #print ((data), 'aaa') #wurde verschoben arrdata = data.split(',') zustand = int(arrdata[0]) if zustand != 2 and zustand != 3: pin = int(arrdata[1]) if zustand == 0 or zustand == 1: fq = int(arrdata[2]) dc = float(arrdata[3]) else: pin = 0 fq = 0 dc = 0 if (zustand < 0 or zustand > 5): print("ERROR: zustand muss zwischen 0 und 5 sein!") elif ((pin <= 0 or pin > 40) and zustand != 2 and zustand != 3): print("ERROR: pin muss zwischen 1 und 40 sein!") elif ((fq < 0 or fq > 1023) and zustand < 2): print("ERROR: Frequenz muss zwischen 0 und 1023 sein!") elif ((dc < 0 or dc > 100) and zustand < 2): print("ERROR: DutyCicle muss zwischen 0 und 100 sein!") else: if (aktiv == 0 or zustand == 3): if zustand == 3: print("started!") aktiv = 1 else: print ((data), 'aaa') #rausschreiben if zustand == 1 : GPIO.setup(pin, GPIO.OUT) pwm[pin] = GPIO.PWM(pin, fq ) pwm[pin].stop() pwm[pin].start(dc) if pin == 15: time.sleep(0.3) pwm[pin].stop() #GPIO.output(10, GPIO.HIGH) elif zustand == 0 : GPIO.setup(pin, GPIO.OUT) pwm[pin] = GPIO.PWM(pin, fq ) pwm[pin].stop() #GPIO.cleanup() #GPIO.output(10, GPIO.LOW) elif (zustand == 2):#heartbeat timestamp_lock.acquire() last_timestamp = time.time() timestamp_lock.release() elif (zustand == 4):#gpio ein GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.HIGH) elif (zustand == 5):#gpio aus GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.LOW) else : print ("war nix") print ("While wurde beendet!")