Tidying up and adding examples

This commit is contained in:
Sandy Macdonald 2019-06-09 13:11:35 +01:00
parent e827e5d622
commit 5a5dd139c3
5 changed files with 72 additions and 10 deletions

View File

@ -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)

View File

@ -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!
""")

View File

@ -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!
""")

View File

@ -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()

View File

@ -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: