def measurement(): try: # load settings settings = get_settings() # read configured sensors from settings.json ds18b20Sensors = get_sensors(settings, 0) bme680Sensors = get_sensors(settings, 1) weightSensors = get_sensors(settings, 2) dhtSensors = get_sensors(settings, 3) # if bme680 is configured if bme680Sensors and len(bme680Sensors) == 1: gas_baseline = None if initBME680FromMain(): # bme680 sensor must be burned in before use gas_baseline = burn_in_bme680(10) # if burning was canceled => exit if gas_baseline is None: print("gas_baseline can't be None") # dict with all fields and values which will be tranfered to ThingSpeak later ts_fields = {} # measure every sensor with type 0 for (sensorIndex, sensor) in enumerate(ds18b20Sensors): if 'ts_field' in sensor and 'device_id' in sensor: ts_field_ds18b20 = sensor["ts_field"] ds18b20_temperature = measure_temperature(sensor["device_id"]) ts_fields.update({ts_field_ds18b20: ds18b20_temperature}) else: print("DS18b20 missing param: ts_field or device_id") # measure BME680 (can only be once) [type 1] if bme680Sensors and len(bme680Sensors) == 1 and gas_baseline: bme680_values = measure_bme680(gas_baseline, bme680Sensors[0]) ts_fields.update(bme680_values) # disable warnings for HX711 GPIO.setwarnings(False) # measure every sensor with type 2 [HX711] for (i, sensor) in enumerate(weightSensors): weight = measure_weight(sensor) ts_fields.update(weight) # measure every sensor with type 3 [DHT11/DHT22] for (i, sensor) in enumerate(dhtSensors): tempAndHum = measure_dht(sensor) ts_fields.update(tempAndHum) return json.dumps(ts_fields) except Exception as e: print("Measurement: " + str(e))
def measurement(): try: # load settings settings = get_settings() # read configured sensors from settings.json ds18b20Sensors = get_sensors(settings, 0) bme680Sensors = get_sensors(settings, 1) weightSensors = get_sensors(settings, 2) dhtSensors = get_sensors(settings, 3) tcSensors = get_sensors(settings, 4) bme280Sensors = get_sensors(settings, 5) # if bme680 is configured if bme680Sensors and len(bme680Sensors) == 1: bme680IsInitialized = initBME680FromMain(bme680Sensors) else: bme680IsInitialized = 0 # dict with all fields and values which will be tranfered to ThingSpeak later ts_fields = {} # measure every sensor with type 0 for (sensorIndex, sensor) in enumerate(ds18b20Sensors): if 'ts_field' in sensor and 'device_id' in sensor: ts_field_ds18b20 = sensor["ts_field"] ds18b20_temperature = measure_temperature(sensor["device_id"]) ts_fields.update({ts_field_ds18b20: ds18b20_temperature}) else: print("DS18b20 missing param: ts_field or device_id") # measure BME680 (can only be one) [type 1] if bme680Sensors and len(bme680Sensors) == 1 and bme680IsInitialized: bme680_values = measure_bme680(bme680Sensors[0], 10) ts_fields.update(bme680_values) # measure every sensor with type 3 [DHT11/DHT22] for (i, sensor) in enumerate(dhtSensors): tempAndHum = measure_dht(sensor) ts_fields.update(tempAndHum) # measure every sensor with type 4 [MAX6675] for (i, sensor) in enumerate(tcSensors): tc_temp = measure_tc(sensor) ts_fields.update(tc_temp) # measure BME280 (can only be one) [type 5] if bme280Sensors and len(bme280Sensors) == 1: bme280_values = measure_bme280(bme280Sensors[0]) ts_fields.update(bme280_values) start_single() # measure every sensor with type 2 [HX711] for (i, sensor) in enumerate(weightSensors): weight = measure_weight(sensor) weight = compensate_temperature(sensor, weight, ts_fields) ts_fields.update(weight) stop_single() return json.dumps(ts_fields) except Exception as e: print("Measurement: " + str(e)) # Error occured return {}
def measure_all_sensors(debug, filtered_temperature, ds18b20Sensors, bme680Sensors, bme680IsInitialized, dhtSensors, aht10Sensors, sht31Sensors, hdc1008Sensors, tcSensors, bme280Sensors, voltageSensors, ee895Sensors, weightSensors, hxInits): ts_fields = { } # dict with all fields and values which will be tranfered to ThingSpeak later try: # measure every sensor with type 0 (Ds18b20) try: for (sensorIndex, sensor) in enumerate(ds18b20Sensors): filter_temperatur_values(sensorIndex) except Exception as e: error_log( e, "Unhandled Exception in measure_all_sensors / ds18b20Sensors filter_temperatur_values" ) try: for (sensorIndex, sensor) in enumerate(ds18b20Sensors): if filtered_temperature is not None and len( filtered_temperature[sensorIndex] ) > 0 and 'ts_field' in sensor: # if we have at leat one filtered value we can upload ds18b20_temperature = filtered_temperature[ sensorIndex].pop() if sensor["ts_field"] and ds18b20_temperature is not None: if 'offset' in sensor and sensor["offset"] is not None: ds18b20_temperature = ds18b20_temperature - float( sensor["offset"]) ds18b20_temperature = float("{0:.2f}".format( ds18b20_temperature)) # round to two decimals ts_fields.update( {sensor["ts_field"]: ds18b20_temperature}) elif 'ts_field' in sensor: # Case for filtered_temperature was not filled, use direct measured temperture in this case ds18b20_temperature = measure_temperature(sensor) if sensor["ts_field"] and ds18b20_temperature is not None: if 'offset' in sensor and sensor["offset"] is not None: ds18b20_temperature = ds18b20_temperature - float( sensor["offset"]) ds18b20_temperature = float("{0:.2f}".format( ds18b20_temperature)) # round to two decimals ts_fields.update( {sensor["ts_field"]: ds18b20_temperature}) except Exception as e: error_log( e, "Unhandled Exception in measure_all_sensors / ds18b20Sensors") # measure BME680 (can only be one) [type 1] for (sensorIndex, bme680Sensor) in enumerate(bme680Sensors): if bme680IsInitialized[sensorIndex]: bme680_values = measure_bme680(bme680Sensor, 30) ts_fields.update(bme680_values) # measure every sensor with type 3 [DHT11/DHT22] for (i, sensor) in enumerate(dhtSensors): tempAndHum = measure_dht(sensor) ts_fields.update(tempAndHum) # measure every sensor with type 4 [MAX6675] for (i, sensor) in enumerate(tcSensors): tc_temp = measure_tc(sensor) ts_fields.update(tc_temp) # measure BME280 (can only be one) [type 5] if bme280Sensors and len(bme280Sensors) == 1: bme280_values = measure_bme280(bme280Sensors[0]) ts_fields.update(bme280_values) # measure YL-40 PFC8591 (can only be one) [type 6] if voltageSensors and len(voltageSensors) == 1: voltage = measure_voltage(voltageSensors[0]) if voltage is not None: ts_fields.update(voltage) # measure EE895 (can only be one) [type 7] if ee895Sensors and len(ee895Sensors) == 1: ee895_values = measure_ee895(ee895Sensors[0]) if ee895_values is not None: ts_fields.update(ee895_values) # measure AHT10 (can only be one) [type 8] if aht10Sensors and len(aht10Sensors) == 1: aht10_fields = measure_aht10(aht10Sensors[0]) if aht10_fields is not None: ts_fields.update(aht10_fields) # measure sht31 (can only be one) [type 9] if sht31Sensors and len(sht31Sensors) == 1: sht31_fields = measure_sht31(sht31Sensors[0]) if sht31_fields is not None: ts_fields.update(sht31_fields) # measure hdc1008 (can only be one) [type 10] if hdc1008Sensors and len(hdc1008Sensors) == 1: hdc1008_fields = measure_hdc1008(hdc1008Sensors[0]) if hdc1008_fields is not None: ts_fields.update(hdc1008_fields) # measure every sensor with type 2 [HX711] start_single() for (i, sensor) in enumerate(weightSensors): if hxInits is not None: weight = measure_weight(sensor, hxInits[i], debug) weight = compensate_temperature(sensor, weight, ts_fields) ts_fields.update(weight) else: weight = measure_weight(sensor) weight = compensate_temperature(sensor, weight, ts_fields) ts_fields.update(weight) stop_single() # print all measurement values stored in ts_fields if debug: for key, value in ts_fields.items(): print(key + ": " + str(value)) return ts_fields except Exception as ex: error_log(ex, "Exception during measurement 1") return ts_fields
def measure_all_sensors(debug, filtered_temperature, ds18b20Sensors, bme680Sensors, bme680Inits, dhtSensors, aht10Sensors, sht31Sensors, hdc1008Sensors, tcSensors, bme280Sensors, pcf8591Sensors, ee895Sensors, weightSensors, hxInits): ts_fields = { } # dict with all fields and values which will be tranfered to ThingSpeak later global burn_in_time try: logger.debug("Measurement for all configured sensors started...") try: # measure every sensor with type 0 (Ds18b20) for (sensorIndex, sensor) in enumerate(ds18b20Sensors): filter_temperatur_values(sensorIndex) except Exception as ex: logger.exception( "Unhandled Exception in measure_all_sensors / ds18b20Sensors filter_temperatur_values" ) try: for (sensorIndex, sensor) in enumerate(ds18b20Sensors): if filtered_temperature is not None and len( filtered_temperature[sensorIndex] ) > 0 and 'ts_field' in sensor: # if we have at leat one filtered value we can upload ds18b20_temperature = filtered_temperature[ sensorIndex].pop() if sensor["ts_field"] and ds18b20_temperature is not None: if 'offset' in sensor and sensor["offset"] is not None: ds18b20_temperature = ds18b20_temperature - float( sensor["offset"]) ds18b20_temperature = float("{0:.2f}".format( ds18b20_temperature)) # round to two decimals ts_fields.update( {sensor["ts_field"]: ds18b20_temperature}) elif 'ts_field' in sensor: # Case for filtered_temperature was not filled, use direct measured temperture in this case ds18b20_temperature = measure_temperature(sensor) if sensor["ts_field"] and ds18b20_temperature is not None: if 'offset' in sensor and sensor["offset"] is not None: ds18b20_temperature = ds18b20_temperature - float( sensor["offset"]) ds18b20_temperature = float("{0:.2f}".format( ds18b20_temperature)) # round to two decimals ts_fields.update( {sensor["ts_field"]: ds18b20_temperature}) except Exception as ex: logger.exception( "Unhandled Exception in measure_all_sensors / ds18b20Sensors") # measure BME680 (can only be two) [type 1] for (sensorIndex, bme680Sensor) in enumerate(bme680Sensors): if bme680Inits[sensorIndex] != None: bme680Init = bme680Inits[sensorIndex] sensor = bme680Init['sensor'] gas_baseline = bme680Init['gas_baseline'] bme680_values, gas_baseline = measure_bme680( sensor, gas_baseline, bme680Sensor, burn_in_time) ts_fields.update(bme680_values) bme680Init['gas_baseline'] = gas_baseline bme680Inits[sensorIndex] = bme680Init # measure every sensor with type 3 [DHT11/DHT22] for (i, sensor) in enumerate(dhtSensors): tempAndHum = measure_dht(sensor) ts_fields.update(tempAndHum) # measure every sensor with type 4 [MAX6675] for (i, sensor) in enumerate(tcSensors): tc_temp = measure_tc(sensor) ts_fields.update(tc_temp) # measure BME280 (can only be two) [type 5] for (sensorIndex, bme280Sensor) in enumerate(bme280Sensors): bme280_values = measure_bme280(bme280Sensor) if bme280_values is not None: ts_fields.update(bme280_values) # measure PCF8591 [type 6] for (i, sensor) in enumerate(pcf8591Sensors): pcf8591_values = measure_pcf8591(sensor) if pcf8591_values is not None: ts_fields.update(pcf8591_values) # measure EE895 (can only be one) [type 7] if ee895Sensors and len(ee895Sensors) == 1: ee895_values = measure_ee895(ee895Sensors[0]) if ee895_values is not None: ts_fields.update(ee895_values) # measure AHT10 (can only be one) [type 8] if aht10Sensors and len(aht10Sensors) == 1: aht10_fields = measure_aht10(aht10Sensors[0]) if aht10_fields is not None: ts_fields.update(aht10_fields) # measure sht31 (can only be one) [type 9] if sht31Sensors and len(sht31Sensors) == 1: sht31_fields = measure_sht31(sht31Sensors[0]) if sht31_fields is not None: ts_fields.update(sht31_fields) # measure hdc1008 (can only be one) [type 10] if hdc1008Sensors and len(hdc1008Sensors) == 1: hdc1008_fields = measure_hdc1008(hdc1008Sensors[0]) if hdc1008_fields is not None: ts_fields.update(hdc1008_fields) # measure every sensor with type 2 [HX711] start_single() for (i, sensor) in enumerate(weightSensors): if hxInits is not None: weight = measure_weight(sensor, hxInits[i]) weight = compensate_temperature(sensor, weight, ts_fields) ts_fields.update(weight) else: weight = measure_weight(sensor) weight = compensate_temperature(sensor, weight, ts_fields) ts_fields.update(weight) stop_single() # print all measurement values stored in ts_fields logger.debug("Measurement for all configured sensors finished...") if debug: ts_fields_content = "" for key, value in ts_fields.items(): ts_fields_content = ts_fields_content + key + ": " + str( value) + " " logger.debug(ts_fields_content) return ts_fields, bme680Inits except Exception as ex: logger.exception("Unhandled Exception in measure_all_sensors") return ts_fields, bme680Inits