def opc(self, addr): try: # print addr # print "OPC_INIT" pca.pca_init(addr) pca.pca_config_pm(0, 0, 0) time.sleep(0.5) # turn PM module power supplys on spi = spidev.SpiDev() spi.open(5, 1) spi.mode = 1 spi.max_speed_hz = 500000 alphasense = OPCN2(spi) time.sleep(0.5) # Turn the opc ON alphasense.on() # alphasense.on() # alphasense.set_fan_power(255) time.sleep(1) spi.close() # alphasense.on() except Exception, e: # FirmwareError: spi.close() # pca.pca_config_pm(1,1,1,1) time.sleep(0.2) print "firmware error, " + str(e) pass
def readPPD60(self, addr): pca.pca_init(addr) pca.pca_config_pm(1, 0, 0) myDustSensor = upmPpd42ns.PPD42NS(20) data = myDustSensor.getData() time.sleep(0.1) ppd42_lpo = str(data.lowPulseOccupancy) ppd42_con = str(data.concentration) return ppd42_lpo, ppd42_con
def readSHARP(self, addr): pca.pca_init(addr) pca.pca_config_pm(1, 0, 0) myDustSensor = upmPpd42ns.PPD42NS(21) data = myDustSensor.getData() time.sleep(0.1) sharp_lpo = str(data.lowPulseOccupancy) sharp_con = str(data.concentration) return sharp_lpo, sharp_con
def __selftest__(boards, type): if "gas" in str(type): pass else: print "Beginning PM module test cycle" for i in range(len(boards)): if "0x18" not in str(boards[i]): pca.pca_init(boards[i]) print "5V toggling on "+ str(boards[i]) + " for next 5 seconds" for t in range(5): pca.pca_config_pm(1,0,0) #Adjust sleep time to allow for different measurement methods (i.e. multimeter vs analyzer) time.sleep(1) pca.pca_config_pm(1,1,1) time.sleep(1) print "PM test complete, proceeding to gas test" for i in range(len(boards)): pca.pca_init(boards[i]) print "" print str("Board Address: " + str(boards[i])) print str("Test Config Slot1 Counts Slot2 Counts Ratio Result") #print str("I2C Address Slot1 Counts Slot2 Counts Result") print str("---------------------------------------------------------------------------------------") testparams = {"PBias": 28565, "ZBias": 0, "NBias": -28565} for key in testparams.keys(): pca.pca_init(boards[i]) briza.sensor1(key) briza.sensor2(key) slot1_test_params = ['raw', '0','0','0','1',key]#testparams.keys()[1]] slot2_test_params = ['raw', '0','0','0','1',key]#testparams.keys()[1]] tmpdata1 = [] tmpdata2 = [] #for i in range(5): testdata = briza.read(boards[i],slot1_test_params, slot2_test_params, selftest=True) time.sleep(0.5) #tmpdata1.append(int(testdata['raw_value_slot1'])) #tmpdata2.append(int(testdata['raw_value_slot2'])) testval1 = float((testdata['raw_value_slot1']))#reduce(lambda x, y: x + y, tmpdata1) / len(tmpdata1) testval2 = float((testdata['raw_value_slot2']))#)reduce(lambda x, y: x + y, tmpdata2) / len(tmpdata2) if (testval1/testval2)<1.035 and (testval1/testval2)>0.82: resultstr = colored("Pass", "green") else: resultstr = colored("Failed", "red") print str(str(key)+ " " + str(testval1) + " " + str(testval2)+ " " +str("%.5f" % (float(testval1)/float(testval2))) + " " + str(resultstr))
def main(boards): slot1_parameters, slot2_parameters = briza.get_calibration() for board in boards: if any(x in str(slot1_parameters[board][5]) for x in pm_types): print "PM module bypass, configured as: " + str(slot1_parameters[board][5]) #briza.opc(board) #briza.board_init(board, slot1_parameters[board][5],slot2_parameters[board][5]) #briza.board_init(board, ) #pca.pca_init(board, "OPC") else: try: pca.pca_init(board) print "Init Sensor 1 on address: %s " % board briza.sensor1(slot1_parameters[board][5]) time.sleep(0.1) print "Init Sensor 2 on address: %s " % board briza.sensor2(slot2_parameters[board][5]) # print slot1_parameters ## print slot2_parameters except: raise while True: try: dataline = [] for board in boards: if any(x in str(slot1_parameters[board][5]) for x in pm_types): #print str("PM Module / " + str(board)) newdata = briza.readPM(board, slot1_parameters[board][5]) #time.sleep(2) pca.pca_config_pm(1,0,0) #time.sleep(0.5) if newdata.has_key("typeSensor3"): newdata2 = {'concentration_slot1': newdata.get('concentration_slot3'), 'concentration_slot2': newdata.get('concentration_slot3'), 'typeSensor1': newdata.get('typeSensor3'), 'typeSensor2': newdata.get('typeSensor3'), 'raw_value_slot1': newdata.get('raw_value_slot3'), 'raw_value_slot2': newdata.get('raw_value_slot3'), "temperature": newdata.get('temperature'), "humidity": newdata.get('humidity'), "pressure": newdata.get('pressure'), "address": newdata.get('address'), 'unit1': newdata.get('unit1'),'unit2': newdata.get('unit2')} newdata.pop("concentration_slot3") newdata.pop('typeSensor3') newdata.pop('unit3') newdata.pop('raw_value_slot3') try: write_serial(newdata2) print "PM Data Sent" #bleClient(newdata2) except Exception as e: print e pass dataline.append(newdata) dataline.append(newdata2) write_serial(newdata) #time.sleep(0.2) else: #print str("Gas Module / " + str(board)) #pca.pca_init(board) newdata = briza.read(board, slot1_parameters[board], slot2_parameters[board]) dataline.append(newdata) write_serial(newdata) print "Gas Data Sent" #bleData = dict(dataline) #write_serial(newdata) #try: #write_serial(newdata) #bleClient(newdata) #bleClient(newdata) #bleClient(newdata2) #except Exception as e: # print e # pass briza.dbinsert(dataline)#writeToSocket(dataline) except AttributeError as ex: with open('errorlog.txt', 'a') as f: f.write(str(time.time()) + ',' + str(ex) + '\n') f.close() except KeyboardInterrupt: pca.pca_config_pm(1,1,1) pca.pca_config(1,1,1) with open('/home/root/errorlog.txt', 'a') as f: f.write(str(time.time()) + ',' + "Exited" + '\n') f.close() raise Exception, "exited" except IOError as ex: print ex with open('errorlog.txt', 'a') as f: f.write(str(time.time()) + ',' + str(ex) + '\n') f.close() pass
def __conf_OPC__(self): # print("conf_sensor 2:") pca.pca_config_pm(0, 0, 0) time.sleep(1) self.particles
def readPM(self, board, modType): try: temperature, humidity, pressure = self.met_sensors() if "OPC" in str(modType): self.opc(board) time.sleep(0.2) # //for i in range(0,3): pm = self.particles() time.sleep(0.1) pm1, pm2_5, pm10 = pm[0], pm[1], pm[2] # temperature, humidity, pressure = self.met_sensors() dataline = { "temperature": temperature, "humidity": humidity, "pressure": pressure, "address": board, "typeSensor1": str("PM1"), "raw_value_slot1": str(pm1), "concentration_slot1": str(pm1), "unit1": str("ug/m3"), "typeSensor2": str(str("PM2.5")), "raw_value_slot2": str(pm2_5), "unit2": str("ug/m3"), "concentration_slot2": str(pm2_5), "typeSensor3": str(str("PM10")), "raw_value_slot3": str(pm10), "unit3": str("ug/m3"), "concentration_slot3": str(pm10), } pca.pca_config_pm(1, 0, 0) return dataline elif "PPD42" in str(modType): ppd42_lpo, ppd42_con = self.readPPD42(board) dataline = { "temperature": temperature, "humidity": humidity, "pressure": pressure, "address": board, "typeSensor1": str("LPO"), "raw_value_slot1": str(ppd42_lpo), "concentration_slot1": str(ppd42_lpo), "unit1": str("lo/hi"), "typeSensor2": str(str("[PM]")), "raw_value_slot2": str(ppd42_con), "unit2": str("ug/m3"), "concentration_slot2": str(ppd42_con), } print dataline return dataline elif "PPD60" in str(modType): ppd60_lpo, ppd60_con = self.readPPD60(board) dataline = { "temperature": temperature, "humidity": humidity, "pressure": pressure, "address": board, "typeSensor1": str("LPO"), "raw_value_slot1": str(ppd60_lpo), "concentration_slot1": str(ppd60_lpo), "unit1": str("lo/hi"), "typeSensor2": str(str("[PM]")), "raw_value_slot2": str(ppd60_con), "unit2": str("ug/m3"), "concentration_slot2": str(ppd60_con), } print dataline return dataline elif "SHARP" in str(modType): sharp_lpo, sharp_con = self.readSHARP(board) dataline = { "temperature": temperature, "humidity": humidity, "pressure": pressure, "address": board, "typeSensor1": str("LPO"), "raw_value_slot1": str(sharp_lpo), "concentration_slot1": str(sharp_lpo), "unit1": str("lo/hi"), "typeSensor2": str(str("[PM]")), "raw_value_slot2": str(sharp_con), "unit2": str("ug/m3"), "concentration_slot2": str(sharp_con), } print dataline return dataline except Exception, e: pca.pca_config_pm(1, 0, 0) print "PM read failure: " + str(e) pass