Code: Alles auswählen
#Bibliotheken
import RPi.GPIO as GPIO
import time
import logging
logging.basicConfig(filename='teich.log', filemode='w', level=logging.INFO)
#GPIO definieren (Modus, Pins, Output)
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def entfernung():
# Trig High setzen
GPIO.output(GPIO_TRIGGER, True)
# Trig Low setzen (nach 0.01ms)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
Startzeit = time.time()
Endzeit = time.time()
# Start/Stop Zeit ermitteln
while GPIO.input(GPIO_ECHO) == 0:
Startzeit = time.time()
while GPIO.input(GPIO_ECHO) == 1:
Endzeit = time.time()
# Vergangene Zeit
Zeitdifferenz = Endzeit - Startzeit
# Schallgeschwindigkeit (34300 cm/s) einbeziehen
entfernung = 133 - (Zeitdifferenz * 34300) / 2
logging.info('Skript gestartet')
return entfernung
if __name__ == '__main__':
while True:
distanz = entfernung()
print (" %.1f cm" % distanz)
GPIO.cleanup()
Code: Alles auswählen
2020-07-11 21:22:46.828 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Teichstand': For input string: "110.7 cm
Traceback (most recent call last):
File "/etc/openhab2/scripts/hc.py", line 44, in <module>
distanz = entfernung()
File "/etc/openhab2/scripts/hc.py", line 18, in entfernung
GPIO.output(GPIO_TRIGGER, True)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Traceback (most recent call last):
File "/etc/openhab2/scripts/hc.py", line 44, in <module>
distanz = entfernung()
File "/etc/openhab2/scripts/hc.py", line 18, in entfernung
GPIO.output(GPIO_TRIGGER, True)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)"
Code: Alles auswählen
[17:08:52] openhabian@openhab:~$ sudo python /etc/openhab2/scripts/hc.py
[sudo] Passwort für openhabian:
110.6 cm
Traceback (most recent call last):
File "/etc/openhab2/scripts/hc.py", line 44, in <module>
distanz = entfernung()
File "/etc/openhab2/scripts/hc.py", line 18, in entfernung
GPIO.output(GPIO_TRIGGER, True)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)