[PYTHON] Raspberry + am2302 Measure temperature and humidity with temperature and humidity sensor

Connect to GPIO

See https://github.com/adrianlois/RaspberryPi-sensor-dht22-am2302-thingspeak am2302 Connect the temperature / humidity sensor to GPIO of raspberry pi.

Python installation

pip install --upgrade pip setuptools wheel pip install Adafruit_DHT

** Adafruit_DHT supports both python2 and 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]] #Sensor type
    pin = sys.argv[2] #GPIO pin number
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)

Run

python AdafruitDHT.py 2302 4

Recommended Posts

Raspberry + am2302 Measure temperature and humidity with temperature and humidity sensor
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Get temperature and humidity with DHT11 and Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Measure and compare temperature with Raspberry Pi and automatically generate graph
Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 to graph it Part 2
Measure CPU temperature of Raspberry Pi with Python
Measure indoor and outdoor temperature and humidity and display on dashboard
Production of temperature control system with Raspberry Pi and ESP32 (1)
I tried using the DS18B20 temperature sensor with Raspberry Pi
Creating a temperature / humidity monitor with Raspberry Pi (pigpio version)
AM modulation and demodulation with python
Use BME280 temperature / humidity / barometric pressure sensor from Python on Raspberry Pi 2
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
Improved motion sensor made with Raspberry Pi
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Use PIR motion sensor with raspberry Pi
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
I compared wireless IoT sensors from one end (temperature and humidity sensor edition)
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter
MQTT RC car with Arduino and Raspberry Pi
Updated temperature sensor
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Easy IoT to start with Raspberry Pi and MESH
Detect mask wearing status with OpenCV and Raspberry Pi
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Simulate temperature measurement with Raspberry Pi + Flask + SQLite + Ajax
Create your own IoT platform using raspberry pi and ESP32 (Part 3) ~ ESP32 settings Analog temperature sensor