Enviro+のセットアップ

Enviro+のセットアップ
公開日時

環境センサ詰め合わせキットのEnviro+ for Raspberry Piを買ったのでセットアップ。

公式サイトにセットアップ手順があるので、こちらを参考に必要ライブラリのインストールを行った。

気温の補正

Enviro+はHat型であるため、BME280センサーがRaspberry PiのCPU温度の影響を受けてしまうとのこと。

実際にHat型でないBME280センサーの値と比較してみると、+5℃ほど高い値が表示されていた。

そこで、[[compensated-temperature.py]]サンプルコードのように気温の補正を行う必要がある。

各自の環境で補正値を調整できるようfactorという変数が定義されているので、別のBME280センサの値と比較しながら、この値を調整した。

我が家の場合は[[factor = 1.6]]で±0.5℃程度の誤差に補正できた。

湿度の補正

温度センサがCPU熱の影響を受けてしまうので、湿度センサの値も併せて補正する必要があった。

湿度センサの表示値はRHxx%と書かれています。相対湿度のことで、温度が±1℃変化すると、絶対湿度が変わらなくてもRHは±3%ずれます。したがって、湿度センサは温度を同時に測って補正した結果を出力しています。

そこで、湿度表示の際にRaw湿度 +(Raw温度 - 補正温度)* 3に補正するようにした。

# all-in-one.py

# Get the temperature of the CPU for compensation
def get_cpu_temperature():
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE, universal_newlines=True)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:output.rindex("'")])


def raw_temp():
    return bme280.get_temperature()

def compensated_temp(factor, cpu_temps):
    cpu_temp = get_cpu_temperature()
    # Smooth out with some averaging to decrease jitter
    cpu_temps = cpu_temps[1:] + [cpu_temp]
    avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
    temp = raw_temp()
    return temp - ((avg_cpu_temp - temp) / factor)

# Tuning factor for compensation. Decrease this number to adjust the
# temperature down, and increase to adjust up
factor = 1.6

cpu_temps = [get_cpu_temperature()] * 5

delay = 0.5  # Debounce the proximity tap
mode = 0     # The starting mode
last_page = 0
light = 1

# Create a values dict to store the data
variables = ["temperature",
             "pressure",
             "humidity",
             "light",
             "oxidised",
             "reduced",
             "nh3",
            # "pm1",
            # "pm25",
            # "pm10"
            ]

values = {}

for v in variables:
    values[v] = [1] * WIDTH

# The main loop
try:
    while True:
        proximity = ltr559.get_proximity()

        # If the proximity crosses the threshold, toggle the mode
        if proximity > 1500 and time.time() - last_page > delay:
            mode += 1
            mode %= len(variables)
            last_page = time.time()

        # One mode for each variable
        if mode == 0:
            # variable = "temperature"
            unit = "C"
            data = compensated_temp(factor, cpu_temps)
            display_text(variables[mode], data, unit)

        if mode == 1:
            # variable = "pressure"
            unit = "hPa"
            data = bme280.get_pressure()
            display_text(variables[mode], data, unit)

        if mode == 2:
            # variable = "humidity"
            unit = "%"
            raw_hum = bme280.get_humidity()
            temp = raw_temp()
            comp_temp = compensated_temp(factor, cpu_temps)
            data = raw_hum + (temp - comp_temp) * 3
            display_text(variables[mode], data, unit)
            
        # ...
        
        time.sleep(1.0)

# Exit cleanly
except KeyboardInterrupt:
    sys.exit(0)

これで正しくデータ表示できるようになった。

配線なしで色々なセンサ情報をディスプレイに表示できるので便利だ。

ガスセンサとか光センサも活用したい。

参考


Related #raspberry pi