This commit is contained in:
Thomas Klaehn 2019-07-02 14:07:00 +02:00
parent b2ad105079
commit 4ad68caeb6
3 changed files with 62 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

View File

@ -30,7 +30,7 @@ class EnvData(object):
class EnvDataCollector(threading.Thread): class EnvDataCollector(threading.Thread):
def __init__(self, name, dht_pin, update_interval=10): def __init__(self, name, dht_pin, update_interval=60):
super(EnvDataCollector, self).__init__() super(EnvDataCollector, self).__init__()
self.name = name self.name = name
self.sensor = 22 self.sensor = 22
@ -46,8 +46,8 @@ class EnvDataCollector(threading.Thread):
if now >= next_update: if now >= next_update:
humidity, temperature = Adafruit_DHT.read(self.sensor, self.dht_pin) humidity, temperature = Adafruit_DHT.read(self.sensor, self.dht_pin)
if humidity is None or temperature is None: if humidity is None or temperature is None:
log.info("%s: No data read - setting re-read intterval to 1s.", self.name) # log.info("%s: No data read - setting re-read intterval to 1s.", self.name)
next_update = now + datetime.timedelta(seconds=1) next_update = now + datetime.timedelta(seconds=10)
else: else:
if humidity >= 0.0 and humidity <= 100.0 and temperature >= -273.15: if humidity >= 0.0 and humidity <= 100.0 and temperature >= -273.15:
self.data.temperature = temperature self.data.temperature = temperature
@ -55,12 +55,12 @@ class EnvDataCollector(threading.Thread):
self.data.stamp = now self.data.stamp = now
self.data.is_valid = True self.data.is_valid = True
next_update = now + datetime.timedelta(seconds=self.update_interval) next_update = now + datetime.timedelta(seconds=self.update_interval)
log.info("%s: Data read - setting re-read intterval to %ss.", # log.info("%s: Data read - setting re-read intterval to %ss.",
self.name, self.update_interval) # self.name, self.update_interval)
else: else:
log.info("%s: Invalid data read - setting re-read intterval to 1s.", # log.info("%s: Invalid data read - setting re-read intterval to 1s.",
self.name) # self.name)
next_update = now + datetime.timedelta(seconds=1) next_update = now + datetime.timedelta(seconds=10)
time.sleep(1) time.sleep(1)
def stop(self): def stop(self):
@ -99,19 +99,50 @@ class Heat(object):
def off(self): def off(self):
gpio.output(self.pin, 1) gpio.output(self.pin, 1)
class Water(object):
def __init__(self, water_switch):
self.filename = "/var/log/relay_{}.state".format(water_switch)
def state(self):
res = [0, 0, 0]
with open(self.filename, "r") as f:
ctx = f.read().strip().split(" ")
return ctx[0], ctx[1], ctx[2]
def main(): def main():
old_green = EnvDataCollector("Greenhouse one", 17) old_green = EnvDataCollector("Greenhouse one", 17)
old_heat = Heat(6) old_heat = Heat(6)
old_heat.off()
old_heat_state = "off" old_heat_state = "off"
old_water = Water(1)
last_old_water_state = "off"
new_green = EnvDataCollector("Greenhouse two", 21) new_green = EnvDataCollector("Greenhouse two", 21)
new_water = Water(2)
last_new_water_state = "off"
old_green.start() old_green.start()
new_green.start() new_green.start()
mqtt_handler = MqttHandler("mqtt.blackfinn.de", 8883, "/etc/ssl/certs/DST_Root_CA_X3.pem") mqtt_handler = MqttHandler("mqtt.blackfinn.de", 8883, "/etc/ssl/certs/DST_Root_CA_X3.pem")
try: try:
# let first temperature/humidity collection happen.
time.sleep(5)
next_update = datetime.datetime.now() next_update = datetime.datetime.now()
while True: while True:
now = datetime.datetime.now() now = datetime.datetime.now()
# Water state
current_old_water_state = old_water.state()
if current_old_water_state[2] != last_old_water_state:
last_old_water_state = current_old_water_state[2]
msg = "{} {} {}".format(current_old_water_state[0], current_old_water_state[1], current_old_water_state[2])
mqtt_handler.transmit("outdoor/greenhouse/water", msg)
current_new_water_state = new_water.state()
if current_new_water_state[2] != last_new_water_state:
last_new_water_state = current_new_water_state[2]
msg = "{} {} {}".format(current_new_water_state[0], current_new_water_state[1], current_new_water_state[2])
mqtt_handler.transmit("outdoor/greenhouse-2/water", msg)
if now >= next_update: if now >= next_update:
# 1) Check timestamp of last data collection. If data are older # 1) Check timestamp of last data collection. If data are older
@ -123,7 +154,6 @@ def main():
msg = "{} {}".format(datetime.datetime.timestamp(old_green.data.stamp), old_green.data.humidity) msg = "{} {}".format(datetime.datetime.timestamp(old_green.data.stamp), old_green.data.humidity)
mqtt_handler.transmit("outdoor/greenhouse/humidity", msg) mqtt_handler.transmit("outdoor/greenhouse/humidity", msg)
log.info("%s: Humidity: %s", old_green.data.stamp.time(), old_green.data.humidity) log.info("%s: Humidity: %s", old_green.data.stamp.time(), old_green.data.humidity)
if old_green.data.temperature < SWITCH_ON_TEMP and old_heat_state == "off": if old_green.data.temperature < SWITCH_ON_TEMP and old_heat_state == "off":
old_heat.on() old_heat.on()
old_heat_state = "on" old_heat_state = "on"
@ -136,12 +166,32 @@ def main():
msg = "{} {}".format(time.time(), old_heat_state) msg = "{} {}".format(time.time(), old_heat_state)
mqtt_handler.transmit("outdoor/greenhouse/heat", msg) mqtt_handler.transmit("outdoor/greenhouse/heat", msg)
log.info("%s: Heat: switch %s", time.time(), old_heat_state) log.info("%s: Heat: switch %s", time.time(), old_heat_state)
next_update = now + datetime.timedelta(minutes=15) elif old_heat_state == "on":
# Do a power cycle to prevent auto-poweroff
log.info("%s: Heat init power cycle.", time.time())
old_heat.off()
time.sleep(5)
old_heat.on()
log.info("%s: Heat power cycle done.", time.time())
next_update = now + datetime.timedelta(minutes=5)
else: else:
msg = "{} Error! Last valid DHT sensor data is from {}.".format(time.time(), old_green.data.stamp) msg = "{} Error! Last valid DHT sensor data is from {}.".format(time.time(), old_green.data.stamp)
mqtt_handler.transmit("outdoor/greenhouse/status", msg) mqtt_handler.transmit("outdoor/greenhouse/status", msg)
log.error("%s: Error! Last valid DHT sensor data is from %s", time.time(), old_green.data.stamp) log.error("%s: Error! Last valid DHT sensor data is from %s", time.time(), old_green.data.stamp)
next_update = now + datetime.timedelta(seconds=5) next_update = now + datetime.timedelta(seconds=5)
if new_green.data.is_valid and new_green.data.stamp + datetime.timedelta(minutes=5) >= now:
msg = "{} {}".format(datetime.datetime.timestamp(new_green.data.stamp), new_green.data.temperature)
mqtt_handler.transmit("outdoor/greenhouse-2/temperature", msg)
log.info("%s: Temperature: %s", new_green.data.stamp.time(), new_green.data.temperature)
msg = "{} {}".format(datetime.datetime.timestamp(new_green.data.stamp), new_green.data.humidity)
mqtt_handler.transmit("outdoor/greenhouse-2/humidity", msg)
log.info("%s: Humidity: %s", new_green.data.stamp.time(), new_green.data.humidity)
else:
msg = "{} Error! Last valid DHT sensor data is from {}.".format(time.time(), new_green.data.stamp)
mqtt_handler.transmit("outdoor/greenhouse-2/status", msg)
log.error("%s: Error! Last valid DHT sensor data is from %s", time.time(), new_green.data.stamp)
time.sleep(1) time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
log.info("Shutting down...") log.info("Shutting down...")
@ -152,3 +202,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(main()) sys.exit(main())

0
setup.py Normal file → Executable file
View File