Initial commit

This commit is contained in:
Thomas Klaehn 2021-05-05 12:30:56 +02:00
commit 4e25b921c4
22 changed files with 1263 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
dist/
build/
__pycache__/
*.egg-info/

18
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Main File",
"type": "python",
"request": "launch",
"program": "gardencontrol/main.py",
"console": "integratedTerminal",
"env": {
"PYTHONPATH": "${workspaceRoot}"
}
}
]
}

36
README.md Normal file
View File

@ -0,0 +1,36 @@
# Greenhouse
Greenhous control
## Installation
```shell
python setup.py install
```
## Usage
### Manual
Create wrapper script (e.g. wrapper.py):
```python
from greenhous import app
if __name__ == "__main__":
app.run()
```
Execute the wrapper script:
```shell
python3 wrapper.py
```
### gunicorn
```shell
gunicorn --bind 0.0.0.0:80 greenhouse:app
```

135
config/_Config.py Normal file
View File

@ -0,0 +1,135 @@
import os
import json
import shutil
class Config():
def __init__(self, configfile):
self.__config_file = configfile
self.__config = None
try:
with open(self.__config_file, "r") as handle:
self.__config = json.load(handle)
except FileNotFoundError:
# create default config
os.makedirs(os.path.dirname(self.__config_file), exist_ok=True)
shutil.copyfile("config/config.json", self.__config_file)
with open(self.__config_file, "r") as handle:
self.__config = json.load(handle)
def hostinfo(self):
res = self.__config['hostname'], int(self.__config['port'])
return res
def get_heat_autostate(self, ident: int):
for entry in self.__config['heat']:
if int(entry['id']) == ident:
return entry['autostate']
return None
def set_heat_autostate(self, ident: str):
for idx in range(len(self.__config['heat'])):
if self.__config['heat'][idx]['id'] == ident:
self.__config['heat'][idx]['autostate'] = True
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def clear_heat_autostate(self, ident: str):
for idx in range(len(self.__config['heat'])):
if self.__config['heat'][idx]['id'] == ident:
self.__config['heat'][idx]['autostate'] = False
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def increase_on_temperature(self, ident: str):
for idx in range(len(self.__config['heat'])):
if self.__config['heat'][idx]['id'] == ident:
on_temp = int(self.__config['heat'][idx]['on_temperature'])
on_temp += 1
self.__config['heat'][idx]['on_temperature'] = str(on_temp)
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def decrease_on_temperature(self, ident: str):
for idx in range(len(self.__config['heat'])):
if self.__config['heat'][idx]['id'] == ident:
on_temp = int(self.__config['heat'][idx]['on_temperature'])
on_temp -= 1
self.__config['heat'][idx]['on_temperature'] = str(on_temp)
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def increase_off_temperature(self, ident: str):
for idx in range(len(self.__config['heat'])):
if self.__config['heat'][idx]['id'] == ident:
off_temp = int(self.__config['heat'][idx]['off_temperature'])
off_temp += 1
self.__config['heat'][idx]['off_temperature'] = str(off_temp)
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def decrease_off_temperature(self, ident: str):
for idx in range(len(self.__config['heat'])):
if self.__config['heat'][idx]['id'] == ident:
off_temp = int(self.__config['heat'][idx]['off_temperature'])
off_temp -= 1
self.__config['heat'][idx]['off_temperature'] = str(off_temp)
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def get_on_temperature(self, ident: int):
for entry in self.__config['heat']:
if int(entry['id']) == ident:
return entry['on_temperature']
return None
def get_off_temperature(self, ident: int):
for entry in self.__config['heat']:
if int(entry['id']) == ident:
return entry['off_temperature']
return None
def get_water_autostate(self, ident: int):
for entry in self.__config['water']:
if int(entry['id']) == ident:
return entry['autostate']
return None
def get_water_times(self, ident: int):
for entry in self.__config['water']:
if int(entry['id']) == ident:
return entry['times']
return None
def set_water_autostate(self, ident: str):
for idx in range(len(self.__config['water'])):
if self.__config['water'][idx]['id'] == ident:
self.__config['water'][idx]['autostate'] = True
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def clear_water_autostate(self, ident: str):
for idx in range(len(self.__config['water'])):
if self.__config['water'][idx]['id'] == ident:
self.__config['water'][idx]['autostate'] = False
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return
def set_water_times(self, ident: str, times):
for idx in range(len(self.__config['water'])):
if self.__config['water'][idx]['id'] == ident:
self.__config['water'][idx]['times'] = times
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
return

1
config/__init__.py Normal file
View File

@ -0,0 +1 @@
from ._Config import Config

60
config/config.json Normal file
View File

@ -0,0 +1,60 @@
{
"hostname": "localhost",
"port": "64001",
"water": [
{
"id": "1",
"autostate": false,
"pin": "22",
"times": [
{
"on_time": "7:00",
"off_time": "7:20"
},
{
"on_time": "19:00",
"off_time": "19:20"
}
]
},
{
"id": "2",
"autostate": false,
"pin": "17",
"times": [
{
"on_time": "7:20",
"off_time": "7:40"
},
{
"on_time": "19:20",
"off_time": "19:40"
}
]
},
{
"id": "3",
"autostate": false,
"pin": "27",
"times": [
{
"on_time": "7:40",
"off_time": "8:00"
},
{
"on_time": "19:40",
"off_time": "20:00"
}
]
}
],
"heat": [
{
"id": "1",
"autostate": false,
"pin": "26",
"on_temperature": "3",
"off_temperature": "5"
}
]
}

149
control/_Control.py Normal file
View File

@ -0,0 +1,149 @@
import datetime
import json
import logging
import os
import shutil
import threading
import time
from w1thermsensor import W1ThermSensor
import RPi.GPIO as GPIO
import heat
class Control(threading.Thread):
def __init__(self, configfile):
super(Control, self).__init__()
self.__run_condition = True
self.__config_file = configfile
self.__config = None
self.__log = logging.getLogger()
self.__sensor = W1ThermSensor()
self.__heat = None
self.__trigger_read_config = True
self.__water_state = []
self.__temperature = None
def reload_config(self):
self.__trigger_read_config = True
def load_config(self):
try:
with open(self.__config_file, "r") as handle:
self.__config = json.load(handle)
except FileNotFoundError:
# create default config
os.makedirs(os.path.dirname(self.__config_file), exist_ok=True)
shutil.copyfile("config/config.json", self.__config_file)
with open(self.__config_file, "r") as handle:
self.__config = json.load(handle)
self.__heat = heat.Heat(int(self.__config['heat'][0]['pin']))
for _ in range(len(self.__config['water'])):
self.__water_state.append(False)
# Configure all water pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
for entry in self.__config['water']:
pin = int(entry['pin'])
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 1)
def run(self):
self.load_config()
self.__heat.off()
self.__heat.start()
while self.__run_condition:
if self.__trigger_read_config:
self.__trigger_read_config = False
self.load_config()
self.__temperature = float(self.__sensor.get_temperature())
# handle heat
if self.__config['heat'][0]['autostate']:
on_temperature = float(self.__config['heat'][0]['on_temperature'])
off_temperature = float(self.__config['heat'][0]['off_temperature'])
if self.__temperature < on_temperature and not self.__heat.state():
self.__heat.on()
self.__log.info("Switch heat on by temperature level: %.1f °C", self.__temperature)
elif self.__temperature > off_temperature and self.__heat.state():
self.__heat.off()
self.__log.info("Switch heat off by temperature level: %.1f °C", self.__temperature)
# handle water entries
water = self.__config['water']
water_index = 0
for entry in water:
now = datetime.datetime.now()
if entry['autostate']:
idx = 0
if int(now.hour) >= 12:
idx = 1
on_time_pattern = entry['times'][idx]['on_time']
on_time_pattern = on_time_pattern.split(':')
on_time = now.replace(hour=int(on_time_pattern[0]),
minute=int(on_time_pattern[1]),
second=0,
microsecond=0)
off_time_pattern = entry['times'][idx]['off_time']
off_time_pattern = off_time_pattern.split(':')
off_time = now.replace(hour=int(off_time_pattern[0]),
minute=int(off_time_pattern[1]),
second=0,
microsecond=0)
pin = int(entry['pin'])
if now > on_time and now <= off_time and not self.__water_state[water_index]:
GPIO.output(pin, 0)
self.__water_state[water_index] = True
self.__log.info("Switch water on by time")
elif now > off_time and self.__water_state[water_index]:
GPIO.output(pin, 1)
self.__water_state[water_index] = False
self.__log.info("Switch water off by time")
water_index += 1
time.sleep(1)
self.__heat.stop()
def stop(self):
self.__run_condition = False
self.join()
def get_current_temperature(self, ident: int):
return f"{self.__temperature:.1f}"
def get_current_heat_state(self, ident: int):
return self.__heat.state()
def get_current_water_state(self, ident: int):
if ident > 0 and ident < len(self.__water_state):
return self.__water_state[ident -1]
return None
def set_heat_state(self, ident: str):
self.__heat.on()
self.__log.info("Switch heat on by button")
def clear_heat_state(self, ident: str):
self.__heat.off()
self.__log.info("Switch heat off by button")
def set_water_state(self, ident: str):
ident = int(ident)
if ident > 0 and ident < len(self.__water_state):
pin = int(self.__config['water'][ident - 1]['pin'])
self.__water_state[ident - 1] = True
self.__log.info("Switch water on by button")
GPIO.output(pin, 0)
def clear_water_state(self, ident: str):
ident = int(ident)
if ident > 0 and ident < len(self.__water_state):
pin = int(self.__config['water'][ident - 1]['pin'])
self.__water_state[ident - 1] = False
self.__log.info("Switch water off by button")
GPIO.output(pin, 1)

1
control/__init__.py Normal file
View File

@ -0,0 +1 @@
from ._Control import Control

10
gardencontrol.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=Gardencontrol service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/local/bin/gardencontrol
[Install]
WantedBy=multi-user.target

25
gardencontrol/main.py Normal file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python
import logging
import os
import sys
import remotecontrol
LOG_LEVEL = logging.INFO
LOG_FILE = "/var/log/sauna.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
def main():
logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE)
# logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL)
log = logging.getLogger()
config_file = os.path.join(os.path.expanduser('~'), ".config/gardencontrol/config.json")
log.info("Starting...")
server = remotecontrol.Remotecontrol(config_file)
server.start()
if __name__ == '__main__':
sys.exit(main())

1
greenhouse/__init__.py Normal file
View File

@ -0,0 +1 @@
from .app import app

219
greenhouse/app.py Normal file
View File

@ -0,0 +1,219 @@
import os
import site
import json
import datetime
import logging
from threading import Thread
from time import sleep
from flask import Flask
from flask import render_template
from flask import make_response
from flask import request
from w1thermsensor import W1ThermSensor
import RPi.GPIO as GPIO
sensor = W1ThermSensor()
water_pin = 22 #17/27/22
heat_pin = 26
heat_state = False
PACKAGE_PATH = site.getsitepackages()[0]
CONFIG_FILE = os.path.join(PACKAGE_PATH, "greenhouse/config/greenhouse.json")
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__()
self.__pin = pin
self.__state = False
GPIO.setup(pin, GPIO.OUT)
if GPIO.input(pin):
self.__state = True
self.__run_condition = True
self.__next_update = datetime.datetime.now()
def on(self):
self.__state = True
GPIO.output(self.__pin, 0)
def off(self):
self.__state = False
GPIO.output(self.__pin, 1)
def run(self):
self.__next_update = datetime.datetime.now()
while self.__run_condition:
now = datetime.datetime.now()
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)
self.__next_update = now + datetime.timedelta(minutes=5)
sleep(1)
def stop(self):
self.__run_condition = False
self.join()
def state(self):
return self.__state
heat = Heat(heat_pin)
heat.start()
class GreenControl(Thread):
def __init__(self, cfg_file:str):
super(GreenControl, self).__init__()
self.cfg_file = cfg_file
self.config = {}
self.__run_condition = True
self.__water_state = False
self.__trigger_read_config = True
def reload_config(self):
self.__trigger_read_config = True
def run(self):
global sensor
global heat
next_update = datetime.datetime.now()
while self.__run_condition:
if self.__trigger_read_config:
self.__trigger_read_config = False
with open(self.cfg_file, "r") as f:
self.config = json.load(f)
now = datetime.datetime.now()
if now >= next_update:
if self.config['heat']['state']:
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()
sleep(5)
heat.on()
next_update = now + datetime.timedelta(minutes=5)
if self.config['water']['state']:
index = 0
if int(now.hour) >= 12:
index = 1
on_time_pattern = self.config['water']['times'][index]['on_time']
on_time_pattern = on_time_pattern.split(':')
on_time = now.replace(hour=int(on_time_pattern[0]), minute=int(on_time_pattern[1]), second=0, microsecond=0)
off_time_pattern = self.config['water']['times'][index]['off_time']
off_time_pattern = off_time_pattern.split(':')
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 = False
log.info("Switch water off by time")
sleep(1)
green_ctrl = GreenControl(CONFIG_FILE)
green_ctrl.start()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/config')
def config():
return render_template('config.html')
@app.route('/cfg', methods=['GET'])
def get_config():
res = {}
with open(CONFIG_FILE, "r") as f:
res = json.load(f)
return res
@app.route('/cfg', methods=['PATCH'])
def patch_config():
global green_ctrl
res = make_response("", 500)
cfg = json.loads(request.data)
with open(CONFIG_FILE, "w") as f:
json.dump(cfg, f)
res = make_response("", 204)
green_ctrl.reload_config()
return res
@app.route('/sample', methods=['GET'])
def get_sample():
global heat
global sensor
try:
temperature = f"{float(sensor.get_temperature()):.1f}"
except Exception:
temperature = None
water_state = False
if not GPIO.input(water_pin):
water_state = True
res = {}
res["id"] = str(1)
if temperature:
res["temperature"] = temperature
res["water"] = water_state
res["heat"] = heat.state()
return res
@app.route('/sample', methods=['PATCH'])
def patch_sample():
global heat
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()))
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
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)

View File

@ -0,0 +1,73 @@
html, body {
font-size: 22px !important;
font-family: arial, verdana, helvetica, sans-serif;
color: #b6b6b6;
background: #282929;
}
h1, h2, h3 {text-align: center;}
p {text-align: center;}
div {text-align: center;}
.center {
margin-left: auto;
margin-right: auto;
text-align: center;
}
input {
border: 1px #999999 solid;
background-color: #f6f6f6;
padding: 2px 4px 2px 4px;
margin: 0 0 0.5rem 0;
font-size: 1.0rem;
border-radius: 8px;
}
input[type="submit" i] {
color: #b6b6b6;
background-color: #282929;
padding: 10px;
margin: 10px 0;
border-color: #b6b6b6;
border-width: 3px;
border-radius: 18px;
font-size: 20px;
}
input[type="text"] {
border-radius: 8px;
}
td {
padding: 8px;
}
.table_left {
text-align: right;
}
footer {
position: fixed;
/* left: 50%; */
bottom: 20px;
/* transform: translate(-50%, -50%); */
margin: 0 auto;
}
a {
outline: none;
color: #b6b6b6;
text-decoration-line: none;
}
select {
color: #b6b6b6;
background-color: #282929;
padding: 10px;
margin: 10px 0;
border-color: #b6b6b6;
border-width: 3px;
border-radius: 16px;
font-size: 20px;
}

View File

@ -0,0 +1,121 @@
var get_cfg = function (event) {
var config = JSON.parse(event)
if(config.heat) {
if(config.heat.state) {
document.getElementById("heat_state").value = "an"
document.getElementById('heat_config').style.visibility = 'visible';
} else {
document.getElementById("heat_state").value = "aus"
document.getElementById('heat_config').style.visibility = 'hidden';
}
if(config.heat.on_temperature) {
document.getElementById("switch_on_temperature").value = config.heat.on_temperature;
}
if(config.heat.off_temperature) {
document.getElementById("switch_off_temperature").value = config.heat.off_temperature;
}
}
if(config.water) {
if(config.water.state) {
document.getElementById("water_state").value = "an"
document.getElementById('water_times').style.visibility = 'visible';
} else {
document.getElementById("water_state").value = "aus"
document.getElementById('water_times').style.visibility = 'hidden';
}
if(config.water.times) {
document.getElementById("water_on_one").value = config.water.times[0].on_time;
document.getElementById("water_off_one").value = config.water.times[0].off_time;
document.getElementById("water_on_two").value = config.water.times[1].on_time;
document.getElementById("water_off_two").value = config.water.times[1].off_time;
}
}
}
var on_change_heat_state = function() {
if(document.getElementById("heat_state").value == "an") {
document.getElementById("heat_state").value = "aus"
document.getElementById('heat_config').style.visibility = 'hidden';
} else {
document.getElementById("heat_state").value = "an"
document.getElementById('heat_config').style.visibility = 'visible';
}
}
var on_change_water_state = function() {
if(document.getElementById("water_state").value == "an") {
document.getElementById("water_state").value = "aus";
document.getElementById('water_times').style.visibility = 'hidden';
} else {
document.getElementById("water_state").value = "an";
document.getElementById('water_times').style.visibility = 'visible';
}
}
var on_push_config = function() {
var water_state = false;
if(document.getElementById("water_state").value == 'an') {
water_state = true;
}
var on_time_one = document.getElementById("water_on_one").value;
var off_time_one = document.getElementById("water_off_one").value;
var on_time_two = document.getElementById("water_on_two").value;
var off_time_two = document.getElementById("water_off_two").value;
var heat_state = false;
if(document.getElementById("heat_state").value == 'an') {
heat_state = true;
}
var on_tmp = document.getElementById("switch_on_temperature").value;
var off_tmp = document.getElementById("switch_off_temperature").value;
var json_str = JSON.stringify(
{
"water":
{
"state":water_state,
"times":
[
{
"on_time":on_time_one,
"off_time":off_time_one
},
{
"on_time":on_time_two,
"off_time":off_time_two
}
]
},
"heat":
{
"state":heat_state,
"on_temperature":on_tmp,
"off_temperature":off_tmp
}
}
);
patch_cfg(json_str);
}
var patch_http = new XMLHttpRequest();
var patch_cfg = function(config) {
patch_http.abort();
patch_http.open("PATCH", "/cfg");
patch_http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
patch_http.send(config);
}
var http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4) {
var status = http.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
get_cfg(http.responseText);
}
} else {
// request error
}
}
http.open("GET", "/cfg");
http.send();

View File

@ -0,0 +1,66 @@
var on_switch_heat = function() {
var state = true;
if(document.getElementById("heat_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify({"id": "1", "heat": state});
patch_sample(json_str);
}
var on_switch_water = function() {
var state = true;
if(document.getElementById("water_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify({"id": "1", "water": state});
patch_sample(json_str);
}
var patch_http = new XMLHttpRequest();
var patch_sample = function(sample) {
patch_http.abort();
patch_http.open("PATCH", "/sample");
patch_http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
patch_http.send(sample);
}
var get_sample = function (event) {
var sample = JSON.parse(event)
output = "einschalten"
out_state = "aus"
if(sample.water) {
output = "ausschalten"
out_state = "an"
}
document.getElementById("temperature_value").innerHTML = sample.temperature + " °C";
document.getElementById("water_switch").value = output;
document.getElementById("water_state").innerHTML = out_state;
if(sample.heat) {
output = "ausschalten"
out_state = "an"
} else {
output = "einschalten"
out_state = "aus"
}
document.getElementById("heat_switch").value = output;
document.getElementById("heat_state").innerHTML = out_state;
}
var http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4) {
var status = http.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
get_sample(http.responseText);
setTimeout(function () {
http.open("GET", 'sample');
http.send();
}, 500);
}
} else {
// request error
}
}
http.open("GET", "sample");
http.send();

View File

@ -0,0 +1,115 @@
<!DOCTYPE html>
<html>
<head>
<title>Gewächshaus</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/static/css/style.css" rel="stylesheet">
<script src="/static/scripts/config.js"></script>
</head>
<body>
<h1>Gewächshaus</h1>
<h2>Configuration</h2>
<hr>
<h3>Zeitgesteuerte Heizung</h3>
<div class="input">
<input id="heat_state" type="submit" value="aus" onclick="on_change_heat_state()"></input>
</div>
<div id="heat_config" style="visibility:hidden";>
<div>Die Einschalttemperatur muss unterhalb der Ausschalttemperatur liegen.</div>
<table class="center">
<tr>
<td>Einschalttemperatur </td>
<td>
<select id="switch_on_temperature">
<option value=1>1 °C</option>
<option value=2>2 °C</option>
<option value=3>3 °C</option>
<option value=4>4 °C</option>
<option value=5>5 °C</option>
<option value=6>6 °C</option>
<option value=7>7 °C</option>
<option value=8>8 °C</option>
<option value=9>9 °C</option>
<option value=10>10 °C</option>
</select>
</td>
</tr>
<tr>
<td>Ausschalttemperatur </td>
<td>
<select id="switch_off_temperature">
<option value=1>1 °C</option>
<option value=2>2 °C</option>
<option value=3>3 °C</option>
<option value=4>4 °C</option>
<option value=5>5 °C</option>
<option value=6>6 °C</option>
<option value=7>7 °C</option>
<option value=8>8 °C</option>
<option value=9>9 °C</option>
<option value=10>10 °C</option>
</select>
</td>
</tr>
</table>
</div>
<hr>
<h3>Zeitgesteuerte Bewässerung</h3>
<div class="input">
<input id="water_state" type="submit" value="aus" onclick="on_change_water_state()"></input>
</div>
<div id="water_times" style="visibility:hidden;">
<table class="center">
<tr>
<td>Vormittag</td>
<td>
<table>
<tr>
<td>Einschaltzeit</td>
<td class="input">
<input type="text" id="water_on_one" style="width: 70px;"></input>
</td>
</tr>
<tr>
<td>Ausschaltzeit</td>
<td class="input">
<input type="text" id="water_off_one" style="width: 70px;"></input>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Nachmittag</td>
<td>
<table>
<tr>
<td>Einschaltzeit</td>
<td class="input">
<input type="text" id="water_on_two" style="width: 70px;"></input>
</td>
</tr>
<tr>
<td>Ausschaltzeit</td>
<td class="input">
<input type="text" id="water_off_two" style="width: 70px;"></input>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<hr>
<div class="input">
<input type="submit" value="Configuration senden" onclick="on_push_config()"></input>
</div>
</body>
<footer>
<nav>
<a href="/">Home</a>&nbsp|&nbsp</a>
<a href="/config">Configuration</a>
</nav>
</footer>
</html>

View File

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<title>Gewächshaus</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/static/css/style.css" rel="stylesheet">
<script src="/static/scripts/index.js"></script>
</head>
<body>
<h1>Gewächshaus</h1>
<table class="center">
<tr>
<td><h2>Temperatur </h2></td>
<td><h2 id="temperature_value"></h2></td>
</tr>
</table>
<table class="center">
<tr>
<td class="table_left">Heizung </td>
<td id="heat_state"></td>
<td class="input">
<input id="heat_switch" type="submit" value="" onclick="on_switch_heat()"></input>
</td>
</tr>
<tr>
<td class="left">Bewässerung </td>
<td class="center" id="water_state"></td>
<td class="input">
<input id="water_switch" type="submit" value="" onclick="on_switch_water()"></input>
</td>
</tr>
</table>
</body>
<footer>
<nav>
<a href="/">Home</a>&nbsp|&nbsp</a>
<a href="/config">Configuration</a>
</nav>
</footer>
</html>

50
heat/_Heat.py Normal file
View File

@ -0,0 +1,50 @@
import datetime
import threading
import time
import RPi.GPIO as GPIO
class Heat(threading.Thread):
def __init__(self, pin):
super(Heat, self).__init__()
self.__pin = pin
self.__state = False
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
if GPIO.input(pin):
self.__state = True
self.__run_condition = True
self.__next_update = datetime.datetime.now()
def on(self):
self.__state = True
GPIO.output(self.__pin, 0)
def off(self):
self.__state = False
GPIO.output(self.__pin, 1)
def run(self):
self.__next_update = datetime.datetime.now()
while self.__run_condition:
now = datetime.datetime.now()
if now >= self.__next_update:
if self.__state:
# Do a power cycle to prevent auto-poweroff
GPIO.output(self.__pin, 1)
time.sleep(5)
GPIO.output(self.__pin, 0)
self.__next_update = now + datetime.timedelta(minutes=5)
time.sleep(1)
self.off()
def stop(self):
self.__run_condition = False
self.join()
def state(self):
return self.__state

1
heat/__init__.py Normal file
View File

@ -0,0 +1 @@
from ._Heat import Heat

View File

@ -0,0 +1,86 @@
import logging
from xmlrpc.server import SimpleXMLRPCServer
import config
import control
class Remotecontrol():
def __init__(self, configfile):
self.__log = logging.getLogger()
self.__config = config.Config(configfile)
host = (self.__config.hostinfo())
self.__server = SimpleXMLRPCServer(host, allow_none=True)
self.__control = control.Control(configfile)
self.__server.register_function(self.set_water_autostate, 'set_water_autostate')
self.__server.register_function(self.clear_water_autostate, 'clear_water_autostate')
self.__server.register_function(self.set_water_times, 'set_water_times')
self.__server.register_function(self.set_heat_autostate, 'set_heat_autostate')
self.__server.register_function(self.clear_heat_autostate, 'clear_heat_autostate')
self.__server.register_function(self.increase_on_temperature, 'increase_on_temperature')
self.__server.register_function(self.decrease_on_temperature, 'decrease_on_temperature')
self.__server.register_function(self.increase_off_temperature, 'increase_off_temperature')
self.__server.register_function(self.decrease_off_temperature, 'decrease_off_temperature')
self.__server.register_function(self.__config.get_heat_autostate, 'get_heat_autostate')
self.__server.register_function(self.__config.get_on_temperature, 'get_on_temperature')
self.__server.register_function(self.__config.get_off_temperature, 'get_off_temperature')
self.__server.register_function(self.__config.get_water_autostate, 'get_water_autostate')
self.__server.register_function(self.__config.get_water_times, 'get_water_times')
self.__server.register_function(self.__control.get_current_temperature, 'get_current_temperature')
self.__server.register_function(self.__control.get_current_heat_state, 'get_current_heat_state')
self.__server.register_function(self.__control.get_current_water_state, 'get_current_water_state')
self.__server.register_function(self.__control.set_heat_state, 'set_heat_state')
self.__server.register_function(self.__control.clear_heat_state, 'clear_heat_state')
self.__server.register_function(self.__control.set_water_state, 'set_water_state')
self.__server.register_function(self.__control.clear_water_state, 'clear_water_state')
def set_water_times(self, ident: str, times):
self.__config.set_water_times(ident, times)
self.__control.reload_config()
def set_water_autostate(self, ident: str):
self.__config.set_water_autostate(ident)
self.__control.reload_config()
def clear_water_autostate(self, ident: str):
self.__config.clear_water_autostate(ident)
self.__control.reload_config()
def set_heat_autostate(self, ident: str):
self.__config.set_heat_autostate(ident)
self.__control.reload_config()
def clear_heat_autostate(self, ident: str):
self.__config.clear_heat_autostate(ident)
self.__control.reload_config()
def increase_on_temperature(self, ident: str):
self.__config.increase_on_temperature(ident)
self.__control.reload_config()
def decrease_on_temperature(self, ident: str):
self.__config.decrease_on_temperature(ident)
self.__control.reload_config()
def increase_off_temperature(self, ident: str):
self.__config.increase_off_temperature(ident)
self.__control.reload_config()
def decrease_off_temperature(self, ident: str):
self.__config.decrease_off_temperature(ident)
self.__control.reload_config()
def start(self):
self.__log.info('Control-c to quit')
self.__control.start()
self.__server.serve_forever()
self.__log.info("Shutting down...")
self.__control.stop()
self.__log.info("...done. Exiting...")

View File

@ -0,0 +1 @@
from ._Remotecontrol import Remotecontrol

49
setup.py Executable file
View File

@ -0,0 +1,49 @@
import os
import shutil
import stat
from setuptools import setup
from setuptools.command.install import install
NAME = 'gardencontrol'
VERSION = '1'
AUTHOR = 'Thomas Klaehn'
EMAIL = 'tkl@blackfinn.de'
PACKAGES = ['config', 'control', 'gardencontrol', 'heat', 'remotecontrol']
REQUIRES = ['w1thermsensor', 'RPi.GPIO']
CONFIG_FILE = 'config.json'
PACKAGE_DATA = {'gardencontrol': ['config/config.json']}
SERVICEDIR = "/lib/systemd/system"
DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'gardencontrol.service')
LOGFILE = "/var/log/gardencontrol.log"
ENTRY_POINTS = {
'console_scripts': [
'gardencontrol = gardencontrol.main:main'
]
}
class Install(install):
def run(self):
install.run(self)
os.makedirs(SERVICEDIR, exist_ok=True)
shutil.copyfile('gardencontrol.service', os.path.join(SERVICEDIR, DAEMON_START_SCRIPT))
os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
try:
open(LOGFILE, 'r')
except FileNotFoundError:
os.makedirs(os.path.dirname(LOGFILE), exist_ok=True)
open(LOGFILE, 'x')
os.chmod(LOGFILE, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
setup(name=NAME, version=VERSION, long_description=__doc__, author=AUTHOR, author_email=EMAIL,
packages=PACKAGES, include_package_data=True, package_data=PACKAGE_DATA, zip_safe=False,
install_requires=REQUIRES, entry_points=ENTRY_POINTS,
cmdclass={
'install': Install
}
)