LoginSignup
2
5

More than 5 years have passed since last update.

DHT11をラズパイ + pythonで使えるようにする(メモ)

Last updated at Posted at 2017-09-17

DHT11

安価で導入できる温度湿度計DHT11をサクッと使うため、pythonライブラリを導入(ダウンロードするだけ)。

git clone https://github.com/szazo/DHT11_Python

上記で公開されているdht11_example.pyが、スタートプログラムになる。

dht11_example.py
import RPi.GPIO as GPIO
import dht11
import time
import datetime

# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 14
instance = dht11.DHT11(pin=14)

while True:
    result = instance.read()
    if result.is_valid():
        print("Last valid input: " + str(datetime.datetime.now()))
        print("Temperature: %d C" % result.temperature)
        print("Humidity: %d %%" % result.humidity)

time.sleep(1)
2
5
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
2
5