This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermometer.py
187 lines (143 loc) · 4.16 KB
/
thermometer.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import glob
import time
import RPi.GPIO as GPIO
import json
from datetime import datetime
from firebase import firebase
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
import requests.packages.urllib3
firebase = firebase.FirebaseApplication('https://iot-homebrew.firebaseio.com', None)
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
on_pin = 24
on_pin2 = 26
GPIO.setmode(GPIO.BOARD)
GPIO.setup(on_pin, GPIO.OUT)
GPIO.setup(on_pin2, GPIO.OUT)
override = 0
desired_temp = 0
hysteresis = 0.5
extra_resistor_cutoff_temp = 5
power_is_on = False
first_resistor_power_on = False
second_resistor_power_on = False
get_current_program_interval = 5
current_program = {}
current_step_id = {}
content_type = {'X_CONTENT_TYPE': 'application/json'}
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def power_on():
print('FULL POWER')
power_on_first_resistor()
power_on_second_resistor()
def power_on_first_resistor():
print('First resistor on')
global power_is_on
global first_resistor_power_on
first_resistor_power_on = True
GPIO.output(on_pin, False)
if power_is_on == False
power_is_on = True
post_power(power_is_on)
def power_on_second_resistor():
print('Second resistor on')
global power_is_on
global second_resistor_power_on
second_resistor_power_on = True
GPIO.output(on_pin2, False)
if power_is_on == False
power_is_on = True
post_power(power_is_on)
def power_off():
print('Virta pois')
global power_is_on
global first_resistor_power_on
global second_resistor_power_on
power_is_on = False
first_resistor_power_on = False
second_resistor_power_on = False
GPIO.output(on_pin, True)
GPIO.output(on_pin2, True)
post_power(power_is_on)
def power_off_second_resistor():
print('Second resistor off')
global second_resistor_power_on
second_resistor_power_on = False
GPIO.output(on_pin2, True)
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp = float(temp_string) / 1000.0
return temp
def post_temp(current_temp):
global content_type
temp_info = {"timestamp": time.time(), "temp": current_temp, "desiredTemp": desired_temp, "step": current_step_id}
firebase.post(url='/Temperatures', data=temp_info, headers=content_type)
def post_power(power):
global content_type
firebase.put('/Power', "timestamp", time.time())
firebase.put('/Power', "state", power)
def get_current_program():
global desired_temp
global current_program
current_program = firebase.get('/CurrentStep', None)
def get_override():
global override
power = firebase.get('/Power', None)
override = power["override"]
def get_desired_temp():
if current_program != None:
return float(current_program["temp"])
return 0
def set_auto_power(current_temp, desired_temp):
first_resistor_power_off_temp = desired_temp - hysteresis
second_resistor_power_off_temp = first_resistor_power_off_temp - extra_resistor_cutoff_temp
if current_temp > first_resistor_power_off_temp and power_is_on == True:
power_off()
elif current_temp > second_resistor_power_off_temp and second_resistor_power_on == True:
power_off_second_resistor()
elif current_temp < second_resistor_power_off_temp and second_resistor_power_on == False:
power_on()
elif current_temp < first_resistor_power_off_temp and first_resistor_power_on == False:
power_on_first_resistor()
# Set initial state
power_off()
# Run program
print('Aloitetaan')
print('Laitefilu: ' + device_file)
loops = 0
while True:
if loops == 0:
get_current_program()
loops = get_current_program_interval
loops = loops - 1
current_temp = read_temp()
desired_temp = get_desired_temp()
print("desired temp: " + str(desired_temp) + ", current temp: " + str(current_temp))
post_temp(current_temp)
# Override
get_override()
if override == 1:
power_off()
elif override == 2:
power_on()
else:
override = 0
# Automatic
if override != 0:
print("Overrided " + str(override))
else
set_auto_power(current_temp, desired_temp)
time.sleep(1)