2022-07-09 21:51:40 +00:00
|
|
|
import time
|
|
|
|
import os
|
|
|
|
from datetime import datetime
|
|
|
|
try:
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import gpiozero
|
2022-07-09 22:02:25 +00:00
|
|
|
global GPIO
|
|
|
|
import RPi.GPIO as GPIO
|
2022-07-09 21:51:40 +00:00
|
|
|
except ImportError:
|
|
|
|
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
|
|
|
exit()
|
2022-07-09 22:02:25 +00:00
|
|
|
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)
|
|
|
|
|
2022-07-09 21:51:40 +00:00
|
|
|
|
|
|
|
def debug_print(message: str):
|
|
|
|
now = datetime.now()
|
|
|
|
current_time = now.strftime("%H:%M:%S")
|
|
|
|
print('[DEBUG]['+current_time+'] '+message)
|
|
|
|
|
|
|
|
def error_print(message: str):
|
|
|
|
now = datetime.now()
|
|
|
|
current_time = now.strftime("%H:%M:%S")
|
|
|
|
print('[ERROR]['+current_time+'] '+message)
|
|
|
|
|
|
|
|
def relay_controller(pin: int, status: str):
|
|
|
|
if status == "ON":
|
|
|
|
relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
|
|
|
relay.on()
|
|
|
|
elif status == "OFF":
|
|
|
|
relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
|
|
|
relay.off()
|
|
|
|
else:
|
|
|
|
error_print("Unknown status for relay ("+status+")")
|
|
|
|
|
2022-07-09 22:02:25 +00:00
|
|
|
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
|
|
|
|
|