70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
import time
|
|
from datetime import datetime
|
|
try:
|
|
import paho.mqtt.client as mqtt
|
|
import gpiozero
|
|
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)
|
|
exit()
|
|
|
|
|
|
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)
|
|
|
|
#relay
|
|
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+")")
|
|
|
|
#ultrasonic
|
|
|
|
def get_water_level():
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setwarnings(False)
|
|
GPIO_TRIGGER = 24
|
|
GPIO_ECHO = 8
|
|
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
|
|
GPIO.setup(GPIO_ECHO, GPIO.IN)
|
|
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 int(distance)
|
|
|
|
#temperature
|
|
def get_temperature():
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
|
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76)
|
|
return int(bmp280.temperature)
|
|
|
|
print(get_water_level())
|
|
print(get_temperature()) |