diff --git a/main.py b/main.py index 2df2789..76e02f6 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ from string import whitespace import time from datetime import datetime +import os try: import paho.mqtt.client as mqtt import gpiozero @@ -25,17 +26,11 @@ def error_print(message: str): #relay def relay_controller(pin: int, state: str): - GPIO.setmode(GPIO.BCM) - GPIO.setup(pin, GPIO.OUT) if state == "True": - # relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False) - # relay.on() - GPIO.output(pin, GPIO.LOW) + os.system('echo "1" > /sys/class/gpio/gpio'+str(pin)+'/value') debug_print("Set relay-pin "+str(pin)+" to state True") elif state == "False": - # relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False) - # relay.off() - GPIO.output(pin, GPIO.HIGH) + 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)) @@ -116,6 +111,11 @@ def mqtt_on_message(client, userdata, msg): 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!") @@ -134,4 +134,6 @@ if __name__ == "__main__": 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() \ No newline at end of file