def readSerial(ser,code): if ser is not None: if ser.writable(): #Read/Write serial ser.reset_output_buffer() ser.reset_input_buffer() statusBit = str.encode(str(code) + "\r\n") ser.write(statusBit) #time.sleep(0.1) reading = ser.readline() #time.sleep(0.1) return reading else: errCode = "SERIAL NOT WRITABLE" errMsg = "Serial device was not in a writable state" deviceLog().errorLog(errCode,errMsg) print("SERIAL NOT WRITABLE") reading = None return reading else: errCode = "NO SERIAL GIVEN" errMsg = "No serial value provided." deviceLog().errorLog(errCode,errMsg) print("NO SERIAL GIVEN") reading = None return reading
def sensBank(self, var, unit, size, reading): if var is not None: if size is not None: bank = [] for x in range(size): varHold = var + str(x) if self.sensorValue(varHold, unit, reading) == "ERROR": valHold = "NA" else: valHold = self.sensorValue(varHold, unit, reading) bank.insert(0, valHold) return bank else: errCode = "SIZE NOT SUPPLIED" errMsg = "Size was not passed to the sensBank function." deviceLog().errorLog(errCode, errMsg) print("SIZE NOT SUPPLIED") bank = ["NA"] return bank else: errCode = "VAR NOT SUPPLIED" errMsg = " Var was not passed to the sesBank function." deviceLog().errorLog(errCode, errMsg) print("VAR NOT SUPPLIED") bank = ["NA"] return bank
def closeSerial(ser): if ser is not None: if ser.isOpen(): ser.close() time.sleep(0.5) else: print("SERIAL NOT OPEN") return False else: errCode = "NO SERIAL GIVEN" errMsg = "No serial value provided." deviceLog().errorLog(errCode,errMsg) print("NO SERIAL GIVEN") return False
def initalizeOut(self, pin): if pin is not None: try: GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, False) #Initalize as off time.sleep(0.1) return True except Exception as e: errCode = "ERROR INITALIZING OUTPUT" errMsg = "Unable to set GPIO pin " + str(pin) + " as output." deviceLog().errorLog(errCode, errMsg) print("ERROR INITALIZING OUTPUT") return False else: return False
def initalizeIn(self, pin): if pin is not None: try: GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.IN) time.sleep(0.1) return True except Exception as e: errCode = "ERROR INITALIZING INPUT" errMsg = "Unable to set GPIO pin " + str( pin ) + " as input. The following error code appeared; " + str(e) deviceLog().errorLog(errCode, errMsg) print("ERROR INITALIZING INPUT") return False else: return False
def sensorValue(self, sensor, unit, reading): if sensor is not None: if unit is not None: if reading is not None: try: #Processing of serial reading = reading.decode("utf-8").replace("\\r", "") reading = reading.replace("\\n'", "") reading = reading.replace("b'", "") reading = reading.split(",") bank = [] for x in range(len(reading)): hold = reading[x].split("=") bank.insert(len(bank), hold) #Parsing of data for sensor value for x in range(len(bank)): if str(bank[x][0]) == str(sensor): sens_val = bank[x][1].replace(str(unit), "") sens_val = float(sens_val) break else: sens_val = "ERROR" return sens_val except Exception as e: errCode = "ERROR FINDING SENSOR" errMsg = "Error finding sensor " + str( sensor ) + ". The following error code appeared; " + str(e) deviceLog().errorLog(errCode, errMsg) print("ERROR FINDING SENSOR") sens_val = "ERROR" return sens_val else: errCode = "NO READING GIVEN" errMsg = "No reading provided for finding sensor " + str( sensor) + "." deviceLog().errorLog(errCode, errMsg) print("NO READING GIVEN FOR SENSOR " + str(sensor)) sens_val = "ERROR" return sens_val else: errCode = "NO UNIT GIVEN" errMsg = "No unit provided for sensor " + str(sensor) + "." deviceLog().errorLog(errCode, errMsg) print("NO UNIT GIVEN FOR SENSOR " + str(sensor)) sens_val = "ERROR" return sens_val else: errCode = "NO SENSOR GIVEN" errMsg = "No sensor value provided." deviceLog().errorLog(errCode, errMsg) print("NO SENSOR GIVEN") sens_val = "ERROR" return sens_val
def captureIMG(fileName): if fileName is not None: if os.path.isfile(fileName): os.remove(fileName) time.sleep(0.1) cameraPi = PiCamera() rawCapture = PiRGBArray(cameraPi) time.sleep(0.1) cameraPi.capture(rawCapture, format="bgr") image = rawCapture.array cv2.imwrite(fileName, image) cameraPi.close() return True else: errCode = "NO FILE NAME PROVIDED" errMsg = "No file name was provided for the photo to be saved as." deviceLog().errorLog(errCode, errMsg) print("NO FILE NAME PROVIDED") return False
def openSerial(): USB0 = '/dev/ttyACM0' #/dev/ttyACM0 location of serial device USB1 = '/dev/ttyUSB0' #/dev/ttyUSB0 location of serial device if os.path.exists(USB0): if serial.Serial(USB0): ser = serial.Serial(USB0, 9600, timeout=0.5) time.sleep(1.7) return ser else: errCode = "CONNECTION FAILURE" errMsg = "Serial device was unnable to connect with " + str(USB0) deviceLog().errorLog(errCode,errMsg) print("NO SERIAL CONNECTION") ser = None return ser elif os.path.exists(USB1): if serial.Serial(USB1): ser = serial.Serial(USB1, 9600, timeout=0.5) time.sleep(1.7) return ser else: errCode = "CONNECTION FAILURE" errMsg = "Serial device was unnable to connect with " + str(USB1) deviceLog().errorLog(errCode,errMsg) print("NO SERIAL CONNECTION") ser = None return ser else: errCode = "NO SERIAL" errMsg = "No serial communication device unpluged." deviceLog().errorLog(errCode,errMsg) print("NO SERIAL CONNECTION") ser = None return ser
def wAverage(self, valueBank, weightBank): if valueBank is not None: if weightBank is not None: if len(valueBank) == len(weightBank): count = 0 value = 0 for x in range(len(valueBank)): if type(valueBank[x]) is not str: try: value = value + (int(valueBank[x]) * weightBank[x]) count = count + 1 except: pass if count == 0: value = "NA" errCode = "SYSTEM FAILURE" errMsg = "Unable to process values failed while calculating weighted average for valueBank " + str( valueBank) + "." deviceLog().errorLog(errCode, errMsg) print("SYSTEM FAILURE - CALCULATION FAILURE") else: value = value / count return value else: value = "NA" errCode = "BANKS ARE NOT SAME LENGTH" errMsg = "Banks supplied are different sizes." deviceLog().errorLog(errCode, errMsg) print("BANKS ARE NOT SAME LENGTH") return value else: value = "NA" errCode = "NO WEIGHT BANK PROVIDED" errMsg = "No weight bank list was provided for the machine." deviceLog().errorLog(errCode, errMsg) print("NO WEIGHT BANK PROVIDED") return value else: value = "NA" errCode = "NO VALUE BANK PROVIDED" errMsg = "No value bank list was provided for the machine." deviceLog().errorLog(errCode, errMsg) print("NO VALUE BANK PROVIDED") return value
def Mister(self, pin, humidity, humidity_sp): if pin is not None: if humidity is not None: if humidity_sp is not None: init = self.initalizeOut(pin) if init == True: try: if int(humidity) < int(humidity_sp): GPIO.output(pin, True) return 0 #ON else: GPIO.output(pin, False) return 1 #OFF except Exception as e: errCode = "ERROR CONTROLING MISTER" errMsg = "Error occured when trying to control the mister on GPIO pin " + str( pin ) + ". The following error code appeared; " + str( e) deviceLog().errorLog(errCode, errMsg) print("ERROR CONTROLING MISTER") return 2 #ERROR else: #Could not initalize return 2 #ERROR else: errCode = "NO HUMIDITY SETPOINT GIVEN" errMsg = "No humidity setpoint value was provided for GPIO pin " + str( pin) + "." deviceLog().errorLog(errCode, errMsg) print("NO HUMIDITY SETPOINT GIVEN") return 2 #ERROR else: errCode = "NO HUMIDITY GIVEN" errMsg = "No humidity value was provided " + str(pin) + "." deviceLog().errorLog(errCode, errMsg) print("NO HUMIDITY GIVEN") return 2 #ERROR else: errCode = "NO PIN GIVEN" errMsg = "No pin value provided for the mister." deviceLog().errorLog(errCode, errMsg) print("NO PIN GIVEN FOR THE MISTER") return 2 #ERROR
def Pump(self, pin, ws, amount, flowRate): if pin is not None: if ws is not None: init = self.initalizeOut(pin) if init == True: try: #Handaling of water pumps if int(ws) >= 200: runTime = time.time() + ( 60.00 * (float(amount) / float(flowRate))) while time.time() <= runTime: GPIO.output(pin, True) return 1 #ON else: GPIO.output(pin, False) return 0 #OFF except Exception as e: errCode = "ERROR CONTROLING PUMP" errMsg = "Error occured when trying to control pump on GPIO pin " + str( pin ) + ". The following error code appeared; " + str(e) deviceLog().errorLog(errCode, errMsg) print("ERROR CONTROLING PUMP") return 2 #ERROR else: #Could not initalize return 2 #ERROR else: errCode = "NO WATER SENSOR GIVEN" errMsg = "No water sensor value was provided for GPIO pin " + str( pin) + "." deviceLog().errorLog(errCode, errMsg) print("NO WATER SENSOR GIVEN") return 2 #ERROR else: errCode = "NO PIN GIVEN" errMsg = "No pin value provided for the pump." deviceLog().errorLog(errCode, errMsg) print("NO PIN GIVEN FOR THE PUMP") return 2 #ERROR
def Light(self, pin, light): if pin is not None: if light is not None: init = self.initalizeOut(pin) if init == True: try: #Handling of day Ligh light_sp = int((float(light) / (100.00)) * (24.00)) hour = strftime("%H", gmtime()) if int(hour) <= light_sp: GPIO.output(pin, True) return 0 #ON elif int(hour) > light_sp: GPIO.output(pin, False) return 1 #OFF except Exception as e: errCode = "ERROR CONTROLING LIGHT" errMsg = "Error occured when trying to control light on GPIO pin " + str( pin ) + ". The following error code appeared; " + str(e) deviceLog().errorLog(errCode, errMsg) print("ERROR CONTROLING LIGHT") return 2 #ERROR else: #Could not initalize return 2 #ERROR else: errCode = "NO LIGHT VALUE GIVEN" errMsg = "No light value was provided on GPIO pin " + str( pin) + "." deviceLog().errorLog(errCode, errMsg) print("NO LIGHT VALUE GIVEN") return 2 #ERROR else: errCode = "NO PIN GIVEN" errMsg = "No pin value provided for the light." deviceLog().errorLog(errCode, errMsg) print("NO PIN GIVEN FOR THE LIGHT") return 2 #ERROR
def hotPlate(self, pin, output): if pin is not None: if output is not None: init = self.initalizeOut(pin) if init == True: try: #Handling of hot plate if output == True: GPIO.output(pin, GPIO.LOW) return 0 #ON elif output == False: GPIO.output(pin, GPIO.HIGH) return 1 #OFF except Exception as e: errCode = "ERROR CONTROLING HOTPLATE" errMsg = "Error occured when trying to control hotplate on GPIO pin " + str( pin ) + ". The following error code appeared; " + str(e) deviceLog().errorLog(errCode, errMsg) print("ERROR CONTROLING HOTPLATE") return 2 #ERROR else: #Could not initalize return 2 #ERROR else: errCode = "NO OUTPUT STATE GIVEN" errMsg = "No output state was provided for GPIO pin " + str( pin) + "." deviceLog().errorLog(errCode, errMsg) print("NO OUTPUT STATE GIVEN") return 2 #ERROR else: errCode = "NO PIN GIVEN" errMsg = "No pin value provided for the hotplate." deviceLog().errorLog(errCode, errMsg) print("NO PIN GIVEN FOR THE HOTPLATE") return 2 #ERROR
def atmMain(self, humiditySP, carbonSP, tempatureSP, mainLight, potLight1, potLight2, potLight3, elecSP, ser): if humiditySP is not None: if carbonSP is not None: if tempatureSP is not None: if light is not None: if ser is not None: if ser.isOpen() == True: #Output Pin variables L1_Pin = 2 #Light GPIO 8 L2_Pin = 3 #Light GPIO 9 L3_Pin = 4 #Light GPIO 7 L4_Pin = 17 #Light GPIO 0 L5_Pin = 27 #Light GPIO 2 F1_Pin = 22 #Circulation fan GPIO 3 F2_Pin = 10 #Exhaust fan GPIO 12 F3_Pin = 9 #Intake fan GPIO 13 F4_Pin = 11 #Transition fan GPIO 14 F5_Pin = 5 #Electrical exhaust fan GPIO 21 F6_Pin = 6 #Electrical intake fan GPIO 22 M1_Pin = 26 #Mister GPIO 10 sensorBank1 = network.readSerial(ser, 1) sensorBank3 = network.readSerial(ser, 3) #Temp sensors tBank = deviceControl().sensBank( "T", "C", 5, sensorBank1) tempWeight = [1, 1, 1, 1, 1] temp = self.wAverage(tBank, tempWeight) print("temp = " + str(temp)) #Humidity sensors hBank = deviceControl().sensBank( "H", "%", 5, sensorBank1) humidWeight = [1, 1, 1, 1, 1] humid = self.wAverage(hBank, humidWeight) print("humidity = " + str(humid)) #Electrical box sensors t6 = deviceControl().sensorValue( "T6", "C", sensorBank1) #Electrical box h6 = deviceControl().sensorValue( "H6", "%", sensorBank1) #Electrical box try: elecTemp = int(t6) print("Electircal Box Tempature = " + str(elecTemp)) except: elecTemp = "NA" print( "SYSTEM FAILURE - ELECTRICAL BOX SENSORS OFFLINE" ) #Carbon sensors c1 = deviceControl().sensorValue( "C1", "%", sensorBank1) try: carbon = int(c1) print("Carbon contentent = " + str(carbon)) except: carbon = "NA" print( "SYSTEM FAILURE - CARBON SENSORS OFFLINE" ) #Fire sensors fBank = deviceControl().sensBank( "F", "", 5, sensorBank3) try: fire = 0 for x in range(fBank): fire = fire + fBank[x] print("Fire levels are = " + str(fire)) except: fire = "NA" print( "SYSTEM FAILURE - FIRE SENSORS OFFLINE" ) #Output control fireLevel = 1000 if fire == "NA": return False elif fire <= fireLevel and fire != "NA": #If fire is not detected deviceControl().Light(L1_Pin, mainLight) #Light deviceControl().Light(L2_Pin, potLight1) #Light deviceControl().Light(L3_Pin, potLight2) #Light deviceControl().Light(L4_Pin, potLight3) #Light deviceControl().Fan(F1_Pin, True) #Circulation if elecSP <= elecTemp and elecTemp != "NA": #Electrical box to hot if tempatureSP <= temp and temp != "NA": #Too hot if carbonSP <= carbon and carbon != "NA": #Too much carbon dioxide deviceControl().Fan( F2_Pin, True) #Exhaust deviceControl().Fan( F3_Pin, True) #Intake deviceControl().Fan( F4_Pin, False) #Transition deviceControl().Fan( F5_Pin, True) #Electrical exhaust deviceControl().Fan( F6_Pin, True) #Electrical intake print("[f2,f3,f5,f6]") print("[1,1,1,1]") elif carbonSP > carbon and carbon != "NA": #Too little carbon dioxide deviceControl().Fan( F2_Pin, False) #Exhaust deviceControl().Fan( F3_Pin, True) #Intake deviceControl().Fan( F4_Pin, True) #Transition deviceControl().Fan( F5_Pin, False) #Electrical exhaust deviceControl().Fan( F6_Pin, True) #Electrical intake print("[f2,f3,f5,f6]") print("[0,1,0,1]") elif carbon == "NA": #Carbon sensors offline return False elif tempatureSP > temp and temp != "NA": #Too cold if carbonSP <= carbon and carbon != "NA": #Too much carbon dioxide deviceControl().Fan( F2_Pin, False) #Exhaust deviceControl().Fan( F3_Pin, False) #Intake deviceControl().Fan( F4_Pin, False) #Transition deviceControl().Fan( F5_Pin, True) #Electrical exhaust deviceControl().Fan( F6_Pin, True) #Electrical intake print("[f2,f3,f5,f6]") print("[0,0,1,1]") elif carbonSP > carbon and carbon != "NA": #Too little carbon dioxide deviceControl().Fan( F2_Pin, True) #Exhaust deviceControl().Fan( F3_Pin, False) #Intake deviceControl().Fan( F4_Pin, True) #Transition deviceControl().Fan( F5_Pin, False) #Electrical exhaust deviceControl().Fan( F6_Pin, True) #Electrical intake print("[f2,f3,f5,f6]") print("[1,0,0,1]") elif carbon == "NA": #Carbon sensor offline return False elif temp == "NA": #Tempature sensors offline return False elif elecSP > elecTemp and elecTemp != "NA": #Electrical box ok if tempatureSP <= temp and temp != "NA": #Too hot if carbonSP <= carbon and carbon != "NA": #Too much carbon dioxide deviceControl().Fan( F2_Pin, True) #Exhaust deviceControl().Fan( F3_Pin, True) #Intake deviceControl().Fan( F4_Pin, False) #Transition deviceControl().Fan( F5_Pin, False) #Electrical exhaust deviceControl().Fan( F6_Pin, False) #Electrical intake print("[f2,f3,f5,f6]") print("[1,1,0,0]") elif carbonSP > carbon and carbon != "NA": #Too little carbon dioxide deviceControl().Fan( F2_Pin, True) #Exhaust deviceControl().Fan( F3_Pin, False) #Intake deviceControl().Fan( F4_Pin, True) #Transition deviceControl().Fan( F5_Pin, False) #Electrical exhaust deviceControl().Fan( F6_Pin, True) #Electrical intake print("[f2,f3,f5,f6]") print("[1,0,0,1]") elif carbon == "NA": #Carbon sensor offline return False elif tempatureSP > temp and temp != "NA": #Too cold if carbonSP <= carbon: #Too much carbon dioxide deviceControl().Fan( F2_Pin, False) #Exhaust deviceControl().Fan( F3_Pin, False) #Intake deviceControl().Fan( F4_Pin, True) #Transition deviceControl().Fan( F5_Pin, True) #Electrical exhaust deviceControl().Fan( F6_Pin, False) #Electrical intake print("[f2,f3,f5,f6]") print("[0,0,1,0]") elif carbonSP > carbon and carbon != "NA": #Too little carbon dioxide deviceControl().Fan( F2_Pin, True) #Exhaust deviceControl().Fan( F3_Pin, False) #Intake deviceControl().Fan( F4_Pin, True) #Transition deviceControl().Fan( F5_Pin, False) #Electrical exhaust deviceControl().Fan( F6_Pin, True) #Electrical intake print("[f2,f3,f5,f6]") print("[1,0,0,1]") elif carbon == "NA": #Carbon sensor offline return False elif temp == "NA": #Tempature sensors offline return False elif elecTemp == "NA": #Electical sensors offline return False deviceControl().Mister( M1_Pin, humid, humiditySP) #Mister return True elif fire > fireLevel and fire != "NA": if int(fBank[0]) > fireLevel: deviceControl().Fire("F1") elif int(fBank[1]) > fireLevel: deviceControl().Fire("F2") elif int(fBank[2]) > fireLevel: deviceControl().Fire("F3") elif int(fBank[3]) > fireLevel: deviceControl().Fire("F4") elif int(fBank[4]) > fireLevel: deviceControl().Fire("F5") return False else: errCode = "SERIAL PORT CLOSED" errMsg = "The provided serial port is not open." deviceLog().errorLog(errCode, errMsg) print("SERIAL PORT CLOSED") return False else: errCode = "NO SERIAL PROVIDED" errMsg = "No serial instance was provided for the machine." deviceLog().errorLog(errCode, errMsg) print("NO SERIAL PROVIDED") return False else: errCode = "NO LIGHT PROVIDED" errMsg = "No light percentage value was provided for the machine." deviceLog().errorLog(errCode, errMsg) print("NO LIGHT PROVIDED") return False else: errCode = "NO TEMPATURE SETPOINT PROVIDED" errMsg = "No tempature setpoint value was provided for the machine." deviceLog().errorLog(errCode, errMsg) print("NO TEMPATURE SETPOINT PROVIDED") return False else: errCode = "NO CARBON SETPOINT PROVIDED" errMsg = "No carbon setpoint value was provided for the machine." deviceLog().errorLog(errCode, errMsg) print("NO CARBON SETPOINT PROVIDED") return False else: errCode = "NO HUMIDITY SETPOINT PROVIDED" errMsg = "No humidity setpoint value was provided for the machine." deviceLog().errorLog(errCode, errMsg) print("NO HUMIDITY SETPOINT PROVIDED") return False
* @modified August 13 2018 * @modifiedby BB * @brief testing playground to try out classes */ """ import time from atmSequence import atmosphere #from pumpSequence import pumps from networking import network from logg import deviceLog if __name__ == "__main__": startTime = time.time() plantName = "Plant1" plantStrain = "Kush" index = deviceLog().findIndex("dayLog.txt", plantName) print(index) elecSP = 40 phSP = 50 fullSP = deviceLog().findSP("autoSP.csv", int(index)) tempatureSP = fullSP[0] humiditySP = fullSP[1] carbonSP = fullSP[2] mainLight = fullSP[3] potLight1 = fullSP[4] potLight2 = fullSP[5] potLight3 = fullSP[6] ser = network.openSerial() stats = ["Tempature=20"] deviceLog().dayLog(index, plantName, plantStrain, stats)
def Fire(sensor): errCode = "FIRE" errMsg = "Fire was detected with fire sensor " + str(sensor) deviceLog().errorLog(errCode, errMsg) GPIO.cleanup() return True