def control(self, devices, control, GPIO): import growControl as gc oldStatus = self.status testOn = gc.getMinuteDiff(self.timeOn) testOff = gc.getMinuteDiff(self.timeOff) if testOn >= 0 and testOff < 0: # time in the off times, set light off self.status = self.relayOn print "Controling Light" + str(self.id) + ": Set to On" else: # if the light is not between the off times, it should be on self.status = self.relayOff print "Controling Light" + str(self.id) + ": Set to Off" if oldStatus != self.status: print "STATUS CHANGE: LIGHT " + str(seld.id) # this must always be the last thing to run in the control method # it checks if any of the temperature sensors are exceeding the limit set in control and if so, sets light to low # by being the last check in the control method, it insures a high temp will cause light on for d in devices: if d.growType == "tempSensor": if d.lastTemp >control.MaxTemp: self.status = self.relayOff gc.errorDisplay("-- Light " + str(self.id) + " status changed because of high reading on Temp Sensor " + str(d.id)) GPIO.output(self.pin, self.status) #finally set the light value
def control(self, devices, control, GPIO): import growControl as gc oldStatus = self.status #record the original status so we know if it changes #run checks to see what type of fan and what controls to use, then actually control it if self.fanOnType == 1: #print "fanOnType == 1" testOn = gc.getMinuteDiff(self.fanOnAt) testOff = gc.getMinuteDiff(self.fanOffAt) #print "" #print "Fan " + str(self.id) + " testOn = " + str(testOn) #print "Fan " + str(self.id) + " testOff = " + str(testOn) if testOn >= 0 and testOff < 0: # time in the off times, set fan off self.status = self.relayOn print "Controling Fan" + str(self.id) + ": Set to On" else: # if the fan is not between the off times, it should be on self.status = self.relayOff print "Controling Fan" + str(self.id) + ": Set to Off" elif self.fanOnType ==2: # this type runs for fanOnAt minutes, then turns off for fanOffAt minutes print "fanOnType == 2 is not yet supported, please change the type of fan #" + str(self.id) elif self.fanOnType ==3: # this type is always on print "fanOnType == 3" self.status = self.relayOn else: print "In fan" + str(self.id) + " fanOnType not defined or not recognized, setting to always on" self.status = self.relayOn if oldStatus != self.status: print "STATUS CHANGE: FAN " + str(self.id) #this should always be the last thing to run in the control method # it checks if any of the temperature sensors are exceeding the limit set in control and if so, sets fan to high #by being the last check in the control method, it insures a high temp will cause fans on for d in devices: if d.growType == "tempSensor": if d.lastTemp > control.MaxTemp: self.status = self.relayOn gc.errorDisplay("-- Fan " + str(self.id) + " status changed because of high reading on Temp Sensor " + str(d.id)) GPIO.output(self.pin, self.status) #finally set the fan value
def checkRelayType(GPIO, rt): if GPIO == "TEST": #testing on PC(GPIO does not exist here) testCase relayOn = 1 relayOff = 0 else: if rt == "NORMALLY_OPEN": # The relays are normally open, the attached device is not receiving power when the input pin is set to 0V relayOn = GPIO.HIGH relayOff = GPIO.LOW elif rt == "NORMALLY_CLOSED": # The relays are normally closed, the attached device is receiving power when the input pin is set to 0V relayOn = GPIO.LOW relayOff = GPIO.HIGH else: gc.errorDisplay("The relay type of GPIO is not defined properly") relayOn = "ERROR" relayOff = "ERROR" return [relayOn, relayOff]
gc.initFile(inputDevices+setableDevices,control) #variable that kills everything on = 1 print "Starting the main control loop" while on: print "" print "-----------------------" print "--Starting Main Loop --" print "-----------------------" control.check(gc.getDateTime(0,0,0,1)) # needs the devices so it can recreate the headers for the file names print "Reading Status" gc.readStatus(inputDevices) print "Running deviceControl" gc.deviceControl(setableDevices,control, gc.getDateTime(0,0,1,0),GPIO) #time in is epoch print "Running writeStatus" gc.writeStatus(inputDevices+setableDevices,control.fileName, gc.getDateTime(1,1,0,0), gc.getDateTime(0,0,1,0)) print "Going to sleep" sleep(control.recordInterval) except KeyboardInterrupt: # allow the user to end program with CTRL+C gc.errorDisplay("User Initiated Quit") except Exception as e: # If there are any errors, this should catch them gc.errorDisplay("Unhandeled Exception: " + str(sys.exc_info()[0])) gc.outputException(control.fileName, e) finally: # Reset GPIO settings GPIO.cleanup()
def readConfig(GPIO): # This will eventually be the function that reads in the configuration file and does all of the initial validation # Until then, this function will have kind of random things happening in it # This function will eventually only need to be passed the GPIO module and the config file name # Check the relay type, allows support for normally open and normally closed relays # The intention here is to make the device methods easier to understand # by not having things turn on when set to low print "Starting to parse the config file" import growControl as gc import ConfigParser curTimeEpoch = gc.getDateTime(0,0,1,0) currentTimeString = gc.getDateTime(0,0,0,1) def ConfigSectionMap(section): dict1 = {} options = Config.options(section) for option in options: try: dict1[option] = Config.get(section, option) if dict1[option] == -1: DebugPrint("skip: %s" % option) except: print("exception on %s!" % option) dict1[option] = None return dict1 inputs = [] outputs = [] controls = -1 Config = ConfigParser.ConfigParser() configFile = '/home/pi/git/growControl/growControl.config' #configFile = 'C:\\Users\\Neil\\Documents\\GitHub\\growControl\\growControl.config' Config.read(configFile) print "Devices found in the config file:" sectionNames = Config.sections() print sectionNames for ii in range(len(sectionNames)): try: name = sectionNames[ii][:-3] number = int(sectionNames[ii][-3:]) except: gc.errorDisplay("Error in reading in a section name from the config file") #controls if name == "controls": print "Creating controls" maxTemp = int(ConfigSectionMap(sectionNames[ii])['max_temp']) minTemp = int(ConfigSectionMap(sectionNames[ii])['min_temp']) recordInterval =int(ConfigSectionMap(sectionNames[ii])['record_interval']) #how often to record, in seconds baseFileName = ConfigSectionMap(sectionNames[ii])['base_file_name'] #the file name to start with fileLength = int(ConfigSectionMap(sectionNames[ii])['file_length']) #number of lines to record in the file controls = gc.control.control(maxTemp,minTemp,recordInterval,fileLength,baseFileName, currentTimeString) #temp sensor elif name == "temp": print "Creating " + sectionNames[ii] id = number pin = int(ConfigSectionMap(sectionNames[ii])['pin']) sensorType = ConfigSectionMap(sectionNames[ii])['sensor_type'] libraryPath = ConfigSectionMap(sectionNames[ii])['library_path'] inputs.append(gc.tempSensor.tempSensor(number, pin, sensorType, libraryPath)) #fan elif name == "fan": print "Creating " + sectionNames[ii] id = number pin = int(ConfigSectionMap(sectionNames[ii])['pin']) fanOnType= int(ConfigSectionMap(sectionNames[ii])['fan_on_type']) timeOn = int(ConfigSectionMap(sectionNames[ii])['time_on']) timeOff = int(ConfigSectionMap(sectionNames[ii])['time_off']) relayOn, relayOff = checkRelayType(GPIO, ConfigSectionMap(sectionNames[ii])['relay_type']) outputs.append(gc.fan.fan(number, pin, fanOnType, timeOn, timeOff, relayOn, relayOff)) #light elif name == "light": print "Creating " + sectionNames[ii] id = number pin = int(ConfigSectionMap(sectionNames[ii])['pin']) timeOn = int(ConfigSectionMap(sectionNames[ii])['time_on']) timeOff = int(ConfigSectionMap(sectionNames[ii])['time_off']) relayOn, relayOff = checkRelayType(GPIO, ConfigSectionMap(sectionNames[ii])['relay_type']) outputs.append(gc.light.light(number, pin, timeOn, timeOff, relayOn, relayOff)) return [inputs, outputs, controls]