Read DHT22 temperature sensor using python

0
#!/usr/bin/env python

import sys
import RPi.GPIO as GPIO
import Adafruit_DHT
import time
import urllib3

GPIO.setmode(GPIO.BCM)

# Setup RED LED (read Data indication)
RedLedPin = 17
GPIO.setup(RedLedPin, GPIO.OUT)
GPIO.output(RedLedPin, GPIO.HIGH)

# Setup GREEB LED (send Data)
GreenLedPin = 27
GPIO.setup(GreenLedPin, GPIO.OUT)
GPIO.output(GreenLedPin, GPIO.HIGH)

# Setup sensor

# Setup DHT22 1
sensorId1 = 'aw4o5n7w'
sensor1 = Adafruit_DHT.DHT22
DataPin1 = 4

# 
GPIO.output(RedLedPin, GPIO.LOW)
GPIO.output(GreenLedPin, GPIO.LOW)
time.sleep(5)
GPIO.output(RedLedPin, GPIO.HIGH)
GPIO.output(GreenLedPin, GPIO.HIGH)
time.sleep(5)

while True:
    try:
        # Read data
        GPIO.output(RedLedPin, GPIO.LOW)
        humidity1, temperature1 = Adafruit_DHT.read_retry(sensor1, DataPin1)
        print('Sensor 1: Temperatur: {0:0.1f}*C Luftfeuchtigkeit: {1:0.1f}%'.format(temperature1,humidity1))

        GPIO.output(GreenLedPin, GPIO.HIGH)
	time.sleep(10)
    except ValueError:
        GPIO.output(RedLedPin, GPIO.HIGH)
        GPIO.output(GreenLedPin, GPIO.HIGH)
        print('Sensor 1: Error reading data')
	time.sleep(10)
    except KeyboardInterrupt:
        GPIO.output(RedLedPin, GPIO.HIGH)
        GPIO.output(GreenLedPin, GPIO.HIGH)
        GPIO.cleanup()

Christian Unnerstallchristian

Share.

About Author

Christian Unnerstall

Christian Unnerstall, founder of devshooin.com, enjoys designing and building things that look nice and work well, whether it's websites or services within the interwebs. Have questions - feel free to reach out to him. Enjoy

Leave a Reply