2022-07-09 21:51:40 +00:00
|
|
|
import time
|
|
|
|
import os
|
|
|
|
from datetime import datetime
|
2022-07-09 22:24:05 +00:00
|
|
|
import signal
|
|
|
|
import sys
|
2022-07-09 21:51:40 +00:00
|
|
|
try:
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import gpiozero
|
2022-07-09 22:02:25 +00:00
|
|
|
import RPi.GPIO as GPIO
|
2022-07-12 06:58:11 +00:00
|
|
|
import board
|
|
|
|
import busio
|
|
|
|
import adafruit_bmp280
|
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"')
|
2022-07-10 22:32:06 +00:00
|
|
|
print(ImportError)
|
2022-07-09 21:51:40 +00:00
|
|
|
exit()
|
2022-07-09 22:02:25 +00:00
|
|
|
|
2022-07-09 22:24:05 +00:00
|
|
|
sys.path.append('./SDL_Adafruit_ADS1x15')
|
|
|
|
import SDL_Adafruit_ADS1x15
|
2022-07-09 22:02:25 +00:00
|
|
|
|
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)
|
|
|
|
|
2022-07-09 22:24:05 +00:00
|
|
|
#relay
|
2022-07-09 21:51:40 +00:00
|
|
|
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:24:05 +00:00
|
|
|
#ultrasonic
|
|
|
|
|
2022-07-09 22:02:25 +00:00
|
|
|
def get_water_level():
|
2022-07-12 08:00:37 +00:00
|
|
|
GPIO.setmode(GPIO.BOARD)
|
|
|
|
GPIO.setwarnings(False)
|
|
|
|
GPIO_TRIGGER = 18
|
|
|
|
GPIO_ECHO = 24
|
|
|
|
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
|
|
|
|
GPIO.setup(GPIO_ECHO, GPIO.IN)
|
2022-07-09 22:02:25 +00:00
|
|
|
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
|
|
|
|
|
2022-07-12 07:09:10 +00:00
|
|
|
return int(distance)
|
2022-07-09 22:02:25 +00:00
|
|
|
|
2022-07-09 22:24:05 +00:00
|
|
|
#moisture
|
|
|
|
def signal_handler(signal, frame):
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
|
|
|
def get_moisture():
|
|
|
|
ADS1115 = 0x01
|
|
|
|
gain = 6144
|
|
|
|
sps = 250
|
|
|
|
adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115)
|
|
|
|
|
2022-07-10 16:58:12 +00:00
|
|
|
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
|
2022-07-10 17:44:39 +00:00
|
|
|
|
2022-07-12 06:58:11 +00:00
|
|
|
#temperature
|
|
|
|
def get_temperature():
|
|
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
2022-07-12 08:28:31 +00:00
|
|
|
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76)
|
2022-07-12 06:58:11 +00:00
|
|
|
return int(bmp280.temperature)
|
|
|
|
|
2022-07-12 08:26:01 +00:00
|
|
|
# print(get_moisture())
|
2022-07-12 08:27:38 +00:00
|
|
|
# print(get_water_level())
|
2022-07-12 07:10:45 +00:00
|
|
|
print(get_temperature())
|