-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature.py
178 lines (152 loc) · 4.8 KB
/
temperature.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
import datetime
import logging
import time
import RPi.GPIO as GPIO
import adafruit_mcp9808
import board
import busio
import mapSun
import relay
logger = logging.getLogger('temperature')
h_hot = 0
h_cold = 0
t_hot = 0
t_cold = 0
heater_status = 0
uvb_status = 0
day_status = 0
night_status = 0
season = "unknown"
spring = "spring"
summer = "summer"
autumn = "autumn"
winter = "winter"
cycle = "unknown"
spring_day = 98
summer_day = 100
autumn_day = 97
winter_day = 95
spring_night = 74
summer_night = 75
autumn_night = 73
winter_night = 72
fail_safe = 65
temp_set = fail_safe
temp_rest = 5
rest_count = 0
rest_limit = 12
spring_season = "03-01"
summer_season = "06-01"
autumn_season = "09-01"
winter_season = "12-01"
def manage(tod):
find_season(tod)
control_heat()
def find_season(tod):
global season, cycle
date_now = datetime.datetime.now().strftime("%m-%d")
if date_now > winter_season:
season = winter
elif date_now > autumn_season:
season = autumn
elif date_now > summer_season:
season = summer
elif date_now > spring_season:
season = spring
else:
season = winter
cycle = tod
logger.debug("Season: {} ".format(season))
def control_heat():
global temp_set
while (datetime.datetime.now() < mapSun.sunset) and (cycle == "day") and (season == winter):
temp_set = winter_day
control_elements()
break
while (datetime.datetime.now() < mapSun.sunset) and (cycle == "day") and (season == autumn):
temp_set = autumn_day
control_elements()
break
while (datetime.datetime.now() < mapSun.sunset) and (cycle == "day") and (season == summer):
temp_set = summer_day
control_elements()
break
while (datetime.datetime.now() < mapSun.sunset) and (cycle == "day") and (season == spring):
temp_set = spring_day
control_elements()
break
while ((datetime.datetime.now() > mapSun.sunset) or (datetime.datetime.now() < mapSun.sunrise)) \
and (cycle == "night") and (season == winter):
temp_set = winter_night
control_elements()
break
while ((datetime.datetime.now() > mapSun.sunset) or (datetime.datetime.now() < mapSun.sunrise)) \
and (cycle == "night") and (season == autumn):
temp_set = autumn_night
control_elements()
break
while ((datetime.datetime.now() > mapSun.sunset) or (datetime.datetime.now() < mapSun.sunrise)) \
and (cycle == "night") and (season == summer):
temp_set = summer_night
control_elements()
break
while ((datetime.datetime.now() > mapSun.sunset) or (datetime.datetime.now() < mapSun.sunrise)) \
and (cycle == "night") and (season == spring):
temp_set = spring_night
control_elements()
break
def temp_gradiant():
temp_diff = summer_day - summer_night
time_diff = mapSun.noon - datetime.datetime.now()
def control_elements():
global rest_count
check_temp()
if datetime.datetime.now() > mapSun.noon and (t_hot < fail_safe or t_hot < temp_set - 5):
relay.emergency_heat()
elif t_hot < temp_set:
relay.heater_on()
elif t_hot > temp_set:
relay.heater_off()
else:
relay.heater_off()
temp_status()
time.sleep(temp_rest)
def check_temp():
global h_hot, t_hot, h_cold, t_cold
try:
t_hot = (adafruit_mcp9808.MCP9808(busio.I2C(board.SCL, board.SDA)).temperature * (9 / 5)) + 32
except Exception as e:
logger.error("Failed to detect temperature: {}".format(e))
def check_relays():
global uvb_status, day_status, night_status, heater_status
try:
if GPIO.input(relay.pin_heater):
heater_status = 0
else:
heater_status = 1
if GPIO.input(relay.pin_light):
day_status = 0
else:
day_status = 1
if GPIO.input(relay.pin_uvb):
uvb_status = 0
else:
uvb_status = 1
if GPIO.input(relay.pin_night):
night_status = 0
else:
night_status = 1
except Exception as e:
logger.error("Failed to control relays: {}".format(e))
def temp_status():
from main import upload_temps
if upload_temps:
from mySql import insert
try:
insert(datetime.datetime.now(), cycle, season, temp_set, t_hot, uvb_status, day_status,
night_status, heater_status)
except Exception as e:
logger.error("Failed to insert temperature data: {}".format(e))
logger.debug("Current time: {} Cycle: {} Season: {} Temp_Set {} Temp_Read {} UVB {} Day {} Night {} Heat {} ".
format(datetime.datetime.now(), cycle, season, temp_set, t_hot, uvb_status, day_status,
night_status, heater_status))