added ultrasonic

Signed-off-by: Ebbe Baß <ebbe@ping-mee.de>
master
Ebbe Baß 2022-07-10 00:02:25 +02:00
parent 8fd8483121
commit 60ab07dbc6
1 changed files with 28 additions and 2 deletions

30
main.py
View File

@ -4,9 +4,19 @@ from datetime import datetime
try: try:
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import gpiozero import gpiozero
global GPIO
import RPi.GPIO as GPIO
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"')
exit() exit()
GPIO.setmode(GPIO.BCM)
# setting up ultrasonic pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def debug_print(message: str): def debug_print(message: str):
now = datetime.now() now = datetime.now()
@ -28,5 +38,21 @@ def relay_controller(pin: int, status: str):
else: else:
error_print("Unknown status for relay ("+status+")") error_print("Unknown status for relay ("+status+")")
def get_water_lvl(): 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