summaryrefslogtreecommitdiff
path: root/heating.py
diff options
context:
space:
mode:
authorroot <root@turin.home>2022-01-15 14:46:26 +0000
committerroot <root@turin.home>2022-01-15 14:46:26 +0000
commitf315adea703193deb7419355dc084e903dbfde1e (patch)
tree64f0855bd91bf2c8b7cc4d9c9dc5a80a5c448015 /heating.py
parentcf70ae6ded332729f0dc2833d6680d7dac8072db (diff)
Development
Diffstat (limited to 'heating.py')
-rw-r--r--heating.py85
1 files changed, 65 insertions, 20 deletions
diff --git a/heating.py b/heating.py
index e780cd2..a774254 100644
--- a/heating.py
+++ b/heating.py
@@ -1,14 +1,14 @@
-import RPi.GPIO as GPIO
from abc import ABC, abstractmethod
+import time
+import os
# Abstract Class for heating
class heating(ABC):
def __init__(self, db):
self.temperature = 0
- self.taget = 0
+ self.target = 19
self.on = False
self.db = db
- self.setup()
def up(self, increase=0.5):
self.target += increase
@@ -19,28 +19,38 @@ class heating(ABC):
self.update()
def update(self):
+ self.temperature = self.get_temperature()
if self.temperature < self.target:
self.on = True
else:
self.on = False
- '''
+ '''
+ self.db.exec(
+ "insert into temperature values (?,?,?,?)",
+ (date, time, self.temperature, self.target),
+ )
+ self.db.exec(
+ "insert into history values (?,?,?)",
+ (date, time, heatingon(int)),
+ )
+ '''
+
+ """
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
@@ -48,19 +58,54 @@ class heating(ABC):
def get_temperature(self):
pass
-# inherrited from heating class
+
+# inherited 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)
+ def __init__(self,db):
# 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
+ # https://www.ics.com/blog/gpio-programming-using-sysfs-interface
+ # https://medium.com/initial-state/how-to-build-a-raspberry-pi-temperature-monitor-8c2f70acaea9
+
+ # run init from super class
+ super().__init__(db)
+
+ # constant values
+ self.RELAY = 17 # relay GPIO pin
+ self.SENSORID = "28-0621412f1b5a" # serial number of sensor
+
+ # setup GPIO for the relay
+
+ os.system(
+ "echo {} >/sys/class/gpio/export".format(self.RELAY)
+ )
+ os.system(
+ "echo out >/sys/class/gpio/gpio{}/direction".format(
+ self.RELAY
+ )
+ )
+ # file that the temperature gets written t
+ self.sensor = "/sys/bus/w1/devices/{}/w1_slave".format(
+ self.SENSORID
+ )
+
def turn_on(self):
- GPIO.output(self.RELAY, GPIO.HIGH) # on
+ # turn the heating on by triggering the relay
+ os.system(
+ "echo 1 >/sys/class/gpio/gpio{}/value".format(self.RELAY)
+ )
+
+ def turn_off(self):
+ # turn the heating off by triggering the relay
+ os.system(
+ "echo 0 >/sys/class/gpio/gpio{}/value".format(self.RELAY)
+ )
+
def get_temperature(self):
- pass
+ with open(self.sensor, "r") as file:
+ data = file.read()
+ # parse temperature from file
+ return (
+ float(data.split("\n")[1].split(" ")[9].split("=")[1])
+ / 1000
+ )