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(offline, debug, channel, filtered_temperature, ds18b20Sensors, bme680Sensors, bme680IsInitialized, dhtSensors, tcSensors, bme280Sensors, weightSensors, hxInits, connectionErrors, measurementIsRunning): measurementIsRunning.value = 1 # set flag # filter the values out for (sensorIndex, sensor) in enumerate(ds18b20Sensors): filter_temperatur_values(sensorIndex) # 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 we have at leat one filtered value we can upload if len(filtered_temperature[sensorIndex]) > 0 and 'ts_field' in sensor: ds18b20_temperature = filtered_temperature[sensorIndex].pop( ) # get last value from array ts_field_ds18b20 = sensor["ts_field"] if ts_field_ds18b20: ts_fields.update({ts_field_ds18b20: ds18b20_temperature}) # measure BME680 (can only be one) [type 1] if bme680Sensors and len(bme680Sensors) == 1 and bme680IsInitialized: bme680_values = measure_bme680(bme680Sensors[0], 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) start_single() # measure every sensor with type 2 [HX711] for (i, sensor) in enumerate(weightSensors): weight = measure_weight(sensor, hxInits[i]) weight = compensate_temperature(sensor, weight, ts_fields) ts_fields.update(weight) stop_single() # print all measurement values stored in ts_fields for key, value in ts_fields.items(): print(key + ": " + str(value)) if len(ts_fields) > 0: if offline == 1 or offline == 3: try: s = write_csv(ts_fields) if s and debug: error_log("Info: Data succesfully saved to CSV-File.") except Exception as ex: error_log(ex, "Exception") # if transfer to thingspeak is set if offline == 0 or offline == 1 or offline == 2: # do-while to retry failed transfer retries = 0 connectionError = True while True: try: # update ThingSpeak / transfer values channel.update(ts_fields) if debug: error_log( "Info: Data succesfully transfered to ThingSpeak.") if connectionErrors.value > 0: if debug: error_log("Info: Connection Errors (" + str(connectionErrors.value) + ") Counting resetet.") # reset connectionErrors because transfer succeded connectionErrors.value = 0 # break do-while because transfer succeded connectionError = False break except requests.exceptions.HTTPError as errh: error_log(errh, "Http Error") except requests.exceptions.ConnectionError as errc: pass except requests.exceptions.Timeout as errt: error_log(errt, "Timeout Error") except requests.exceptions.RequestException as err: error_log(err, "Something Else") except Exception as ex: error_log(ex, "Exception while sending Data") finally: if connectionError: print( "Warning: Waiting for internet connection to try transfer again..." ) wait_for_internet_connection( 15) # wait before next try retries += 1 # Maximum 3 retries if retries >= 3: break # break do-while if connectionError: # Write to CSV-File if ConnectionError occured if offline == 2: s = write_csv(ts_fields) if s and debug: error_log("Info: Data succesfully saved to CSV-File.") # Do Rebooting if to many connectionErrors in a row connectionErrors.value += 1 error_log("Error: Failed internet connection. Count: " + str(connectionErrors.value)) if connectionErrors.value >= 5: if not debug: error_log( "Info: Too many ConnectionErrors in a row => Rebooting" ) time.sleep(4) reboot() else: error_log( "Info: Did not reboot because debug mode is enabled." ) elif debug: error_log("Info: No measurement data to send.") measurementIsRunning.value = 0 # clear flag
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
def start_measurement(measurement_stop): try: print("The measurements have started.") start_time = time.time() # load settings settings = get_settings() # ThingSpeak data channel_id = settings["ts_channel_id"] write_key = settings["ts_write_key"] interval = settings["interval"] if interval and not isinstance( interval, int) or interval == 0 or not channel_id or not write_key: print("ThingSpeak settings are not complete or interval is 0") measurement_stop.set() # 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(30) # if burning was canceled => exit if gas_baseline is None: print("gas_baseline can't be None") # ThingSpeak channel channel = thingspeak.Channel(id=channel_id, write_key=write_key) # counting connection Errors connectionErros = 0 # start at -6 because we want to get 6 values before we can filter some out counter = -6 while not measurement_stop.is_set(): # read values from sensors every second for (sensorIndex, sensor) in enumerate(ds18b20Sensors): checkIfSensorExistsInArray(sensorIndex) if 'device_id' in sensor: read_unfiltered_temperatur_values(sensorIndex, sensor['device_id']) # for testing: #try: # weight = measure_weight(weightSensors[0]) # print("weight: " + str(list(weight.values())[0])) #except IOError: # print "IOError occurred" #except TypeError: # print "TypeError occurred" #except IndexError: # print "IndexError occurred" # wait seconds of interval before next check # free ThingSpeak account has an upload limit of 15 seconds if counter % interval == 0: print("Time over for a new measurement.") # filter the values out for (sensorIndex, sensor) in enumerate(ds18b20Sensors): filter_temperatur_values(sensorIndex) # 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 we have at leat one filtered value we can upload if len(filtered_temperature[sensorIndex] ) > 0 and 'ts_field' in sensor: ds18b20_temperature = filtered_temperature[ sensorIndex].pop() # get last value from array ts_field_ds18b20 = sensor["ts_field"] if ts_field_ds18b20: ts_fields.update( {ts_field_ds18b20: ds18b20_temperature}) # 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) # 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) # print measurement values for debug reasons for key, value in ts_fields.iteritems(): print key + ": " + str(value) try: # update ThingSpeak / transfer values if len(ts_fields) > 0: channel.update(ts_fields) # reset connectionErros because transfer succeded connectionErros = 0 except requests.exceptions.HTTPError as errh: error_log(errh, "Http Error") except requests.exceptions.ConnectionError as errc: error_log(errc, "Error Connecting") connectionErros += 1 # multiple connectionErrors in a row => Exception if connectionErros > 4: raise MyRebootException except requests.exceptions.Timeout as errt: error_log(errt, "Timeout Error") except requests.exceptions.RequestException as err: error_log(err, "Something Else") counter += 1 sleep(0.96) end_time = time.time() time_taken = end_time - start_time # time_taken is in seconds time_taken_s = float( "{0:.2f}".format(time_taken)) # remove microseconds print("Measurement-Script runtime was " + str(time_taken_s) + " seconds.") except MyRebootException as re: error_log(re, "Too many ConnectionErrors in a row => Rebooting") time.sleep(1) reboot() except Exception as e: error_log(e, "Unhandled Exception while Measurement") time.sleep(60) reboot()
def start_read_and_upload_all(measurement_stop): print("Messungen beginnen") # reset event flag in case thread is started again measurement_stop.clear() event.clear() # bme680 sensor must be burned in before use gas_baseline = burn_in_bme680() # if burning was canceled=> exit if gas_baseline is None: print "gas_baseline can't be None" stop_read_and_upload_all() # here we start the thread # we use a thread in order to gather/process the data separately from the printing process data_collector = threading.Thread(target=read_values) data_collector.start() # ThingSpeak channel channel = thingspeak.Channel(id=channel_id, write_key=write_key) counter = 0 while not event.is_set() and not measurement_stop.is_set(): # wait seconds of interval before next check # free ThingSpeak account has an upload limit of 15 seconds if counter%interval == 0: if len(filtered_temperature) > 0: # or we could have used filtered_humidity instead lock.acquire() # dict with all fields and values with will be tranfered later to ThingSpeak ts_fields = {} # here you can do whatever you want with the variables: print them, file them out, anything # measure sensor with type 0 ds18b20_temperature = filtered_temperature.pop() ts_field_ds18b20 = get_sensors(0)[0]["ts_field"] ts_fields.update({ts_field_ds18b20: ds18b20_temperature}) # measure BME680 (can only be once) bme680_values = measure_bme680(gas_baseline, get_sensors(1)[0]) ts_fields.update(bme680_values) # measure every sensor with type 2 for (i, sensor) in enumerate(get_sensors(2)): weight = measure_weight(sensor) ts_fields.update(weight) # print measurement values for debug reasons for key, value in ts_fields.iteritems(): print key + ": " + str(value) try: # update ThingSpeak / transfer values channel.update(ts_fields) except HTTPError: print "HTTPError occurred" lock.release() counter++ sleep(1) # wait until the thread is finished data_collector.join()