ultrasonic test

Signed-off-by: Ebbe Baß <ebbe@ping-mee.de>
master
Ebbe Baß 2022-07-12 08:58:11 +02:00
parent d8da243a07
commit 40b68af9aa
2 changed files with 30 additions and 39 deletions

View File

@ -8,6 +8,9 @@ try:
import gpiozero import gpiozero
global GPIO global GPIO
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import board
import busio
import adafruit_bmp280
except ImportError: except ImportError:
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"') print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
print(ImportError) 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 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_moisture())
print(get_water_level()) print(get_water_level())

60
test.py
View File

@ -1,48 +1,30 @@
import board global GPIO
import busio import RPi.GPIO as GPIO
import adafruit_bmp280
import time import time
# def get_temperature(): GPIO.setmode(GPIO.BCM)
# i2c = busio.I2C(board.SCL, board.SDA)
# bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x03)
# return bmp280.temperature
count = 2 GPIO_TRIGGER = 18
i2c = busio.I2C(board.SCL, board.SDA) GPIO_ECHO = 24
while True: GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76) GPIO.setup(GPIO_ECHO, GPIO.IN)
print(int(bmp280.temperature))
# print(get_temperature()
def get_water_level():
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
# global GPIO startTime = time.time()
# import RPi.GPIO as GPIO StopTime = time.time()
# import time
# GPIO.setmode(GPIO.BCM) while GPIO.input(GPIO_ECHO) == 0:
startTime = time.time()
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# GPIO_TRIGGER = 18 TimeElapsed = StopTime - startTime
# GPIO_ECHO = 24 distance = (TimeElapsed * 34300) / 2
# GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
# GPIO.setup(GPIO_ECHO, GPIO.IN)
# def get_water_level(): return distance
# GPIO.output(GPIO_TRIGGER, True)
# time.sleep(0.00001)
# GPIO.output(GPIO_TRIGGER, False)
# startTime = time.time() print(get_water_level())
# 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())