-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (57 loc) · 2.07 KB
/
main.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
#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
import datetime
# import dhtreader
from config import *
from config import SPICLK, SPIMISO, SPICS, RELAY
from gpio import *
if __name__ == '__main__':
print("initializing gpio")
initialize_gpio()
print("initializing adc")
adc = ADC(adc_number=adc_number, clock_pin=SPICLK, mosi_pin=SPIMISO, miso_pin=SPIMISO, cs_pin=SPICS)
print("initializing relay")
relay = Relay(RELAY)
print("entering loop")
try:
while True:
# k=1
# while k<10:
# try:
# print 'dht read start'
# tempDHT, humidity = dhtreader.read(22, 26)
# print 'dht read successful'
# break
# except TypeError:
# k += 1
#
temp = adc.read_temperature_c().temp_f
# tempF2 = tempDHT * 2 + 30
# itempAve = float((tempF+tempF2)/2)
timenow = datetime.datetime.now()
if temp > target_temp_f and not relay.on:
try:
relay.turn_on()
except CompressorException:
pass
elif temp < target_temp_f and relay.on:
try:
relay.turn_off()
except CompressorException:
pass
print("temp is".format(temp))
# if databaseConnect:
# sql = "INSERT INTO temperature (year,month,day,hour,minute,second,temperature,temperature2, humidity) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
# timenow.year, timenow.month, day, hour, minute, second, tempF, tempF2, humidity)
# cur.execute(sql)
time.sleep(2)
except Exception as e:
print(f"main loop crashed: {e}")
finally:
print("attempting to turn of fridge.")
try:
relay._turn_off()
print("relay disengage signal sent. program terminating")
except:
print("failed to disengage relay. program terminating")