Skip to content

Commit 91434ca

Browse files
Merge pull request #24 from pimoroni/examples-tidyup
Tidying up and adding examples
2 parents e827e5d + 5a5dd13 commit 91434ca

5 files changed

Lines changed: 72 additions & 10 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
3+
import time
4+
import bme680
5+
from subprocess import PIPE, Popen
6+
7+
try:
8+
from smbus2 import SMBus
9+
except ImportError:
10+
from smbus import SMBus
11+
12+
print("""compensated-temperature.py - Use the CPU temperature to compensate temperature
13+
readings from the BME680 sensor. Method adapted from Initial State's Enviro pHAT
14+
review: https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441
15+
16+
Press Ctrl+C to exit!
17+
18+
""")
19+
20+
try:
21+
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
22+
except IOError:
23+
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
24+
25+
# These oversampling settings can be tweaked to
26+
# change the balance between accuracy and noise in
27+
# the data.
28+
29+
sensor.set_humidity_oversample(bme680.OS_2X)
30+
sensor.set_pressure_oversample(bme680.OS_4X)
31+
sensor.set_temperature_oversample(bme680.OS_8X)
32+
sensor.set_filter(bme680.FILTER_SIZE_3)
33+
34+
# Gets the CPU temperature in degrees C
35+
def get_cpu_temperature():
36+
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
37+
output, _error = process.communicate()
38+
return float(output[output.index('=') + 1:output.rindex("'")])
39+
40+
factor = 1.0 # Smaller numbers adjust temp down, vice versa
41+
smooth_size = 10 # Dampens jitter due to rapid CPU temp changes
42+
43+
cpu_temps = []
44+
45+
while True:
46+
if sensor.get_sensor_data():
47+
cpu_temp = get_cpu_temperature()
48+
cpu_temps.append(cpu_temp)
49+
50+
if len(cpu_temps) > smooth_size:
51+
cpu_temps = cpu_temps[1:]
52+
53+
smoothed_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
54+
raw_temp = sensor.data.temperature
55+
comp_temp = raw_temp - ((smoothed_cpu_temp - raw_temp) / factor)
56+
57+
print("Compensated temperature: {:05.2f} *C".format(comp_temp))
58+
59+
time.sleep(1.0)

examples/indoor-air-quality.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env python
2+
23
import bme680
34
import time
45

5-
print("""Estimate indoor air quality
6+
print("""indoor-air-quality.py - Estimates indoor air quality.
67
78
Runs the sensor for a burn-in period, then uses a
89
combination of relative humidity and gas resistance
910
to estimate indoor air quality as a percentage.
1011
11-
Press Ctrl+C to exit
12+
Press Ctrl+C to exit!
1213
1314
""")
1415

examples/read-all.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env python
2+
23
import bme680
34
import time
45

5-
print("""Display Temperature, Pressure, Humidity and Gas
6+
print("""read-all.py - Displays temperature, pressure, humidity, and gas.
67
7-
Press Ctrl+C to exit
8+
Press Ctrl+C to exit!
89
910
""")
1011

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/usr/bin/env python
2+
23
import bme680
34

4-
print("""Display Temperature, Pressure and Humidity with different offsets.
5+
print("""temperature-offset.py - Displays temperature, pressure, and humidity with different offsets.
6+
7+
Press Ctrl+C to exit!
8+
59
""")
610

711
try:
@@ -18,7 +22,6 @@
1822
sensor.set_temperature_oversample(bme680.OS_8X)
1923
sensor.set_filter(bme680.FILTER_SIZE_3)
2024

21-
2225
def display_data(offset=0):
2326
sensor.set_temp_offset(offset)
2427
sensor.get_sensor_data()
@@ -29,7 +32,6 @@ def display_data(offset=0):
2932
print(output)
3033
print('')
3134

32-
3335
print('Initial readings')
3436
display_data()
3537

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
2+
23
import bme680
34

4-
print("""Display Temperature, Pressure and Humidity
5+
print("""temperature-pressure-humidity.py - Displays temperature, pressure, and humidity.
56
67
If you don't need gas readings, then you can read temperature,
78
pressure and humidity quickly.
@@ -28,12 +29,10 @@
2829
try:
2930
while True:
3031
if sensor.get_sensor_data():
31-
3232
output = '{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH'.format(
3333
sensor.data.temperature,
3434
sensor.data.pressure,
3535
sensor.data.humidity)
36-
3736
print(output)
3837

3938
except KeyboardInterrupt:

0 commit comments

Comments
 (0)