"""

temp.py is a Python Script Designed to Automate an Incuabtor.

Copyright Alek Vasek, 2022

"""
# Importing Modules
import time
import Adafruit_DHT
import RPi.GPIO as GPIO

# Setting Temperature Sensor as DHT22 and located on pin 4
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4


# Sets Relay on Pin 8 for the Incubator
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT)


# Creates or Opens a TXT File Called Temperature.txt
temp = open("Temperature.txt", "a")


# These are the Functions for Turning on or off the Heat Lamp
def On():
    GPIO.output(8, True)

def Off():
    GPIO.output(8, False)

# This Sets the Amount of Time to Run the Incubator, and convert days to minutes
days = 3
repeats = days*1440

# This is the For Loop that Repeats for the Amount of time Specified Above,
# This Loop is in Charge of Checking the Temperature,
# Writing the Temperature plius the Date to a txt file,
# And Controlling the Heat Lamp
for x in range(0, repeats):
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    Temp = temperature*1.8+32
    date = time.ctime()
    print("Temp={0}F  Humidity={1}% Date={2}".format(Temp, humidity, time.ctime()))
    temp.write("Temp={0}F  Humidity={1}% Date={2}".format(Temp, humidity, time.ctime()))
    temp.write("\n")
    
    if Temp  > 95.00:
        Off()
    elif Temp < 80.00:
        On()
    else:
        Off()
    
    time.sleep(60)



#This Closes the TXT file and Deactivates the Pins
temp.close()
GPIO.cleanup()
