From 40b68af9aa07cc12070ad25463e848d541ff3f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Tue, 12 Jul 2022 08:58:11 +0200 Subject: [PATCH] ultrasonic test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- main.py | 9 +++++++++ test.py | 60 ++++++++++++++++++++------------------------------------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index 64543a0..8542bd7 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,9 @@ try: import gpiozero global GPIO import RPi.GPIO as GPIO + import board + import busio + import adafruit_bmp280 except ImportError: print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"') print(ImportError) @@ -77,5 +80,11 @@ def get_moisture(): return adc.readADCSingleEnded(0, gain, sps) / 1000 + adc.readADCSingleEnded(1, gain, sps) / 1000 + adc.readADCSingleEnded(2, gain, sps) / 1000 + adc.readADCSingleEnded(3, gain, sps) / 1000 +#temperature +def get_temperature(): + i2c = busio.I2C(board.SCL, board.SDA) + bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x03) + return int(bmp280.temperature) + print(get_moisture()) print(get_water_level()) \ No newline at end of file diff --git a/test.py b/test.py index 1381259..d7caf69 100644 --- a/test.py +++ b/test.py @@ -1,48 +1,30 @@ -import board -import busio -import adafruit_bmp280 +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=0x03) -# return bmp280.temperature +GPIO.setmode(GPIO.BCM) -count = 2 -i2c = busio.I2C(board.SCL, board.SDA) -while True: - bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76) - print(int(bmp280.temperature)) - # print(get_temperature() +GPIO_TRIGGER = 18 +GPIO_ECHO = 24 +GPIO.setup(GPIO_TRIGGER, GPIO.OUT) +GPIO.setup(GPIO_ECHO, GPIO.IN) - -# global GPIO -# import RPi.GPIO as GPIO -# import time - -# GPIO.setmode(GPIO.BCM) - -# GPIO_TRIGGER = 18 -# GPIO_ECHO = 24 -# GPIO.setup(GPIO_TRIGGER, GPIO.OUT) -# GPIO.setup(GPIO_ECHO, GPIO.IN) - -# def get_water_level(): -# GPIO.output(GPIO_TRIGGER, True) -# time.sleep(0.00001) -# GPIO.output(GPIO_TRIGGER, False) +def get_water_level(): + 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 + TimeElapsed = StopTime - startTime + distance = (TimeElapsed * 34300) / 2 -# return distance + return distance -# print(get_water_level()) \ No newline at end of file +print(get_water_level()) \ No newline at end of file