From 5a5dd139c3e02ead0ac00a47a1e9dd436bb989c9 Mon Sep 17 00:00:00 2001 From: Sandy Macdonald Date: Sun, 9 Jun 2019 13:11:35 +0100 Subject: [PATCH] Tidying up and adding examples --- examples/compensated-temperature.py | 59 +++++++++++++++++++ examples/indoor-air-quality.py | 5 +- examples/read-all.py | 5 +- .../{temp-offset.py => temperature-offset.py} | 8 ++- ...um.py => temperature-pressure-humidity.py} | 5 +- 5 files changed, 72 insertions(+), 10 deletions(-) create mode 100755 examples/compensated-temperature.py rename examples/{temp-offset.py => temperature-offset.py} (89%) rename examples/{temp-press-hum.py => temperature-pressure-humidity.py} (90%) diff --git a/examples/compensated-temperature.py b/examples/compensated-temperature.py new file mode 100755 index 0000000..d3901ca --- /dev/null +++ b/examples/compensated-temperature.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +import time +import bme680 +from subprocess import PIPE, Popen + +try: + from smbus2 import SMBus +except ImportError: + from smbus import SMBus + +print("""compensated-temperature.py - Use the CPU temperature to compensate temperature +readings from the BME680 sensor. Method adapted from Initial State's Enviro pHAT +review: https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441 + +Press Ctrl+C to exit! + +""") + +try: + sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY) +except IOError: + sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY) + +# These oversampling settings can be tweaked to +# change the balance between accuracy and noise in +# the data. + +sensor.set_humidity_oversample(bme680.OS_2X) +sensor.set_pressure_oversample(bme680.OS_4X) +sensor.set_temperature_oversample(bme680.OS_8X) +sensor.set_filter(bme680.FILTER_SIZE_3) + +# Gets the CPU temperature in degrees C +def get_cpu_temperature(): + process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) + output, _error = process.communicate() + return float(output[output.index('=') + 1:output.rindex("'")]) + +factor = 1.0 # Smaller numbers adjust temp down, vice versa +smooth_size = 10 # Dampens jitter due to rapid CPU temp changes + +cpu_temps = [] + +while True: + if sensor.get_sensor_data(): + cpu_temp = get_cpu_temperature() + cpu_temps.append(cpu_temp) + + if len(cpu_temps) > smooth_size: + cpu_temps = cpu_temps[1:] + + smoothed_cpu_temp = sum(cpu_temps) / float(len(cpu_temps)) + raw_temp = sensor.data.temperature + comp_temp = raw_temp - ((smoothed_cpu_temp - raw_temp) / factor) + + print("Compensated temperature: {:05.2f} *C".format(comp_temp)) + + time.sleep(1.0) diff --git a/examples/indoor-air-quality.py b/examples/indoor-air-quality.py index 0a60643..a762e49 100755 --- a/examples/indoor-air-quality.py +++ b/examples/indoor-air-quality.py @@ -1,14 +1,15 @@ #!/usr/bin/env python + import bme680 import time -print("""Estimate indoor air quality +print("""indoor-air-quality.py - Estimates indoor air quality. Runs the sensor for a burn-in period, then uses a combination of relative humidity and gas resistance to estimate indoor air quality as a percentage. -Press Ctrl+C to exit +Press Ctrl+C to exit! """) diff --git a/examples/read-all.py b/examples/read-all.py index 1c23ed0..b0d31d2 100755 --- a/examples/read-all.py +++ b/examples/read-all.py @@ -1,10 +1,11 @@ #!/usr/bin/env python + import bme680 import time -print("""Display Temperature, Pressure, Humidity and Gas +print("""read-all.py - Displays temperature, pressure, humidity, and gas. -Press Ctrl+C to exit +Press Ctrl+C to exit! """) diff --git a/examples/temp-offset.py b/examples/temperature-offset.py similarity index 89% rename from examples/temp-offset.py rename to examples/temperature-offset.py index 2ccd73d..7f298cd 100755 --- a/examples/temp-offset.py +++ b/examples/temperature-offset.py @@ -1,7 +1,11 @@ #!/usr/bin/env python + import bme680 -print("""Display Temperature, Pressure and Humidity with different offsets. +print("""temperature-offset.py - Displays temperature, pressure, and humidity with different offsets. + +Press Ctrl+C to exit! + """) try: @@ -18,7 +22,6 @@ sensor.set_pressure_oversample(bme680.OS_4X) sensor.set_temperature_oversample(bme680.OS_8X) sensor.set_filter(bme680.FILTER_SIZE_3) - def display_data(offset=0): sensor.set_temp_offset(offset) sensor.get_sensor_data() @@ -29,7 +32,6 @@ def display_data(offset=0): print(output) print('') - print('Initial readings') display_data() diff --git a/examples/temp-press-hum.py b/examples/temperature-pressure-humidity.py similarity index 90% rename from examples/temp-press-hum.py rename to examples/temperature-pressure-humidity.py index 8a2702d..ae3321e 100755 --- a/examples/temp-press-hum.py +++ b/examples/temperature-pressure-humidity.py @@ -1,7 +1,8 @@ #!/usr/bin/env python + import bme680 -print("""Display Temperature, Pressure and Humidity +print("""temperature-pressure-humidity.py - Displays temperature, pressure, and humidity. If you don't need gas readings, then you can read temperature, pressure and humidity quickly. @@ -28,12 +29,10 @@ print('Polling:') try: while True: if sensor.get_sensor_data(): - output = '{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH'.format( sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) - print(output) except KeyboardInterrupt: