Read and send two DHT22 temperature/humidity data using python

0

Script to read two DHT22 temperature/humidity sensors connected to (e.g. Raspberry PI) and will send the data to a remote API to store the data.

This was used to read two sensors. One outside a building and one from inside connected to Raspberry PI.

NOTE: The remote API is not part of this article.

#!/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

# Setup DHT22 2
sensorId2 = 'bw4o5n7w'
sensor2 = Adafruit_DHT.DHT22
DataPin2 = 24

# Setup HTTP Request
url = 'https://server/post.php'
http = urllib3.PoolManager()

# 
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: Temperature: {0:0.1f}*C Humidity: {1:0.1f}%'.format(temperature1,humidity1))

        # Send data
        GPIO.output(GreenLedPin, GPIO.LOW)
        r = http.request('POST',
                         url,
                         fields={'sensorId': sensorId1, 'temperature': str(temperature1), 'humidity': str(humidity2)})
        print(r.data)
        GPIO.output(GreenLedPin, GPIO.HIGH)

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

    try:
        # Read data
	GPIO.output(RedLedPin, GPIO.LOW)
        humidity2, temperature2 = Adafruit_DHT.read_retry(sensor2, DataPin2)
        print('Sensor 2: Temperature: {0:0.1f}*C Humidity: {1:0.1f}%'.format(temperature2,humidity2))
        GPIO.output(RedLedPin, GPIO.HIGH)

        # Send data
        GPIO.output(GreenLedPin, GPIO.LOW)
        r = http.request('POST',
                         url,
                         fields={'sensorId': sensorId2, 'temperature': str(temperature2), 'humidity': str(humidity2)})
        print(r.data)
        GPIO.output(GreenLedPin, GPIO.HIGH)

        # Wait 10 seconds
        time.sleep(30)
    except ValueError:
        GPIO.output(RedLedPin, GPIO.HIGH)
        GPIO.output(GreenLedPin, GPIO.HIGH)
        print('Sensor 2: Error reading data')
        time.sleep(10)
    except KeyboardInterrupt:
        GPIO.output(RedLedPin, GPIO.HIGH)
        GPIO.output(GreenLedPin, GPIO.HIGH)
        GPIO.cleanup()
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