2022-07-24 08:13:44 +00:00
|
|
|
from datetime import datetime
|
2022-07-24 08:13:00 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
debug_print("Importing modules")
|
|
|
|
|
2022-07-12 09:52:48 +00:00
|
|
|
from string import whitespace
|
2022-07-09 21:51:40 +00:00
|
|
|
import time
|
2022-07-17 18:59:43 +00:00
|
|
|
import os
|
2022-07-09 21:51:40 +00:00
|
|
|
try:
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import gpiozero
|
2022-07-09 22:02:25 +00:00
|
|
|
import RPi.GPIO as GPIO
|
2022-07-12 06:58:11 +00:00
|
|
|
import board
|
|
|
|
import busio
|
|
|
|
import adafruit_bmp280
|
2022-07-09 21:51:40 +00:00
|
|
|
except ImportError:
|
|
|
|
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
2022-07-10 22:32:06 +00:00
|
|
|
print(ImportError)
|
2022-07-09 21:51:40 +00:00
|
|
|
exit()
|
2022-07-09 22:02:25 +00:00
|
|
|
|
2022-07-24 08:13:00 +00:00
|
|
|
debug_print("Imported modules")
|
2022-07-09 21:51:40 +00:00
|
|
|
|
2022-07-09 22:24:05 +00:00
|
|
|
#relay
|
2022-07-13 23:21:44 +00:00
|
|
|
def relay_controller(pin: int, state: str):
|
|
|
|
if state == "True":
|
2022-07-17 19:13:59 +00:00
|
|
|
os.system('raspi-gpio set '+str(pin)+' op')
|
2022-07-13 23:16:30 +00:00
|
|
|
debug_print("Set relay-pin "+str(pin)+" to state True")
|
2022-07-13 23:21:44 +00:00
|
|
|
elif state == "False":
|
2022-07-17 19:13:59 +00:00
|
|
|
os.system('raspi-gpio set '+str(pin)+' ip')
|
2022-07-13 23:16:30 +00:00
|
|
|
debug_print("Set relay-pin "+str(pin)+" to state False")
|
2022-07-09 21:51:40 +00:00
|
|
|
else:
|
2022-07-14 08:47:42 +00:00
|
|
|
error_print("Unknown state ("+state+") for relay channel "+str(pin))
|
2022-07-09 21:51:40 +00:00
|
|
|
|
2022-07-09 22:24:05 +00:00
|
|
|
#ultrasonic
|
2022-07-09 22:02:25 +00:00
|
|
|
def get_water_level():
|
2022-07-12 08:30:51 +00:00
|
|
|
GPIO.setmode(GPIO.BCM)
|
2022-07-12 08:00:37 +00:00
|
|
|
GPIO.setwarnings(False)
|
2022-07-12 08:36:38 +00:00
|
|
|
GPIO_TRIGGER = 24
|
2022-07-24 10:18:20 +00:00
|
|
|
GPIO_ECHO = 25
|
2022-07-12 08:00:37 +00:00
|
|
|
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
|
|
|
|
GPIO.setup(GPIO_ECHO, GPIO.IN)
|
2022-07-09 22:02:25 +00:00
|
|
|
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
|
2022-07-12 09:54:25 +00:00
|
|
|
rounded_distance = int(distance)
|
|
|
|
return str(rounded_distance)
|
2022-07-09 22:02:25 +00:00
|
|
|
|
2022-07-24 08:13:00 +00:00
|
|
|
debug_print("Added water level detector")
|
|
|
|
|
2022-07-12 06:58:11 +00:00
|
|
|
#temperature
|
|
|
|
def get_temperature():
|
|
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
2022-07-12 08:28:31 +00:00
|
|
|
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76)
|
2022-07-12 09:52:48 +00:00
|
|
|
rounded_temp = int(bmp280.temperature)
|
|
|
|
return str(rounded_temp)
|
2022-07-12 06:58:11 +00:00
|
|
|
|
2022-07-24 08:13:00 +00:00
|
|
|
debug_print("Added temperature sensor")
|
|
|
|
|
2022-07-24 10:18:37 +00:00
|
|
|
print(get_water_level())
|
|
|
|
debug_print("got water level")
|
2022-07-12 09:52:48 +00:00
|
|
|
print(get_temperature())
|
2022-07-24 08:14:52 +00:00
|
|
|
debug_print("got temperature")
|
2022-07-12 09:52:48 +00:00
|
|
|
|
2022-07-12 09:54:25 +00:00
|
|
|
def mqtt_on_connect(client, userdata, flags, rc):
|
2022-07-12 09:52:48 +00:00
|
|
|
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")
|
|
|
|
|
2022-07-12 09:54:25 +00:00
|
|
|
def mqtt_on_message(client, userdata, msg):
|
2022-07-12 09:52:48 +00:00
|
|
|
print("topic: " ,str(msg.topic))
|
|
|
|
print("payload: " ,str(msg.payload.decode("utf-8")))
|
|
|
|
topic = str(msg.topic)
|
|
|
|
payload = str(msg.payload.decode("utf-8"))
|
|
|
|
|
2022-07-17 15:25:02 +00:00
|
|
|
channel_1 = 19
|
|
|
|
channel_2 = 16
|
|
|
|
channel_3 = 26
|
|
|
|
channel_4 = 20
|
2022-07-13 23:14:27 +00:00
|
|
|
|
2022-07-12 09:52:48 +00:00
|
|
|
if topic == "strawberry-pi-greenhouse/relay/channel/1":
|
2022-07-13 07:51:58 +00:00
|
|
|
if payload == "on":
|
2022-07-13 23:37:38 +00:00
|
|
|
relay_controller(channel_1, "True")
|
2022-07-13 23:39:34 +00:00
|
|
|
elif payload == "off":
|
|
|
|
relay_controller(channel_1, "False")
|
2022-07-12 09:52:48 +00:00
|
|
|
elif topic == "strawberry-pi-greenhouse/relay/channel/2":
|
2022-07-13 07:51:58 +00:00
|
|
|
if payload == "on":
|
2022-07-13 23:21:44 +00:00
|
|
|
relay_controller(channel_2, "True")
|
2022-07-13 07:51:58 +00:00
|
|
|
elif payload == "off":
|
2022-07-13 23:21:44 +00:00
|
|
|
relay_controller(channel_2, "False")
|
2022-07-12 09:52:48 +00:00
|
|
|
elif topic == "strawberry-pi-greenhouse/relay/channel/3":
|
2022-07-13 07:51:58 +00:00
|
|
|
if payload == "on":
|
2022-07-13 23:21:44 +00:00
|
|
|
relay_controller(channel_3, "True")
|
2022-07-13 07:51:58 +00:00
|
|
|
elif payload == "off":
|
2022-07-13 23:21:44 +00:00
|
|
|
relay_controller(channel_3, "False")
|
2022-07-12 09:52:48 +00:00
|
|
|
elif topic == "strawberry-pi-greenhouse/relay/channel/4":
|
2022-07-13 07:51:58 +00:00
|
|
|
if payload == "on":
|
2022-07-13 23:21:44 +00:00
|
|
|
relay_controller(channel_4, "True")
|
2022-07-13 07:51:58 +00:00
|
|
|
elif payload == "off":
|
2022-07-13 23:21:44 +00:00
|
|
|
relay_controller(channel_4, "False")
|
2022-07-12 09:52:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2022-07-17 19:13:59 +00:00
|
|
|
relay_channels = ["19", "16", "26", "20"]
|
2022-07-17 18:59:43 +00:00
|
|
|
for channel in relay_channels:
|
2022-07-17 19:13:59 +00:00
|
|
|
os.system('raspi-gpio set '+channel+' ip')
|
2022-07-12 09:52:48 +00:00
|
|
|
GPIO.cleanup()
|