Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime as dt
- import RPi.GPIO as GPIO
- import Adafruit_DHT as Adafruit_DHT
- class CoolingSchedule:
- def __init__(self):
- self.temp_s = None
- self.temp_p = None
- self.humid_s = None
- self.humid_p = None
- self.status = False
- self.lcd = None
- self.log_file = "logs/log_cooling.txt"
- def setup(self, lcd):
- # cooling
- GPIO.setup(26, GPIO.OUT)
- # initialize to 0
- GPIO.output(26, GPIO.HIGH)
- # initialize lcd for cooling
- self.lcd = lcd
- # log system startup
- self.log("SYS START: COOLING")
- # display startup on lcd
- cur_time = dt.datetime.now()
- dsp_time = cur_time.strftime("%H:%M:%S")
- msg_top = "SYS START: AC\n"
- msg_btm = "{:8}".format(dsp_time)
- def teardown(self):
- # clear GPIO on program failure
- GPIO.output(26, GPIO.HIGH)
- def log(self, msg):
- # get time
- cur_time = dt.datetime.now()
- cur_time = cur_time.strftime("%Y/%m/%d %H:%M:%S")
- # get variables
- status = self.status
- temp = self.temp_s
- humid = self.humid_s
- # log
- pin_26 = "0" if GPIO.input(26) else "1"
- log_msg = "[ {:>22} ] : [ {} ] Status: {:1}, Temp: {}, Humid: {}%".format(msg, cur_time, status, temp, humid)
- log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("Pin", 26, pin_26)
- log_file = open(self.log_file, "a")
- log_file.write(log_msg)
- log_file.close()
- # display on LCD
- dsp_status = "ON" if status else "OFF"
- msg_top = "TEMP: {:5}\n".format(self.temp_s)
- msg_btm = "AC: {:3}".format(dsp_status)
- self.lcd.showMessage(msg_top, msg_btm)
- # use these for debugging
- #print "[ {:>22} ] : [ {} ] Status: {:1}, Temp: {}, Humid: {}%".format(msg, cur_time, status, temp, humid)
- #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 26, pin_26)
- def start(self):
- # set status
- self.status = True
- # set relay on
- GPIO.output(26, GPIO.LOW)
- # call log
- self.log("COOLING ON")
- def stop(self):
- # set status
- self.status = False
- # set relay off
- GPIO.output(26, GPIO.HIGH)
- # call log
- self.log("COOLING OFF")
- def update(self):
- # get temperature and humidity
- humid, temp = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
- # check value exists
- if temp is not None and humid is not None:
- # calculate farenheit
- temp = temp * 9 / 5.0 + 32
- # check if temp has been saved
- if self.temp_p == None or self.humid_p == None:
- # set initial value
- self.temp_s = "{:.2f}".format(temp)
- self.temp_p = temp
- self.humid_s = "{:.2f}".format(humid)
- self.humid_p = humid
- # saved initial value, skip the rest
- return
- # at this point, temp has been checked more than once
- # check margin (faulty reading), within 3 degrees
- margin = abs(self.temp_p - temp)
- if margin < 5:
- # check status
- if not self.status and temp > 77:
- # start
- self.start()
- elif self.status and temp < 73:
- # stop
- self.stop()
- else:
- # log
- self.log("COOLING LOG")
- self.temp_s = "{:.2f}".format(temp)
- self.temp_p = temp
- self.humid_s = "{:.2f}".format(humid)
- self.humid_p = humid
Add Comment
Please, Sign In to add comment