Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e827e5d
commit 5a5dd13
Showing
5 changed files
with
72 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters