]>
zdv2.bktei.com Git - EVA-2020-02-2.git/blob - examples/compensated-temperature.py
b572ad5127eddd247a858cfb805db4d996cae076
   4 from bme280 
import BME280
 
   5 from subprocess 
import PIPE
, Popen
 
   8     from smbus2 
import SMBus
 
  10     from smbus 
import SMBus
 
  12 print("""compensated-temperature.py - Use the CPU temperature to compensate temperature 
  13 readings from the BME280 sensor. Method adapted from Initial State's Enviro pHAT 
  14 review: https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441 
  21 bme280 
= BME280(i2c_dev
=bus
) 
  23 # Get the temperature of the CPU for compensation 
  24 def get_cpu_temperature(): 
  25     process 
= Popen(['vcgencmd', 'measure_temp'], stdout
=PIPE
) 
  26     output
, _error 
= process
.communicate() 
  27     return float(output
[output
.index('=') + 1:output
.rindex("'")]) 
  29 # Tuning factor for compensation. Decrease this number to adjust the 
  30 # temperature down, and increase to adjust up 
  36     cpu_temp 
= get_cpu_temperature() 
  37     # Smooth out with some averaging to decrease jitter 
  38     cpu_temps 
= cpu_temps
[1:] + [cpu_temp
] 
  39     avg_cpu_temp 
= sum(cpu_temps
) / float(len(cpu_temps
)) 
  40     raw_temp 
= bme280
.get_temperature() 
  41     comp_temp 
= raw_temp 
- ((avg_cpu_temp 
- raw_temp
) / factor
) 
  42     print("Compensated temperature: {:05.2f} *C".format(comp_temp
))