parent
2ad871446f
commit
9ba3edbf20
18
main.py
18
main.py
|
@ -1,6 +1,7 @@
|
||||||
from string import whitespace
|
from string import whitespace
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import os
|
||||||
try:
|
try:
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import gpiozero
|
import gpiozero
|
||||||
|
@ -25,17 +26,11 @@ def error_print(message: str):
|
||||||
|
|
||||||
#relay
|
#relay
|
||||||
def relay_controller(pin: int, state: str):
|
def relay_controller(pin: int, state: str):
|
||||||
GPIO.setmode(GPIO.BCM)
|
|
||||||
GPIO.setup(pin, GPIO.OUT)
|
|
||||||
if state == "True":
|
if state == "True":
|
||||||
# relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
os.system('echo "1" > /sys/class/gpio/gpio'+str(pin)+'/value')
|
||||||
# relay.on()
|
|
||||||
GPIO.output(pin, GPIO.LOW)
|
|
||||||
debug_print("Set relay-pin "+str(pin)+" to state True")
|
debug_print("Set relay-pin "+str(pin)+" to state True")
|
||||||
elif state == "False":
|
elif state == "False":
|
||||||
# relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
os.system('echo "0" > /sys/class/gpio/gpio'+str(pin)+'/value')
|
||||||
# relay.off()
|
|
||||||
GPIO.output(pin, GPIO.HIGH)
|
|
||||||
debug_print("Set relay-pin "+str(pin)+" to state False")
|
debug_print("Set relay-pin "+str(pin)+" to state False")
|
||||||
else:
|
else:
|
||||||
error_print("Unknown state ("+state+") for relay channel "+str(pin))
|
error_print("Unknown state ("+state+") for relay channel "+str(pin))
|
||||||
|
@ -116,6 +111,11 @@ def mqtt_on_message(client, userdata, msg):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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"
|
mqttBroker ="homeassistant.ping-mee.local"
|
||||||
client = mqtt.Client("greenhouse_client")
|
client = mqtt.Client("greenhouse_client")
|
||||||
client.username_pw_set("mqtt", "pmMQTT_11!")
|
client.username_pw_set("mqtt", "pmMQTT_11!")
|
||||||
|
@ -134,4 +134,6 @@ if __name__ == "__main__":
|
||||||
debug_print("Published current water level: "+get_water_level())
|
debug_print("Published current water level: "+get_water_level())
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
for channel in relay_channels:
|
||||||
|
os.system('echo "'+channel+'" > /sys/class/gpio/unexport')
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
Loading…
Reference in New Issue