Sense HAT の環境センサーから温度・湿度・気圧を取得

メモ:  Category:raspberry_pi

Sense HAT に用意されている環境センサーから温度・湿度・気圧を取得したいと思います。

環境センサーから温度・湿度・気圧を取得

Sense HAT から温度・湿度・気圧を取得するのは非常に簡単で、用意された関数を呼ぶだけで取得することができます。

  • get_humidity() : 湿度を取得
  • get_temperature() : 温度を取得
  • get_pressure() : 気圧を取得

それでは、次の画像のようなイメージでプログラムを作成してみます。

from sense_hat import SenseHat

sense = SenseHat()

humidity = sense.get_humidity()
temp = sense.get_temperature()
pressure = sense.get_pressure()

print("Humidity: %s %%rH" % humidity)
print("Temperature: %s C" % temp)
print("Pressure: %s Millibars" % pressure)

温度を取得するメソッドに 気圧センサーから温度がとれる get_temperature_from_pressure() もありますが、 get_temperature() で取得できる値と違うようです。

Getting started with the Sense HAT」にあるチュートリアルでは、 LED に温度・気圧・湿度をテロップ表示し 18.3 度以下もしくは 26.7 度以上で背景色を変更します。

from sense_hat import SenseHat
sense = SenseHat()

# Define the colours red and green
red = (255, 0, 0)
green = (0, 255, 0)

while True:

  # Take readings from all three sensors
  t = sense.get_temperature()
  p = sense.get_pressure()
  h = sense.get_humidity()

  # Round the values to one decimal place
  t = round(t, 1)
  p = round(p, 1)
  h = round(h, 1)
  
  # Create the message
  # str() converts the value to a string so it can be concatenated
  message = "Temperature: " + str(t) + " Pressure: " + str(p) + " Humidity: " + str(h)
  
  if t > 18.3 and t < 26.7:
    bg = green
  else:
    bg = red
  
  # Display the scrolling message
  sense.show_message(message, scroll_speed=0.05, back_colour=bg)

温度をどのセンサーから取得しているのか分かりませんが、かなり高く計測されます。

bluenote by BBB