-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
43 lines (40 loc) · 1.49 KB
/
main.py
File metadata and controls
43 lines (40 loc) · 1.49 KB
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
import RPi.GPIO as GPIO
import sys
import time
from datetime import datetime
from lib.relay import Relay
from lib.temp_sensor import TempSensor
relay_pin = 16
# After turning the relay on or maintaining current status, sleep this period before checking the temperature again
period = 30
# Set the GPIO mode to use the board numbers
GPIO.setmode(GPIO.BOARD)
# Initialize the AC relay pin in the off state
GPIO.setup(relay_pin, GPIO.OUT, initial=0)
upper_threshold = 80.00
lower_threshold = 79.00
def execute(relay, temp_sensor):
while True:
temp = temp_sensor.get_temp()
print('Temperature at {now} is {temp} degrees Fahrenheit.'.format(now=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),temp=temp))
if temp >= upper_threshold and relay.is_on == False:
print("Turning unit on...")
relay.turn_on()
time.sleep(period)
elif temp < lower_threshold and relay.is_on == True:
print('Turning unit off...')
relay.turn_off()
# In this case, sleep for a manufacturer-specified reset period for the AC unit. Not all units may have such a recommendation.
time.sleep(relay.reset_period)
else:
print('Maintaining current status...')
time.sleep(period)
if __name__ == '__main__':
try:
relay = Relay(relay_pin)
temp_sensor = TempSensor()
execute(relay, temp_sensor)
except KeyboardInterrupt:
print('Exiting...')
GPIO.cleanup()
sys.exit(0)