def startSensor(devId): global sensors, sensor, badSensor global startTime global as3935sensor global inside, minStrikes, tuneCapacitor, minNoiseFloor, interruptGPIO, noiseFlorSet,calibrationDynamic, noiseFloor, CapValue U.toLog(-1,"==== Start "+G.program+" ===== @ i2c= " +unicode(i2cAddress),doPrint=True) startTime =time.time() i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) # switch mux on if requested and use the i2c address of the mix if enabled try: as3935sensor[devId] = RPi_AS3935(address=i2cAdd, minNoiseFloor=minNoiseFloor) as3935sensor[devId].set_indoors( inside == 1) noiseFlorSet = as3935sensor[devId].set_noise_floor(max(noiseFloor,minNoiseFloor)) as3935sensor[devId].set_min_strikes(minStrikes) if as3935sensor[devId].calibrate(tuneCapacitor) ==-1: time.sleep(10) data={"sensors":{sensor:{devId:{"eventType":"badsensor"}}}} badSensor else: badSensor = False except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) data={"sensors":{sensor:{devId:{"eventType":"badsensor"}}}} badSensor = True
def calibrateSensor(devId): global sensors, sensor global mhz16sensor global CO2normal, CO2offset, sensitivity i2cAdd = U.muxTCA9548A( sensors[sensor][devId] ) # switch mux on if requested and use the i2c address of the mix if enabled #print "calibrating" ret = "" try: CO2offset[devId] = 0 mhz16sensor[devId].calibrate() time.sleep(5) ret = getValues(devId, nMeasurements=3) if ret == "badSensor": print " calibration did not work exit " print ret time.sleep(5) return co2 = ret["CO2"] CO2offset[devId] = CO2normal[devId] - co2 #print "calib co2, CO2offset, CO2normal: ", co2, CO2offset[devId], CO2normal[devId] except Exception, e: print "ret =", ret U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def startSensor(devId, i2cAddress): global sensors, sensor global startTime global gasBaseLine, gasBurnIn global BMEsensor, firstValue U.toLog(-1, "==== Start BME680 ===== @ i2c= " + unicode(i2cAddress)) startTime = time.time() gasBaseLine = 0 gasBurnIn = [] i2cAdd = U.muxTCA9548A( sensors[sensor][devId] ) # switch mux on if requested and use the i2c address of the mix if enabled print " starting with ic2= " + str(i2cAdd) try: BMEsensor[devId] = BME680(i2c_addr=i2cAdd) except Exception, e: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) BMEsensor[devId] = "" U.muxTCA9548Areset() return
def getValues(devId): global sensor, sensors, amg88xxsensor, badSensor global oldPixels global startTime global lastMeasurement global uniformityOLD, movementOLD, horizontal1OLD, horizontal2OLD, vertical1OLD, vertical2OLD try: ret ="" if amg88xxsensor[devId] =="": badSensor +=1 return "badSensor" i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) # switch mux on if requested and use the i2c address of the mix if enabled ##print oldPixels oldPixels[devId] , maxV, minV, average, nPixels, AmbientTemperature, uniformity, movement, movementAbs= amg88xxsensor[devId].readPixels(oldPixels[devId]) #print " average %5.1f AmbientTemperature %5.1f movement %4.2f movementAbs %4.2f uniformity %4.2f "%( average, AmbientTemperature, movement, movementAbs, uniformity) #print " movement %4.2f uniformity %4.2f horizontal1 %4.1f horizontal2 %4.1f vertical1 %4.1f vertical2 %4.1f"%( movement-movementOLD, uniformity-uniformityOLD, horizontal1-horizontal1OLD, horizontal2-horizontal2OLD, vertical1-vertical1OLD, vertical2-vertical2OLD) ret = {"Movement": ( "%7.1f"%( movement ) ).strip(), "MovementAbs": ( "%7.1f"%( movementAbs ) ).strip(), # "Vertical1": ( "%7.3f"%( vertical1 ) ).strip(), # "Horizontal1": ( "%7.3f"%( horizontal1 ) ).strip(), "Uniformity": ( "%7.1f"%( uniformity ) ).strip(), "AmbientTemperature": ( "%7.1f"%( AmbientTemperature ) ).strip(), "MaximumPixel": ( "%7.1f"%( maxV ) ).strip(), "MinimumPixel": ( "%7.1f"%( minV ) ).strip(), "temp": ( "%7.1f"%( average ) ).strip(), "rawData": json.dumps(oldPixels[devId]).replace(" ","")} U.toLog(2, unicode(ret)) badSensor = 0 except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) badSensor+=1 if badSensor >3: ret = "badSensor"
def getValues(devId): global sensor, sensors, INAsensor, badSensor global actionDistanceOld, actionShortDistance, actionShortDistanceLimit, actionMediumDistance, actionMediumDistanceLimit, actionLongDistance, actionLongDistanceLimit i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) for ii in range(2): try: ShuntVoltage = INAsensor[devId].getShuntVoltage_V() BusVoltage = INAsensor[devId].getBusVoltage_mV() Current = INAsensor[devId].getCurrent_mA() Power = INAsensor[devId].getPower_mW() #print "SV:",ShuntVoltage, " BV:",BusVoltage, " C:",Current, " P:",Power data = { "ShuntVoltage": ("%7.1f" % ShuntVoltage).strip(), "BusVoltage": ("%7d" % BusVoltage).strip(), "Power": ("%7d" % Power).strip(), "Current": ("%7.1f" % Current).strip() } badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor > 2 and badSensor < 5: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, u"Current>>" + unicode(Current) + "<<") badSensor += 1
def getValues(devId): global sensor, sensors, as726xsensor, badSensor global LEDmA, doAverage, LEDBlink, gain i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) data ={"blue":"","green":"","yellow":"","orange":"","red":"","violet":"","temp":"","LEDcurrent":0} try: for ii in range(20): if as726xsensor[devId].data_ready(): break time.sleep(.1) ##print "blue" , as726xsensor[devId].blue() data["blue"] = as726xsensor[devId].blue() data["green"] = as726xsensor[devId].green() data["yellow"] = as726xsensor[devId].yellow() data["orange"] = as726xsensor[devId].orange() data["red"] = as726xsensor[devId].red() data["violet"] = as726xsensor[devId].violet() data["temp"] = as726xsensor[devId].temperature() badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor >-1 and badSensor < 5000: U.toLog(-1, u"getValues in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, unicode(data) ) badSensor+=1
def getValues(devId): global sensor, sensors, BMEsensor, badSensor global startTime global gasBaseLine, gasBurnIn, lastMeasurement, calibrateIfgt,setCalibration, firstValue try: if BMEsensor[devId] =="": badSensor +=1 return "badSensor" i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) # switch mux on if requested and use the i2c address of the mix if enabled if BMEsensor[devId].get_sensor_data(): temp = BMEsensor[devId].data.temperature press = BMEsensor[devId].data.pressure hum = BMEsensor[devId].data.humidity gas = BMEsensor[devId].data.gas_resistance heatSt = BMEsensor[devId].data.status & HEAT_STAB_MSK lastMeasurement = time.time() if setCalibration >0.: startTime = 0. gasBaseLine = setCalibration if startTime >0 and heatSt > 0 and time.time() - startTime > 25 : # wait at least 25secs and heat ok gasBurnIn.append(float(gas)) if len(gasBurnIn) > 2: U.toLog(2, "gasBurnIn: "+unicode(gasBurnIn)+ ", delta: "+ unicode(abs(gasBurnIn[-1]-gasBurnIn[-2])/max(1,(gasBurnIn[-1]+gasBurnIn[-2]))) ) if len(gasBurnIn) > 6 and abs(gasBurnIn[-1]-gasBurnIn[-2])/max(1,(gasBurnIn[-1]+gasBurnIn[-2])) < 0.0005: # wait for 13+ cycles, last measurements must be stable gasBaseLine = sum(gasBurnIn[-4:])/(4.) # take last 4 measurements U.toLog(-1," calibrated: after "+ str(int(time.time()-startTime))+" sec; nof samples:"+str(len(gasBurnIn)) +" baseline:"+str(gasBaseLine)+" heatStatus:"+str(heatSt) ) startTime = 0 # save new calibration data to file f = open(G.homeDir+"bme680.Calibration","w") f.write(json.dumps({"resistance":gasBaseLine})) f.close() if startTime ==0 : if gas > gasBaseLine*(calibrateIfgt/100.) and not firstValue and setCalibration == 0: #recalibrate over x % higher than previous calibartion (=100% clean air) U.toLog(-1,"re calibrated: due to shift high in baseline new value:"+str(gas)+"; oldbaseLine: "+str(gasBaseLine) ) gasScore = "calibrating" startSensor(devId, int(sensors[sensor][devId]["i2cAddress"]) ) else: gasScore = ("%3d"%(int(gas/ gasBaseLine *100))).strip() else: gasScore ="calibrating" temp += float(sensors[sensor][devId]["offsetTemp"]) press += float(sensors[sensor][devId]["offsetPress"]) hum += float(sensors[sensor][devId]["offsetHum"]) gas += float(sensors[sensor][devId]["offsetGas"]) data = {"temp": ("%.2f"%( temp ) ).strip(), "press": ( "%7d"%( press ) ).strip(), "hum": ( "%7d"%( hum ) ).strip(), "GasResistance":( "%7d"%( gas ) ).strip(), "GasBaseline": ( "%7d"%( gasBaseLine ) ).strip(), "AirQuality": gasScore} U.toLog(2, unicode(data)) badSensor = 0 return data except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def setLED(devId,value): global sensor, sensors, as726xsensor try: i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) if value == 0: as726xsensor[devId].enable_driver_led(False) else: as726xsensor[devId].set_driver_led_current(value) as726xsensor[devId].enable_driver_led(True) U.muxTCA9548Areset() except Exception, e: U.toLog(-1, u"setLED in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def getValues(devId): global sensor, sensors, sgp30sensor, badSensor global startTime global lastMeasurement global lastBaseLine global lastCO2, lastVOC try: ret ="" if sgp30sensor[devId] =="": badSensor +=1 return "badSensor" if time.time() - lastBaseLine[devId] > 1000: setBaseLine(devId) time.sleep(10) CO2 = 0. VOC = 0. n = 0 fail = 0 i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) # switch mux on if requested and use the i2c address of the mix if enabled for ii in range(20): try: co2eq, tvoc = sgp30sensor[devId].iaq_measure() except: continue if co2eq >0 and tvoc > 0: if lastVOC != 0 and fail < 3: if abs(co2eq - lastCO2) > 10: fail+=1 continue if abs(tvoc - lastVOC) > 3: fail +=1 continue CO2 += co2eq VOC += tvoc n += 1. #print lastCO2, lastVOC, n, ii, co2eq, tvoc, CO2/n, VOC/n if n > 6: break time.sleep(1) if CO2 == 0: badSensor+=1 else: lastCO2 = CO2/n lastVOC = VOC/n ret = {"CO2": ( "%d"%( CO2/n ) ).strip(), "VOC": ( "%d"%( VOC/n ) ).strip()} #print ret U.toLog(3, unicode(ret)) badSensor = 0 except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) badSensor+=1 if badSensor >3: ret = "badSensor"
def setLED(devId, value): global sensor, sensors, as726xsensor try: i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) if value == 0: as726xsensor[devId].enable_driver_led(False) else: as726xsensor[devId].set_driver_led_current(value) as726xsensor[devId].enable_driver_led(True) U.muxTCA9548Areset() except Exception, e: U.toLog( -1, u"setLED in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def startSensor(devId,i2cAddress): global sensors,sensor global startTime global amg88xxsensor, oldPixels U.toLog(-1,"==== Start amg88xx ===== @ i2c= " +unicode(i2cAddress)) startTime =time.time() i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) # switch mux on if requested and use the i2c address of the mix if enabled oldPixels[devId] = [0 for ii in range(64)] try: amg88xxsensor[devId] = AMG88xx_class(address=i2cAdd) except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) amg88xxsensor[devId] =""
def getValues(devId): global sensor, sensors, SI7021sensor, badSensor i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) try: temp,hum = SI7021sensor[devId].getdata() if temp =="" or hum =="" : badSensor+=1 U.muxTCA9548Areset() return "badSensor" data = {"temp":("%7.1f"%temp).strip(), "hum":("%7d"%hum).strip()} badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor >2 and badSensor < 5: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, u"temp>>" + unicode(temp)+"<<") badSensor+=1
def getValues(devId): global sensor, sensors, tmp006sensor, badSensor i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) try: temp,ambTemp = tmp006sensor[devId].getdata() if temp =="" or ambTemp =="" : badSensor+=1 U.muxTCA9548Areset() return "badSensor" data = {"temp":round(temp,2),"AmbientTemperature":round(ambTemp,2)} badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor >2 and badSensor < 5: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, u"temp>>" + unicode(temp)+"<<") badSensor+=1
def getValues(devId): global sensor, sensors, SI7021sensor, badSensor i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) try: temp, hum = SI7021sensor[devId].getdata() if temp == "" or hum == "": badSensor += 1 U.muxTCA9548Areset() return "badSensor" data = {"temp": ("%7.1f" % temp).strip(), "hum": ("%7d" % hum).strip()} badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor > 2 and badSensor < 5: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, u"temp>>" + unicode(temp) + "<<") badSensor += 1
def getValues(devId): global sensor, sensors, INAsensor, badSensor global actionDistanceOld, actionShortDistance, actionShortDistanceLimit, actionMediumDistance, actionMediumDistanceLimit, actionLongDistance, actionLongDistanceLimit i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) for ii in range(2): try: ShuntVoltage = INAsensor[devId].getShuntVoltage_V() BusVoltage = INAsensor[devId].getBusVoltage_mV() Current = INAsensor[devId].getCurrent_mA() Power = INAsensor[devId].getPower_mW() #print "SV:",ShuntVoltage, " BV:",BusVoltage, " C:",Current, " P:",Power data = {"ShuntVoltage":("%7.1f"%ShuntVoltage).strip(), "BusVoltage":("%7d"%BusVoltage).strip(), "Power":("%7d"%Power).strip(), "Current":("%7.1f"%Current).strip()} badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor >2 and badSensor < 5: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, u"Current>>" + unicode(Current)+"<<") badSensor+=1
def getMLX90614(sensor, data): global sensorMLX90614, MLX90614Started global sensors, sValues, displayInfo try: if sensor not in sensors: return data t="" a="" try: ii= MLX90614Started except: MLX90614Started=1 sensorMLX90614 ={} U.setStopCondition(on=True) for devId in sensors[sensor]: i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) if devId not in sensorMLX90614 : sensorMLX90614[devId]=MLX90614(address=i2cAdd) a = round(sensorMLX90614[devId].get_amb_temp(),1) t = round(sensorMLX90614[devId].get_obj_temp(),1) if t!="": try: t = (float(t) + float(sensors[sensor][devId]["offsetTemp"])) except: continue data["sensors"][sensor][devId] = {"temp":t} if a!="": try: a = float(a) + float(sensors[sensor][devId]["offsetTemp"]) except: continue data["sensors"][sensor][devId]["AmbientTemperature"]=a if devId in badSensors: del badSensors[devId] putValText(sensors[sensor][devId],[t,a],["temp","ambient"]) else: data= incrementBadSensor(devId,sensor,data) except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def startSensor(devId,i2cAddress): global sensors,sensor global startTime global gasBaseLine, gasBurnIn global BMEsensor, firstValue U.toLog(-1,"==== Start BME680 ===== @ i2c= " +unicode(i2cAddress)) startTime =time.time() gasBaseLine = 0 gasBurnIn = [] i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) # switch mux on if requested and use the i2c address of the mix if enabled print " starting with ic2= "+ str(i2cAdd) try: BMEsensor[devId] = BME680(i2c_addr=i2cAdd) except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) BMEsensor[devId] ="" U.muxTCA9548Areset() return
def startSensor(devId,i2cAddress): global sensors,sensor global startTime global sgp30sensor U.toLog(-1,"==== Start "+G.program+" ===== @ i2c= " +unicode(i2cAddress)) startTime =time.time() i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) # switch mux on if requested and use the i2c address of the mix if enabled try: sgp30sensor[devId] = SGP30_class(address=i2cAdd) U.toLog(-1, "SGP30 serial #", [hex(i) for i in sgp30sensor[devId].serial]) U.toLog(-1, "featureset ", [hex(i) for i in sgp30sensor[devId].featureset]) sgp30sensor[devId].iaq_init() #sgp30.set_iaq_baseline(0x8973, 0x8aae) setBaseLine(devId) except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) sgp30sensor[devId] =""
def getValues(devId): global sensor, sensors, as726xsensor, badSensor global LEDmA, doAverage, LEDBlink, gain i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) data = { "blue": "", "green": "", "yellow": "", "orange": "", "red": "", "violet": "", "temp": "", "LEDcurrent": 0 } try: for ii in range(20): if as726xsensor[devId].data_ready(): break time.sleep(.1) ##print "blue" , as726xsensor[devId].blue() data["blue"] = as726xsensor[devId].blue() data["green"] = as726xsensor[devId].green() data["yellow"] = as726xsensor[devId].yellow() data["orange"] = as726xsensor[devId].orange() data["red"] = as726xsensor[devId].red() data["violet"] = as726xsensor[devId].violet() data["temp"] = as726xsensor[devId].temperature() badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor > -1 and badSensor < 5000: U.toLog( -1, u"getValues in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, unicode(data)) badSensor += 1
def getValues(devId): global sensor, sensors, INAsensor, badSensor global actionDistanceOld, actionShortDistance, actionShortDistanceLimit, actionMediumDistance, actionMediumDistanceLimit, actionLongDistance, actionLongDistanceLimit i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) for ll1 in range(2): try: data ={"Current1":"","Current2":"","Current3":""} for ii in range(3): ShuntVoltage,Current = INAsensor[devId].getShuntVoltageCurrent(ii) BusVoltage = INAsensor[devId].getBusVoltage_V(ii) #print ii, "SV:",ShuntVoltage, " BV:",BusVoltage, " C:",Current data["ShuntVoltage"+str(ii+1)] =("%7.1f"%ShuntVoltage).strip() data["BusVoltage"+str(ii+1)] =("%7.3f"%BusVoltage).strip() data["Current"+str(ii+1)] =("%7.1f"%Current).strip() badSensor = 0 U.muxTCA9548Areset() return data except Exception, e: if badSensor >2 and badSensor < 5: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) U.toLog(-1, u"Current>>" + unicode(Current)+"<<") badSensor+=1
def startSensor(devId, i2cAddress): global sensors, sensor global startTime global mhz16sensor U.toLog(-1, "==== Start " + G.program + " ===== @ i2c= " + unicode(i2cAddress)) startTime = time.time() i2cAdd = U.muxTCA9548A( sensors[sensor][devId] ) # switch mux on if requested and use the i2c address of the mix if enabled try: mhz16sensor[devId] = mhz16_class(address=i2cAdd, sensorType=2) time.sleep(1) mhz16sensor[devId].start() except Exception, e: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) mhz16sensor[devId] = ""
def startSensor(devId, i2cAddress): global sensors, sensor global startTime global ccs811sensor U.toLog(-1, "==== Start " + G.program + " ===== @ i2c= " + unicode(i2cAddress)) startTime = time.time() i2cAdd = U.muxTCA9548A( sensors[sensor][devId] ) # switch mux on if requested and use the i2c address of the mix if enabled try: ccs811sensor[devId] = ccs811_class(address=i2cAdd) for ii in range(100): if ccs811sensor[devId].available(): break time.sleep(0.5) temp = ccs811sensor[devId].calculateTemperature() ccs811sensor[devId].tempOffset = temp - 25.0 time.sleep(1) try: ccs811sensor[devId].start() except: pass try: temp = ccs811sensor[devId].calculateTemperature() except: pass except Exception, e: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) ccs811sensor[devId] = ""
def readParams(): global sensorList, sensors, logDir, sensor, sensorRefreshSecs global rawOld global deltaX, INAsensor, minSendDelta global oldRaw, lastRead try: inp, inpRaw, lastRead2 = U.doRead(lastTimeStamp=lastRead) if inp == "": return if lastRead2 == lastRead: return lastRead = lastRead2 if inpRaw == oldRaw: return oldRaw = inpRaw externalSensor = False sensorList = [] sensorsOld = copy.copy(sensors) U.getGlobalParams(inp) if "sensorList" in inp: sensorList = (inp["sensorList"]) if "sensors" in inp: sensors = (inp["sensors"]) if "debugRPI" in inp: G.debug = int(inp["debugRPI"]["debugRPISENSOR"]) if sensor not in sensors: U.toLog( -1, G.program + " is not in parameters = not enabled, stopping " + G.program + ".py") exit() deltaX = {} for devId in sensors[sensor]: deltaX[devId] = 0.1 try: xx = sensors[sensor][devId]["sensorRefreshSecs"].split("#") sensorRefreshSecs = float(xx[0]) except: sensorRefreshSecs = 90 try: if "i2cAddress" in sensors[sensor][devId]: i2cAddress = int(sensors[sensor][devId]["i2cAddress"]) except: i2cAddress = "" try: if "deltaX" in sensors[sensor][devId]: deltaX[devId] = float( sensors[sensor][devId]["deltaX"]) / 100. except: deltaX[devId] = 0.1 try: if "minSendDelta" in sensors[sensor][devId]: minSendDelta = float( sensors[sensor][devId]["minSendDelta"]) / 100. except: minSendDelta = 5. try: if "minSendDelta" in sensors[sensor][devId]: minSendDelta = float( sensors[sensor][devId]["minSendDelta"]) / 100. except: minSendDelta = 5. try: shuntResistor = 0.1 if "shuntResistor" in sensors[sensor][devId]: shuntResistor = float( sensors[sensor][devId]["shuntResistor"]) except: shuntResistor = 0.1 if devId not in INAsensor: U.toLog( -1, "==== Start " + G.program + " ===== @ i2c= " + unicode(i2cAddress) + "; shuntResistor= " + unicode(shuntResistor)) i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) INAsensor[devId] = INA219(i2cAddress=i2cAdd, shuntResistor=shuntResistor) U.muxTCA9548Areset() deldevID = {} for devId in INAsensor: if devId not in sensors[sensor]: deldevID[devId] = 1 for dd in deldevID: del INAsensor[dd] if len(INAsensor) == 0: ####exit() pass except Exception, e: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def readParams(): global sensorList, sensors, logDir, sensor, sensorRefreshSecs global rawOld global deltaX, as726xsensor, minSendDelta global oldRaw, lastRead global LEDmA, doAverage, LEDBlink, gain, integrationTime try: inp,inpRaw,lastRead2 = U.doRead(lastTimeStamp=lastRead) if inp == "": return if lastRead2 == lastRead: return lastRead = lastRead2 if inpRaw == oldRaw: return oldRaw = inpRaw externalSensor=False sensorList=[] sensorsOld= copy.copy(sensors) U.getGlobalParams(inp) if "sensorList" in inp: sensorList= (inp["sensorList"]) if "sensors" in inp: sensors = (inp["sensors"]) if "debugRPI" in inp: G.debug= int(inp["debugRPI"]["debugRPISENSOR"]) if sensor not in sensors: U.toLog(-1, G.program+" is not in parameters = not enabled, stopping "+G.program+".py" ) exit() deltaX={} for devId in sensors[sensor]: deltaX[devId] = 0.1 try: xx = sensors[sensor][devId]["sensorRefreshSecs"].split("#") sensorRefreshSecs = float(xx[0]) except: sensorRefreshSecs = 100 try: if "i2cAddress" in sensors[sensor][devId]: i2cAddress = int(sensors[sensor][devId]["i2cAddress"]) except: i2cAddress = "" try: if "deltaX" in sensors[sensor][devId]: deltaX[devId]= float(sensors[sensor][devId]["deltaX"])/100. except: deltaX[devId] = 0.1 try: if "minSendDelta" in sensors[sensor][devId]: minSendDelta= float(sensors[sensor][devId]["minSendDelta"])/100. except: minSendDelta = 5. try: if "gain" in sensors[sensor][devId]: gain= float(sensors[sensor][devId]["gain"]) except: gain = 16 try: if "integrationTime" in sensors[sensor][devId]: integrationTime= float(sensors[sensor][devId]["integrationTime"]) except: integrationTime = 140 try: if "doAverage" in sensors[sensor][devId]: doAverage= int(sensors[sensor][devId]["doAverage"]) except: doAverage = 1 try: if "LEDmA" in sensors[sensor][devId]: LEDmA= float(sensors[sensor][devId]["LEDmA"]) except: LEDmA = 5. try: if "LEDBlink" in sensors[sensor][devId]: LEDBlink= int(sensors[sensor][devId]["LEDBlink"]) except: LEDBlink = 0 if devId not in as726xsensor: U.toLog(-1,"==== Start "+G.program+" ====== @ i2c= " +unicode(i2cAddress) ) i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) as726xsensor[devId] = Adafruit_AS726x(i2cAddress=i2cAdd) as726xsensor[devId].set_Conversion_mode(as726xsensor[devId].MODE_2) U.muxTCA9548Areset() i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) as726xsensor[devId].set_Integration_time(integrationTime) as726xsensor[devId].set_Gain(gain) U.muxTCA9548Areset() deldevID={} for devId in as726xsensor: if devId not in sensors[sensor]: deldevID[devId]=1 for dd in deldevID: del as726xsensor[dd] if len(as726xsensor) ==0: ####exit() pass except Exception, e: U.toLog(-1, u"readParams in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def readParams(): global sensorList, sensors, logDir, sensor, sensorRefreshSecs global rawOld global deltaX, as726xsensor, minSendDelta global oldRaw, lastRead global LEDmA, doAverage, LEDBlink, gain, integrationTime try: inp, inpRaw, lastRead2 = U.doRead(lastTimeStamp=lastRead) if inp == "": return if lastRead2 == lastRead: return lastRead = lastRead2 if inpRaw == oldRaw: return oldRaw = inpRaw externalSensor = False sensorList = [] sensorsOld = copy.copy(sensors) U.getGlobalParams(inp) if "sensorList" in inp: sensorList = (inp["sensorList"]) if "sensors" in inp: sensors = (inp["sensors"]) if "debugRPI" in inp: G.debug = int(inp["debugRPI"]["debugRPISENSOR"]) if sensor not in sensors: U.toLog( -1, G.program + " is not in parameters = not enabled, stopping " + G.program + ".py") exit() deltaX = {} for devId in sensors[sensor]: deltaX[devId] = 0.1 try: xx = sensors[sensor][devId]["sensorRefreshSecs"].split("#") sensorRefreshSecs = float(xx[0]) except: sensorRefreshSecs = 100 try: if "i2cAddress" in sensors[sensor][devId]: i2cAddress = int(sensors[sensor][devId]["i2cAddress"]) except: i2cAddress = "" try: if "deltaX" in sensors[sensor][devId]: deltaX[devId] = float( sensors[sensor][devId]["deltaX"]) / 100. except: deltaX[devId] = 0.1 try: if "minSendDelta" in sensors[sensor][devId]: minSendDelta = float( sensors[sensor][devId]["minSendDelta"]) / 100. except: minSendDelta = 5. try: if "gain" in sensors[sensor][devId]: gain = float(sensors[sensor][devId]["gain"]) except: gain = 16 try: if "integrationTime" in sensors[sensor][devId]: integrationTime = float( sensors[sensor][devId]["integrationTime"]) except: integrationTime = 140 try: if "doAverage" in sensors[sensor][devId]: doAverage = int(sensors[sensor][devId]["doAverage"]) except: doAverage = 1 try: if "LEDmA" in sensors[sensor][devId]: LEDmA = float(sensors[sensor][devId]["LEDmA"]) except: LEDmA = 5. try: if "LEDBlink" in sensors[sensor][devId]: LEDBlink = int(sensors[sensor][devId]["LEDBlink"]) except: LEDBlink = 0 if devId not in as726xsensor: U.toLog( -1, "==== Start " + G.program + " ====== @ i2c= " + unicode(i2cAddress)) i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) as726xsensor[devId] = Adafruit_AS726x(i2cAddress=i2cAdd) as726xsensor[devId].set_Conversion_mode( as726xsensor[devId].MODE_2) U.muxTCA9548Areset() i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) as726xsensor[devId].set_Integration_time(integrationTime) as726xsensor[devId].set_Gain(gain) U.muxTCA9548Areset() deldevID = {} for devId in as726xsensor: if devId not in sensors[sensor]: deldevID[devId] = 1 for dd in deldevID: del as726xsensor[dd] if len(as726xsensor) == 0: ####exit() pass except Exception, e: U.toLog( -1, u"readParams in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def readParams(): global sensorList, sensors, logDir, sensor, sensorRefreshSecs global rawOld global deltaX, SI7021sensor, minSendDelta global oldRaw, lastRead try: inp,inpRaw,lastRead2 = U.doRead(lastTimeStamp=lastRead) if inp == "": return if lastRead2 == lastRead: return lastRead = lastRead2 if inpRaw == oldRaw: return oldRaw = inpRaw externalSensor=False sensorList=[] sensorsOld= copy.copy(sensors) U.getGlobalParams(inp) if "sensorList" in inp: sensorList= (inp["sensorList"]) if "sensors" in inp: sensors = (inp["sensors"]) if "debugRPI" in inp: G.debug= int(inp["debugRPI"]["debugRPISENSOR"]) if sensor not in sensors: U.toLog(-1, G.program+" is not in parameters = not enabled, stopping "+G.program+".py" ) exit() deltaX={} for devId in sensors[sensor]: deltaX[devId] = 0.1 try: xx = sensors[sensor][devId]["sensorRefreshSecs"].split("#") sensorRefreshSecs = float(xx[0]) except: sensorRefreshSecs = 90 try: if "i2cAddress" in sensors[sensor][devId]: i2cAddress = int(sensors[sensor][devId]["i2cAddress"]) except: i2cAddress = "" try: if "deltaX" in sensors[sensor][devId]: deltaX[devId]= float(sensors[sensor][devId]["deltaX"])/100. except: deltaX[devId] = 0.05 try: if "minSendDelta" in sensors[sensor][devId]: minSendDelta= float(sensors[sensor][devId]["minSendDelta"])/100. except: minSendDelta = 5. if devId not in SI7021sensor: U.toLog(-1,"==== Start "+G.program+" ===== @ i2c= " +unicode(i2cAddress)) i2cAdd = U.muxTCA9548A(sensors[sensor][devId]) SI7021sensor[devId] = si7021(i2cAddress=i2cAdd) U.muxTCA9548Areset() deldevID={} for devId in SI7021sensor: if devId not in sensors[sensor]: deldevID[devId]=1 for dd in deldevID: del SI7021sensor[dd] if len(SI7021sensor) ==0: ####exit() pass except Exception, e: U.toLog(-1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))
def getValues(devId): global sensor, sensors, ccs811sensor, badSensor global startTime, lastTemp, lastVOC, countVOC, countCO2, lastCO2 global lastMeasurement try: ret = "" if ccs811sensor[devId] == "": badSensor += 1 return "badSensor" i2cAdd = U.muxTCA9548A( sensors[sensor][devId] ) # switch mux on if requested and use the i2c address of the mix if enabled if ccs811sensor[devId] == "": badSensor += 1 return "badSensor" goodData = False newVOC = -1 newCO2 = -1 n = 0 CO2 = 0 VOC = 0 TEMP = 0 co2 = 0 voc = 0 for kk in range(15): for ii in range(20): if ccs811sensor[devId].available(): try: temp = ccs811sensor[devId].calculateTemperature() except: temp = lastTemp if temp < -20 or temp > 50 or (lastTemp != -100 and abs(temp - lastTemp) > 10): temp = lastTemp lastTemp = temp if not ccs811sensor[devId].readData(): co2 = ccs811sensor[devId].geteCO2() voc = ccs811sensor[devId].getTVOC() ## check if bad data, if jumped too much rewuest a re-read if co2 > 8200 or co2 == 0: U.toLog(2, "data read out of bounce CO2: %d" % (co2)) time.sleep(0.5) continue if voc > 1200: U.toLog(2, "data read out of bounce VOC: %d" % (voc)) time.sleep(0.5) continue if lastCO2 != -100 and (abs(co2 - lastCO2) > 50 or co2 > 450) and countCO2 < 2: U.toLog(2, "data read jump CO2: %d" % co2) countCO2 += 1 time.sleep(0.5) continue if lastVOC != -100 and (abs(voc - lastVOC) > 100 or voc > 100) and countVOC < 2: U.toLog(2, "data read jump VOC: %d" % voc) countVOC += 1 time.sleep(0.5) continue else: if newVOC == -1: newVOC = voc newCO2 = co2 U.toLog( 2, "CO2: %d" % co2 + "ppm; TVOC: %d ppb" % voc + "; temp: %4.1f" % temp + " first ok read") time.sleep(0.5) continue if abs(newVOC - voc) > 10 or abs(newCO2 - co2) > 10: U.toLog( 2, "CO2: %d" % co2 + "ppm; TVOC: %d ppb" % voc + "; temp: %4.1f" % temp + " re-read not the same ") newVOC = voc newCO2 = co2 time.sleep(0.5) continue co2 = (co2 + newCO2) / 2 voc = (voc + newVOC) / 2 break else: U.toLog(0, "ERROR data not ready!") co2 = ccs811sensor[devId].geteCO2() voc = ccs811sensor[devId].getTVOC() U.toLog( 2, "CO2: %d" % co2 + "ppm, TVOC: %d" % voc + " temp: %4.1f" % temp) try: ccs811sensor[devId].start() except: pass time.sleep(0.5) if co2 == 0 and voc == 0: continue VOC += voc CO2 += co2 TEMP += temp n += 1 if n > 3: goodData = True VOC /= n CO2 /= n TEMP /= n break time.sleep(0.2) if goodData: U.toLog( 2, "n:" + str(n) + " CO2: %d" % CO2 + "ppm; TVOC: %d ppb" % VOC + "; temp: %4.1f" % TEMP + " accepted") ret = { "CO2": ("%d" % (CO2)).strip(), "VOC": ("%d" % (VOC)).strip(), "temp": ("%4.1f" % (TEMP)).strip() } U.toLog(3, unicode(ret)) badSensor = 0 goodData = True countCO2 = 0 lastCO2 = CO2 countVOC = 0 lastVOC = VOC lastTemp = TEMP except Exception, e: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) badSensor += 1 if badSensor > 3: ret = "badSensor" ccs811sensor[devId].start() goodData = True
def getValues(devId, nMeasurements=5): global sensor, sensors, mhz16sensor, badSensor global startTime, CO2offset, CO2normal, sensitivity try: ret = "badSensor" if mhz16sensor[devId] == "": badSensor += 1 return "badSensor" i2cAdd = U.muxTCA9548A( sensors[sensor][devId] ) # switch mux on if requested and use the i2c address of the mix if enabled if mhz16sensor[devId] == "": badSensor += 1 return "badSensor" nnn = max(2, nMeasurements) raw = 0 nMeas = 0. addIfBad = 2 ii = 0 while ii < nnn: ii += 1 mhz16sensor[devId].measure() co2 = mhz16sensor[devId].co2 U.toLog(1, " co2 raw: %d" % co2) if co2 == -1: ii -= addIfBad # onetime only addIfBad = 0 U.toLog(-1, u"bad data read ") continue raw += co2 nMeas += 1. if ii != nnn - 1: time.sleep(2) if ii % 5 == 0: U.echoLastAlive(G.program) if raw == 0: needRestart = True badSensor += 1 if badSensor > 3: ret = "badSensor" mhz16sensor[devId].start() return ret raw /= nMeas CO2 = raw + CO2offset[devId] #print "raw, CO2, CO2offset, CO2normal", raw, CO2, CO2offset[devId], CO2normal[devId] ret = { "CO2": (round(CO2, 1)), "CO2offset": (round(CO2offset[devId], 1)), "CO2calibration": (round(CO2normal[devId], 1)), "raw": (round(raw, 1)) } U.toLog(1, unicode(ret)) badSensor = 0 except Exception, e: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e)) badSensor += 1 if badSensor > 3: ret = "badSensor" mhz16sensor[devId].start()
def getValues(devId): global sensor, sensors, BMEsensor, badSensor global startTime global gasBaseLine, gasBurnIn, lastMeasurement, calibrateIfgt, setCalibration, firstValue try: if BMEsensor[devId] == "": badSensor += 1 return "badSensor" i2cAdd = U.muxTCA9548A( sensors[sensor][devId] ) # switch mux on if requested and use the i2c address of the mix if enabled if BMEsensor[devId].get_sensor_data(): temp = BMEsensor[devId].data.temperature press = BMEsensor[devId].data.pressure hum = BMEsensor[devId].data.humidity gas = BMEsensor[devId].data.gas_resistance heatSt = BMEsensor[devId].data.status & HEAT_STAB_MSK lastMeasurement = time.time() if setCalibration > 0.: startTime = 0. gasBaseLine = setCalibration if startTime > 0 and heatSt > 0 and time.time( ) - startTime > 25: # wait at least 25secs and heat ok gasBurnIn.append(float(gas)) if len(gasBurnIn) > 2: U.toLog( 2, "gasBurnIn: " + unicode(gasBurnIn) + ", delta: " + unicode( abs(gasBurnIn[-1] - gasBurnIn[-2]) / max(1, (gasBurnIn[-1] + gasBurnIn[-2])))) if len( gasBurnIn ) > 6 and abs(gasBurnIn[-1] - gasBurnIn[-2]) / max( 1, (gasBurnIn[-1] + gasBurnIn[-2]) ) < 0.0005: # wait for 13+ cycles, last measurements must be stable gasBaseLine = sum(gasBurnIn[-4:]) / ( 4.) # take last 4 measurements U.toLog( -1, " calibrated: after " + str(int(time.time() - startTime)) + " sec; nof samples:" + str(len(gasBurnIn)) + " baseline:" + str(gasBaseLine) + " heatStatus:" + str(heatSt)) startTime = 0 # save new calibration data to file f = open(G.homeDir + "bme680.Calibration", "w") f.write(json.dumps({"resistance": gasBaseLine})) f.close() if startTime == 0: if gas > gasBaseLine * ( calibrateIfgt / 100. ) and not firstValue and setCalibration == 0: #recalibrate over x % higher than previous calibartion (=100% clean air) U.toLog( -1, "re calibrated: due to shift high in baseline new value:" + str(gas) + "; oldbaseLine: " + str(gasBaseLine)) gasScore = "calibrating" startSensor(devId, int(sensors[sensor][devId]["i2cAddress"])) else: gasScore = ("%3d" % (int(gas / gasBaseLine * 100))).strip() else: gasScore = "calibrating" temp += float(sensors[sensor][devId]["offsetTemp"]) press += float(sensors[sensor][devId]["offsetPress"]) hum += float(sensors[sensor][devId]["offsetHum"]) gas += float(sensors[sensor][devId]["offsetGas"]) data = { "temp": ("%.2f" % (temp)).strip(), "press": ("%7d" % (press)).strip(), "hum": ("%7d" % (hum)).strip(), "GasResistance": ("%7d" % (gas)).strip(), "GasBaseline": ("%7d" % (gasBaseLine)).strip(), "AirQuality": gasScore } U.toLog(2, unicode(data)) badSensor = 0 return data except Exception, e: U.toLog( -1, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))