From 082094ccc570cf3c9e9ecb3bdac15c1574815d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Mon, 11 Jul 2022 01:17:29 +0200 Subject: [PATCH] test for ultrasonic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- main.py | 1 - test.py | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index cd44ab0..64543a0 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,6 @@ def error_print(message: str): print('[ERROR]['+current_time+'] '+message) #relay - def relay_controller(pin: int, status: str): if status == "ON": relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False) diff --git a/test.py b/test.py index 447f4e6..04c007a 100644 --- a/test.py +++ b/test.py @@ -1,13 +1,42 @@ -import board -import busio -import adafruit_bmp280 +# import board +# import busio +# import adafruit_bmp280 +# import time + +# def get_temperature(): +# i2c = busio.I2C(board.SCL, board.SDA) +# bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76) +# return bmp280.temperature + +# while True: +# print(get_temperature()) +# time.sleep(1) + +global GPIO +import RPi.GPIO as GPIO import time -def get_temperature(): - i2c = busio.I2C(board.SCL, board.SDA) - bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76) - return bmp280.temperature +GPIO_TRIGGER = 18 +GPIO_ECHO = 24 +GPIO.setup(GPIO_TRIGGER, GPIO.OUT) +GPIO.setup(GPIO_ECHO, GPIO.IN) -while True: - print(get_temperature()) - time.sleep(1) \ No newline at end of file +def get_water_level(): + GPIO.output(GPIO_TRIGGER, True) + time.sleep(0.00001) + GPIO.output(GPIO_TRIGGER, False) + + startTime = time.time() + 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 + + return distance + +print(get_water_level()) \ No newline at end of file