SHARE
TWEET

Untitled

a guest Aug 12th, 2016 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime as dt
  2. import RPi.GPIO as GPIO
  3. import Adafruit_DHT as Adafruit_DHT
  4.  
  5. class CoolingSchedule:
  6.  
  7.     def __init__(self):
  8.        
  9.         self.temp_s = None
  10.         self.temp_p = None
  11.  
  12.         self.humid_s = None
  13.         self.humid_p = None
  14.  
  15.         self.status = False
  16.  
  17.         self.lcd = None
  18.  
  19.         self.log_file = "logs/log_cooling.txt"
  20.  
  21.     def setup(self, lcd):
  22.        
  23.         # cooling
  24.         GPIO.setup(26, GPIO.OUT)
  25.  
  26.         # initialize to 0
  27.         GPIO.output(26, GPIO.HIGH)
  28.  
  29.         # initialize lcd for cooling
  30.         self.lcd = lcd
  31.  
  32.         # log system startup
  33.         self.log("SYS START: COOLING")
  34.  
  35.         # display startup on lcd
  36.         cur_time = dt.datetime.now()
  37.         dsp_time = cur_time.strftime("%H:%M:%S")
  38.  
  39.         msg_top = "SYS START:    AC\n"
  40.         msg_btm = "{:8}".format(dsp_time)
  41.  
  42.     def teardown(self):
  43.         # clear GPIO on program failure
  44.         GPIO.output(26, GPIO.HIGH)
  45.  
  46.     def log(self, msg):
  47.        
  48.         # get time
  49.         cur_time = dt.datetime.now()
  50.         cur_time = cur_time.strftime("%Y/%m/%d %H:%M:%S")
  51.  
  52.         # get variables
  53.         status = self.status
  54.         temp = self.temp_s
  55.         humid = self.humid_s
  56.  
  57.         # log
  58.         pin_26 = "0" if GPIO.input(26) else "1"
  59.  
  60.         log_msg = "[ {:>22} ] : [ {} ] Status: {:1}, Temp: {}, Humid: {}%".format(msg, cur_time, status, temp, humid)
  61.         log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("Pin", 26, pin_26)
  62.  
  63.         log_file = open(self.log_file, "a")
  64.         log_file.write(log_msg)
  65.         log_file.close()
  66.  
  67.         # display on LCD
  68.         dsp_status = "ON" if status else "OFF"
  69.         msg_top = "TEMP:     {:5}\n".format(self.temp_s)
  70.         msg_btm = "AC:          {:3}".format(dsp_status)
  71.  
  72.         self.lcd.showMessage(msg_top, msg_btm)
  73.  
  74.         # use these for debugging
  75.         #print "[ {:>22} ] : [ {} ] Status: {:1}, Temp: {}, Humid: {}%".format(msg, cur_time, status, temp, humid)
  76.         #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 26, pin_26)
  77.        
  78.     def start(self):
  79.        
  80.         # set status
  81.         self.status = True
  82.  
  83.         # set relay on
  84.         GPIO.output(26, GPIO.LOW)
  85.  
  86.         # call log
  87.         self.log("COOLING ON")
  88.  
  89.     def stop(self):
  90.        
  91.         # set status
  92.         self.status = False
  93.        
  94.         # set relay off
  95.         GPIO.output(26, GPIO.HIGH)
  96.  
  97.         # call log
  98.         self.log("COOLING OFF")
  99.  
  100.     def update(self):
  101.  
  102.         # get temperature and humidity
  103.         humid, temp = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
  104.  
  105.         # check value exists
  106.         if temp is not None and humid is not None:
  107.  
  108.             # calculate farenheit
  109.             temp = temp * 9 / 5.0 + 32
  110.  
  111.             # check if temp has been saved
  112.             if self.temp_p == None or self.humid_p == None:
  113.  
  114.                 # set initial value
  115.                 self.temp_s = "{:.2f}".format(temp)
  116.                 self.temp_p = temp
  117.  
  118.                 self.humid_s = "{:.2f}".format(humid)
  119.                 self.humid_p = humid
  120.  
  121.                 # saved initial value, skip the rest
  122.                 return
  123.  
  124.             # at this point, temp has been checked more than once
  125.             # check margin (faulty reading), within 3 degrees
  126.             margin = abs(self.temp_p - temp)
  127.             if margin < 5:
  128.  
  129.                 # check status
  130.                 if not self.status and temp > 77:
  131.  
  132.                     # start
  133.                     self.start()
  134.  
  135.                 elif self.status and temp < 73:
  136.  
  137.                     # stop
  138.                     self.stop()
  139.  
  140.                 else:
  141.  
  142.                     # log
  143.                     self.log("COOLING LOG")
  144.  
  145.             self.temp_s = "{:.2f}".format(temp)
  146.             self.temp_p = temp  
  147.  
  148.             self.humid_s = "{:.2f}".format(humid)
  149.             self.humid_p = humid
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top