Compare commits

..

No commits in common. "8274c5cd1adccb96177868fb002a21ffa91f1317" and "bd7d19d88d9e8a5e52427b47693108609e51fb3e" have entirely different histories.

2 changed files with 19 additions and 37 deletions

View File

@ -4,8 +4,7 @@ After=multi-user.target
[Service]
Type=idle
ExecStart=gunicorn --bind 0.0.0.0:80 greenhouse:app
ExecStart=/usr/bin/python3 -m greenhouse
[Install]
WantedBy=multi-user.target

View File

@ -1,6 +1,5 @@
import json
import datetime
import logging
from threading import Thread
from time import sleep
@ -13,7 +12,7 @@ from w1thermsensor import W1ThermSensor
import RPi.GPIO as GPIO
sensor = W1ThermSensor()
water_pin = 22 #17/27/22
water_pin = 17 #17/27/22
heat_pin = 26
heat_state = False
@ -24,14 +23,6 @@ GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(water_pin, GPIO.OUT)
log_level = logging.INFO
LOG_FILE = "/var/log/greenhouse.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE)
#logging.basicConfig(format=LOG_FORMAT, level=log_level)
log = logging.getLogger('greenhouse')
class Heat(Thread):
def __init__(self, pin):
super(Heat, self).__init__()
@ -45,11 +36,11 @@ class Heat(Thread):
def on(self):
self.__state = True
GPIO.output(self.__pin, 0)
GPIO.output(self.__pin, 1)
def off(self):
self.__state = False
GPIO.output(self.__pin, 1)
GPIO.output(self.__pin, 0)
def run(self):
self.__next_update = datetime.datetime.now()
@ -58,9 +49,9 @@ class Heat(Thread):
if now >= self.__next_update:
if self.__state:
# Do a power cycle to prevent auto-poweroff
GPIO.output(self.__pin, 1)
sleep(5)
GPIO.output(self.__pin, 0)
sleep(5)
GPIO.output(self.__pin, 1)
self.__next_update = now + datetime.timedelta(minutes=5)
sleep(1)
@ -102,10 +93,8 @@ class GreenControl(Thread):
temperature = sensor.get_temperature()
if float(temperature) < float(self.config['heat']['on_temperature']) and not heat.state():
heat.on()
log.info("Switch heat on by temperature level: %.1f °C", float(temperature))
elif float(temperature) > float(self.config['heat']['off_temperature']) and heat.state():
heat.off()
log.info("Switch heat off by temperature level: %.1f °C", float(temperature))
elif heat.state():
# Do a power cycle to prevent auto-poweroff
heat.off()
@ -124,13 +113,11 @@ class GreenControl(Thread):
off_time = now.replace(hour=int(off_time_pattern[0]), minute=int(off_time_pattern[1]), second=0, microsecond=0)
if now > on_time and now <= off_time and not self.__water_state:
GPIO.output(water_pin, 0)
self.__water_state = True
log.info("Switch water on by time")
elif now > off_time and self.__water_state:
GPIO.output(water_pin, 1)
self.__water_state = True
elif now > off_time and self.__water_state:
GPIO.output(water_pin, 0)
self.__water_state = False
log.info("Switch water off by time")
sleep(1)
@ -178,7 +165,7 @@ def get_sample():
temperature = None
water_state = False
if not GPIO.input(water_pin):
if GPIO.input(water_pin):
water_state = True
res = {}
@ -196,18 +183,14 @@ def patch_sample():
record = json.loads(request.data)
if "water" in record:
if record["water"]:
GPIO.output(water_pin, 0)
log.info("Switch water on by button: {}".format(datetime.datetime.now()))
else:
GPIO.output(water_pin, 1)
log.info("Switch water off by button: {}".format(datetime.datetime.now()))
else:
GPIO.output(water_pin, 0)
if "heat" in record:
if record["heat"]:
heat.on()
log.info("Switch heat on by button: {}".format(datetime.datetime.now()))
else:
heat.off()
log.info("Switch heat off by button: {}".format(datetime.datetime.now()))
res = make_response("", 204)
return res