From 160b724de59b438945b06094b99b0720096478c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Thu, 4 Aug 2022 19:14:59 +0200 Subject: [PATCH] added ultrasonic back MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- main.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index e0f7eb0..f7c1d9a 100644 --- a/main.py +++ b/main.py @@ -40,35 +40,35 @@ def relay_controller(pin: int, state: str): error_print("Unknown state ("+state+") for relay channel "+str(pin)) #ultrasonic -# try: -# def get_water_level(): -# GPIO.setmode(GPIO.BCM) -# GPIO.setwarnings(False) -# GPIO_TRIGGER = 24 -# GPIO_ECHO = 25 -# GPIO.setup(GPIO_TRIGGER, GPIO.OUT) -# GPIO.setup(GPIO_ECHO, GPIO.IN) -# GPIO.output(GPIO_TRIGGER, True) -# time.sleep(0.00001) -# GPIO.output(GPIO_TRIGGER, False) +try: + def get_water_level(): + GPIO.setmode(GPIO.BCM) + GPIO.setwarnings(False) + GPIO_TRIGGER = 24 + GPIO_ECHO = 25 + GPIO.setup(GPIO_TRIGGER, GPIO.OUT) + GPIO.setup(GPIO_ECHO, GPIO.IN) + GPIO.output(GPIO_TRIGGER, True) + time.sleep(0.00001) + GPIO.output(GPIO_TRIGGER, False) -# startTime = time.time() -# StopTime = time.time() + startTime = time.time() + StopTime = time.time() -# while GPIO.input(GPIO_ECHO) == 0: -# startTime = time.time() -# while GPIO.input(GPIO_ECHO) == 1: -# StopTime = time.time() + while GPIO.input(GPIO_ECHO) == 0: + startTime = time.time() + while GPIO.input(GPIO_ECHO) == 1: + StopTime = time.time() -# TimeElapsed = StopTime - startTime -# distance = (TimeElapsed * 34300) / 2 -# rounded_distance = int(distance) -# return str(rounded_distance) + TimeElapsed = StopTime - startTime + distance = (TimeElapsed * 34300) / 2 + rounded_distance = int(distance) + return str(rounded_distance) -# print(get_water_level()) -# debug_print("Added water level detector") -# except: -# print("Adding water level sensor fail.") + print(get_water_level()) + debug_print("Added water level detector") +except: + print("Adding water level sensor fail.") #temperature try: