strawberry-pi-greenhouse/main.py

143 lines
4.5 KiB
Python
Raw Normal View History

from string import whitespace
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()
RELAY_PIN_CH_1 = 4
RELAY_PIN_CH_2 = 17
RELAY_PIN_CH_3 = 27
RELAY_PIN_CH_4 = 11
global relay_ch_1
relay_ch_1 = gpiozero.OutputDevice(RELAY_PIN_CH_1, active_high=True, initial_value=False)
# global relay_ch_2
# relay_ch_2 = gpiozero.OutputDevice(RELAY_PIN_CH_2, active_high=True, initial_value=False)
# global relay_ch_3
# relay_ch_3 = gpiozero.OutputDevice(RELAY_PIN_CH_3, active_high=True, initial_value=False)
# global relay_ch_4
# relay_ch_4 = gpiozero.OutputDevice(RELAY_PIN_CH_4, active_high=True, initial_value=False)
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
rounded_distance = int(distance)
return str(rounded_distance)
#temperature
def get_temperature():
i2c = busio.I2C(board.SCL, board.SDA)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76)
rounded_temp = int(bmp280.temperature)
return str(rounded_temp)
print(get_water_level())
print(get_temperature())
def mqtt_on_connect(client, userdata, flags, rc):
client.subscribe("strawberry-pi-greenhouse/relay/channel/1")
client.subscribe("strawberry-pi-greenhouse/relay/channel/2")
client.subscribe("strawberry-pi-greenhouse/relay/channel/3")
client.subscribe("strawberry-pi-greenhouse/relay/channel/4")
def mqtt_on_message(client, userdata, msg):
print("topic: " ,str(msg.topic))
print("payload: " ,str(msg.payload.decode("utf-8")))
topic = str(msg.topic)
payload = str(msg.payload.decode("utf-8"))
if topic == "strawberry-pi-greenhouse/relay/channel/1":
if payload == "on":
relay_ch_1.on()
elif payload == "off":
relay_ch_1.off()
elif topic == "strawberry-pi-greenhouse/relay/channel/2":
if payload == "on":
relay_ch_2.on()
elif payload == "off":
relay_ch_2.off()
elif topic == "strawberry-pi-greenhouse/relay/channel/3":
if payload == "on":
relay_ch_3.on()
elif payload == "off":
relay_ch_3.off()
elif topic == "strawberry-pi-greenhouse/relay/channel/4":
if payload == "on":
relay_ch_4.on()
elif payload == "off":
relay_ch_4.off()
if __name__ == "__main__":
mqttBroker ="homeassistant.ping-mee.local"
client = mqtt.Client("greenhouse_client")
client.username_pw_set("mqtt", "pmMQTT_11!")
debug_print("Connecting to MQTT Broker "+str(mqttBroker))
client.connect(mqttBroker)
client.on_connect = mqtt_on_connect
client.on_message = mqtt_on_message
client.loop_start()
try:
client.loop_start()
while True:
client.publish("strawberry-pi-greenhouse/sensor/temperature", get_temperature())
debug_print("Published current temperature: "+get_temperature())
client.publish("strawberry-pi-greenhouse/sensor/water-level", get_water_level())
debug_print("Published current water level: "+get_water_level())
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()