summaryrefslogtreecommitdiff
path: root/heating.py
blob: a7742544ff3a1396f14f6a68862acb32402f73ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from abc import ABC, abstractmethod
import time
import os

# Abstract Class for heating
class heating(ABC):
    def __init__(self, db):
        self.temperature = 0
        self.target = 19
        self.on = False
        self.db = db

    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):
        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 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


# inherited from heating class
# overriding abstract methods from heating class
class rpi_heating(heating):
    def __init__(self,db):
        # https://tutorials-raspberrypi.com/raspberry-pi-control-relay-switch-via-gpio/
        # 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):
        # 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):
        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
        )