from dht import DHT22 # DHT22用の制御ライブラリ
from machine import Pin
import utime
sensor = DHT22(Pin(15)) # DHT22のdataピンをGPIO15 に接続
while True: # 温度と湿度を読み取るループ
try:
sensor.measure() # センサーで温度と湿度の計測を行う
temp = sensor.temperature() # 温度を取得(℃)
humid = sensor.humidity() # 湿度を取得(%)
print('温度:', temp, '℃') # 温度を表示
print('湿度:', humid, '%') # 湿度を表示
except OSError as e: # try内処理が失敗した際の処理
print('読み取りエラー:', e) # エラーが発生した場合のエラーメッセージ
utime.sleep(2) # 次の測定まで2秒待機