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() 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": relay = gpiozero.OutputDevice(pin, active_high=True, initial_value=False) relay.on() debug_print("Set relay-pin "+str(pin)+" to state True") elif state == "False": relay = gpiozero.OutputDevice(pin, active_high=True, initial_value=False) relay.off() debug_print("Set relay-pin "+str(pin)+" to state False") else: error_print("Unknown state for relay ("+state+")") #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 = 4 channel_2 = 17 channel_3 = 27 channel_4 = 11 if topic == "strawberry-pi-greenhouse/relay/channel/1": if payload == "on": # relay_controller(channel_1, "True") relay = gpiozero.OutputDevice(4, active_high=True, initial_value=False) relay.on() elif payload == "off": # relay_controller(channel_1, "False") relay = gpiozero.OutputDevice(4, active_high=True, initial_value=False) relay.on() 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__": 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()