LoginSignup
14
21

More than 5 years have passed since last update.

RaspberryPiでDHT11センサーから温湿度データを取得

Posted at

目的

RaspbeeryPiで温湿度データを取得する

使用したもの

・Raspberry Pi 3 MODEL B
・DHT11(温湿度センサー)。
・ジャンプワイヤー×3本

参考

ラズパイでDHT11温湿度センサーを作動する

参考元からの変更点

他からの呼び出し予定があるため、温度と湿度をリターン
センサーが0を返してきたら無視する

ソースコード

gettemp.py
# coding: UTF-8
# RaspberryPiでDHT11センサーから温湿度データを取得

import time
import dht11
import RPi.GPIO as GPIO

#定義
#GPIO 14 as DHT11 data pin
Temp_sensor=14

#温湿度データ取得
def get_temp():

    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)

    instance = dht11.DHT11(pin=Temp_sensor)

    while True:
        #データ取得
        result = instance.read()
        return result.temperature,result.humidity

if __name__ == '__main__':
    try:
        while True:
            #温湿度データ取得
            temperature,humidity = get_temp()

            #画面出力
            if temperature == 0:
                continue
            print("Temperature = ",temperature,"C"," Humidity = ",humidity,"%")

            #指定された秒数スリープ
            time.sleep(5)

    except:
        pass

実行

事前にdht11.pyをダウンロード
sudo wget http://osoyoo.com/driver/dht11.py

実行
sudo python gettemp.py

スクリーンショット (1269).png

14
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
21