Skip to content

Commit

Permalink
Tidying up and adding examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sandyjmacdonald committed Jun 9, 2019
1 parent e827e5d commit 5a5dd13
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
59 changes: 59 additions & 0 deletions 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)
5 changes: 3 additions & 2 deletions 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!
""")

Expand Down
5 changes: 3 additions & 2 deletions 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!
""")

Expand Down
8 changes: 5 additions & 3 deletions examples/temp-offset.py → 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:
Expand All @@ -18,7 +22,6 @@
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()
Expand All @@ -29,7 +32,6 @@ def display_data(offset=0):
print(output)
print('')


print('Initial readings')
display_data()

Expand Down
@@ -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.
Expand All @@ -28,12 +29,10 @@
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:
Expand Down

0 comments on commit 5a5dd13

Please sign in to comment.