-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermostat.py
59 lines (47 loc) · 1.49 KB
/
thermostat.py
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
#thermostat controller
from w1thermsensor import W1ThermSensor
import paho.mqtt.publish as publish
import RPi.GPIO as GPIO
import time
import json
class thermostat:
def __init__(self,temp,setting,switchPin,pubName,host):
self.temp = temp
self.setting = setting
self.switchPin = switchPin
self.switchPos = 0
self.pubName = pubName
self.host = host
def settemp(self,temp):
self.temp = temp
def setsetting(self,setting):
if setting>40 and setting<90:
self.setting = setting
def setswitchPin(self,switchPin):
self.switchPin = switchPin
def gettemp(self):
return self.temp
def getsetting(self):
return self.setting
def getswitchPos(self):
return self.switchPos
def json_encoding(self):
return json.dumps(self.__dict__)
def switch(self):
if self.temp >= self.setting:
GPIO.output(self.switchPin,0)
self.switchPos = 0
else:
GPIO.output(self.switchPin,1)
self.switchPos = 1
def start(self):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.switchPin, GPIO.OUT)
GPIO.output(self.switchPin,0)
sensor = W1ThermSensor()
while(True):
self.temp = sensor.get_temperature(W1ThermSensor.DEGREES_F)
self.switch()
publish.single(self.pubName, self.json_encoding(), hostname=self.host)
time.sleep(5)