2022-07-12 09:52:48 +00:00
|
|
|
from string import whitespace
|
2022-07-09 21:51:40 +00:00
|
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
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-09 21:51:40 +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)
|
|
|
|
|
2022-07-09 22:24:05 +00:00
|
|
|
#relay
|
2022-07-13 23:11:50 +00:00
|
|
|
def relay_controller(pin: int, state: str):
|
|
|
|
if state == True:
|
2022-07-13 23:18:07 +00:00
|
|
|
relay = gpiozero.OutputDevice(pin, active_high=True, initial_value=False)
|
2022-07-09 21:51:40 +00:00
|
|
|
relay.on()
|
2022-07-13 23:16:30 +00:00
|
|
|
debug_print("Set relay-pin "+str(pin)+" to state True")
|
2022-07-13 23:11:50 +00:00
|
|
|
elif state == False:
|
2022-07-13 23:18:07 +00:00
|
|
|
relay = gpiozero.OutputDevice(pin, active_high=True, initial_value=False)
|
2022-07-09 21:51:40 +00:00
|
|
|
relay.off()
|
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-13 23:11:50 +00:00
|
|
|
error_print("Unknown state for relay ("+state+")")
|
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
|
|
|
|
GPIO_ECHO = 8
|
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-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-12 08:29:12 +00:00
|
|
|
print(get_water_level())
|
2022-07-12 09:52:48 +00:00
|
|
|
print(get_temperature())
|
|
|
|
|
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-13 23:14:27 +00:00
|
|
|
channel_1 = 4
|
|
|
|
channel_2 = 17
|
|
|
|
channel_3 = 27
|
|
|
|
channel_4 = 11
|
|
|
|
|
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:14:27 +00:00
|
|
|
relay_controller(channel_1, True)
|
2022-07-13 07:51:58 +00:00
|
|
|
elif payload == "off":
|
2022-07-13 23:14:27 +00:00
|
|
|
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:14:27 +00:00
|
|
|
relay_controller(channel_2, True)
|
2022-07-13 07:51:58 +00:00
|
|
|
elif payload == "off":
|
2022-07-13 23:14:27 +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:14:27 +00:00
|
|
|
relay_controller(channel_3, True)
|
2022-07-13 07:51:58 +00:00
|
|
|
elif payload == "off":
|
2022-07-13 23:14:27 +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:14:27 +00:00
|
|
|
relay_controller(channel_4, True)
|
2022-07-13 07:51:58 +00:00
|
|
|
elif payload == "off":
|
2022-07-13 23:14:27 +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:
|
|
|
|
GPIO.cleanup()
|