|
10 | 10 | from bme280 import BME280 |
11 | 11 | from pms5003 import PMS5003 |
12 | 12 | from enviroplus import gas |
| 13 | +from subprocess import PIPE, Popen |
13 | 14 | from PIL import Image |
14 | 15 | from PIL import ImageDraw |
15 | 16 | from PIL import ImageFont |
@@ -76,6 +77,18 @@ def display_text(variable, data, unit): |
76 | 77 | draw.text((0, 0), message, font=font, fill=(0, 0, 0)) |
77 | 78 | st7735.display(img) |
78 | 79 |
|
| 80 | +# Get the temperature of the CPU for compensation |
| 81 | +def get_cpu_temperature(): |
| 82 | + process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) |
| 83 | + output, _error = process.communicate() |
| 84 | + return float(output[output.index('=') + 1:output.rindex("'")]) |
| 85 | + |
| 86 | +# Tuning factor for compensation. Decrease this number to adjust the |
| 87 | +# temperature down, and increase to adjust up |
| 88 | +factor = 0.8 |
| 89 | + |
| 90 | +cpu_temps = [0] * 5 |
| 91 | + |
79 | 92 | delay = 0.5 # Debounce the proximity tap |
80 | 93 | mode = 0 # The starting mode |
81 | 94 | last_page = 0 |
@@ -113,7 +126,12 @@ def display_text(variable, data, unit): |
113 | 126 | if mode == 0: |
114 | 127 | variable = "temperature" |
115 | 128 | unit = "C" |
116 | | - data = bme280.get_temperature() |
| 129 | + cpu_temp = get_cpu_temperature() |
| 130 | + # Smooth out with some averaging to decrease jitter |
| 131 | + cpu_temps = cpu_temps[1:] + [cpu_temp] |
| 132 | + avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps)) |
| 133 | + raw_temp = bme280.get_temperature() |
| 134 | + data = raw_temp - ((avg_cpu_temp - raw_temp) / factor) |
117 | 135 | display_text(variable, data, unit) |
118 | 136 |
|
119 | 137 | if mode == 1: |
|
0 commit comments