strawberry-pi-greenhouse/main.py

139 lines
4.6 KiB
Python

from string import whitespace
import time
from datetime import datetime
import os
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, state: str):
if state == "True":
os.system('echo "1" > /sys/class/gpio/gpio'+str(pin)+'/value')
debug_print("Set relay-pin "+str(pin)+" to state True")
elif state == "False":
os.system('echo "0" > /sys/class/gpio/gpio'+str(pin)+'/value')
debug_print("Set relay-pin "+str(pin)+" to state False")
else:
error_print("Unknown state ("+state+") for relay channel "+str(pin))
#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"))
channel_1 = 19
channel_2 = 16
channel_3 = 26
channel_4 = 20
if topic == "strawberry-pi-greenhouse/relay/channel/1":
if payload == "on":
relay_controller(channel_1, "True")
elif payload == "off":
relay_controller(channel_1, "False")
elif topic == "strawberry-pi-greenhouse/relay/channel/2":
if payload == "on":
relay_controller(channel_2, "True")
elif payload == "off":
relay_controller(channel_2, "False")
elif topic == "strawberry-pi-greenhouse/relay/channel/3":
if payload == "on":
relay_controller(channel_3, "True")
elif payload == "off":
relay_controller(channel_3, "False")
elif topic == "strawberry-pi-greenhouse/relay/channel/4":
if payload == "on":
relay_controller(channel_4, "True")
elif payload == "off":
relay_controller(channel_4, "False")
if __name__ == "__main__":
relay_channels = ["19", "16", "26", "20"]
for channel in relay_channels:
os.system('echo "'+channel+'" /sys/class/gpio/export')
os.system('echo "out" > /sys/class/gpio/gpio'+channel+'/direction')
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:
for channel in relay_channels:
os.system('echo "'+channel+'" > /sys/class/gpio/unexport')
GPIO.cleanup()