0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Raspberry+am2302温湿度センサーで温湿度を計測する

Last updated at Posted at 2020-02-18

GPIOに繋がる

https://github.com/adrianlois/RaspberryPi-sensor-dht22-am2302-thingspeak を参考して
am2302温湿度センサーをraspberry piのGPIOに繋がる。

Pythonの設置

pip install --upgrade pip setuptools wheel
pip install Adafruit_DHT

Adafruit_DHTはpython2とpython3両方対応しています

python code

AdafruitDHT.py


import sys
import Adafruit_DHT


sensor_args = { '11': Adafruit_DHT.DHT11,
                '22': Adafruit_DHT.DHT22,
                '2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
    sensor = sensor_args[sys.argv[1]] #センサーのタイプー
    pin = sys.argv[2] #GPIOのpinナンバー
else:
    print('Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>')
    print('Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4')
    sys.exit(1)

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
    print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
    print('Failed to get reading. Try again!')
    sys.exit(1)

実行

python AdafruitDHT.py 2302 4

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?