diff options
| author | root <root@turin.home> | 2022-01-07 19:19:45 +0000 |
|---|---|---|
| committer | root <root@turin.home> | 2022-01-07 19:19:45 +0000 |
| commit | cf70ae6ded332729f0dc2833d6680d7dac8072db (patch) | |
| tree | fc8e5e39cffa07f2635dc0c71379ece34647fd58 | |
Commiting basic code
| -rw-r--r-- | .gitignore | 13 | ||||
| -rw-r--r-- | heating.py | 66 | ||||
| -rw-r--r-- | main.py | 56 | ||||
| -rw-r--r-- | sql.py | 71 | ||||
| -rw-r--r-- | static/style.css | 0 | ||||
| -rw-r--r-- | weather.py | 9 |
6 files changed, 215 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b95a570 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +**/latex.out +*~ +*.orig +*.pyc +.*.sw? +.sw? +*.elf +*.pdf +*.html +**/bin +**/build +**/target +**/__pycache__ diff --git a/heating.py b/heating.py new file mode 100644 index 0000000..e780cd2 --- /dev/null +++ b/heating.py @@ -0,0 +1,66 @@ +import RPi.GPIO as GPIO +from abc import ABC, abstractmethod + +# Abstract Class for heating +class heating(ABC): + def __init__(self, db): + self.temperature = 0 + self.taget = 0 + self.on = False + self.db = db + self.setup() + + def up(self, increase=0.5): + self.target += increase + self.update() + + def down(self, decrease=0.5): + self.target -= decrease + self.update() + + def update(self): + if self.temperature < self.target: + self.on = True + else: + self.on = False + ''' + Abstract methods used so that configuration for different + device types other than a Raspberry Pi is easier in the future + and all other methods can simply be inherited + ''' + + # Method for initial setup according to device GPIO + @abstractmethod + def setup(self): + pass + # Method to turn the heating on + @abstractmethod + def turn_on(self): + pass + # Method to turn the heating off + @abstractmethod + def turn_off(self): + pass + # Method to get the current actual temperature, can be changed for + # different types of temperature sensor when inheriting this + # class + @abstractmethod + def get_temperature(self): + pass + +# inherrited from heating class +# overriding abstract methods from heating class +class rpi_heating(heating): + def setup(self): + # GPIO Numbers instead of board numbers + GPIO.setmode(GPIO.BCM) + # https://tutorials-raspberrypi.com/raspberry-pi-control-relay-switch-via-gpio/ + # constant value for the relay number + self.RELAY = 17 + GPIO.setup(self.RELAY, GPIO.OUT) # GPIO Assign mode + def turn_on(self): + GPIO.output(self.RELAY, GPIO.LOW) # out + def turn_on(self): + GPIO.output(self.RELAY, GPIO.HIGH) # on + def get_temperature(self): + pass @@ -0,0 +1,56 @@ +import sqlite3 +import heating +import sql +import time + +from flask import Flask, render_template + +app = Flask(__name__) + +boiler = heating.rpi_heating() + +# paramaterised location of database file +# Use of final data type +global DBFILE +DBFILE = 'data.db' + + +db = sql.db(dbfile) + +@app.route('/') +@app.route('/index.html') +def index(): + # paramaterised location of template in 'templates' folder + return render_template( + "main.html", + actual_temp = boiler.temp, + target_temp = boiler.target + ) + +@app.route('/up') +def form(): + boiler.up() + return render_template("main.html") + +@app.route('/down') +def activity(): + boilder.down() + return render_template("main.html") + +@app.route('/export/<opt>') +def export(opt): + if opt == 'txt': + pass + elif opt == 'pdf': + pass + else: + return render_template('error.html', error="Invalid export type") + + +if __name__ == '__main__': + # if the database file does not exist, create it + try: + open(dbfile) + except: + sql.setup(dbfile) + app.run() @@ -0,0 +1,71 @@ +import sqlite3 + +class db: + def __init__(self, dbfile): + # final variable + self.dbfile = dbfile + try: + open(dbfile) + except FileNotFoundError: + self.setup() + def exec(cmd): + con = sqlite3.connect(self.dbfile) + cur = con.cursor + cur.execute(cmd) + con.commit() + con.close() + +def setup(dbfile): + ''' + Necessary when the database does not exist and the tables must be + created for the first time, otherwise not needed to be run. + ''' + con = sqlite3.connect(dbfile) + cur = con.cursor() + cur.execute( + ''' + CREATE TABLE temperature ( + date text not null, + time integer not null, + temperature real, + target real, + PRIMARY KEY (date, time) + ); + ''' + ) + cur.execute( + ''' + CREATE TABLE weather ( + date text not null, + temperature real, + wind real, + PRIMARY KEY (date), + FOREIGN KEY (date) REFERENCES temperature(date) + ); + ''' + ) + cur.execute( + ''' + CREATE TABLE schedule ( + day text not null, + time integer not null, + temperature real, + PRIMARY KEY (day, time) + ''' + ); + cur.execute( + ''' + CREATE TABLE history ( + date text not null, + time integer not null, + heating_on integer, + PRIMARY KEY (day, time), + FOREIGN KEY (date) REFERENCES temperature(date), + FOREIGN KEY (time) REFERENCES temperature(time) + ''' + ); + con.commit() + con.close() + pass + def export_csv(self): + return 0 diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/static/style.css diff --git a/weather.py b/weather.py new file mode 100644 index 0000000..e46fac8 --- /dev/null +++ b/weather.py @@ -0,0 +1,9 @@ +class weather: + def __init__(self): + self.temp = 0 + def get_temp(self): + #update temp + pass + def temperature(self): + self.get_temp() + return self.temp |
