Ein Blog

Gesammelte Notizen

Temperature and humity measure with DHT 11 and raspberry pi using python

24 Dezember, 2015 | IoT

For my little IoT project I need some data. The first sensor will be a cheap humidity and temperature sensor DHT 11 (Temperatur- und Feuchtigkeitssensor DHT11 inkl. Zubehör at Amazon) and a Raspberry Pi (like this one Die neuesten! Raspberry Pi Model A + (Plus) - 256MB RAM Made in the UK at Amazon).

 

Setup:

The setup looks like this

undefined

The resistor is between 4,7k Ohm and 10k Ohm. The data pin (the second pin from the left on the sensor) is connected to GPIO #4, The first pin ohirn the left on the sensor is connected to 3.3V Pin on the raspberry py and the last sensor pin is connected to ground. The third sensor pin is not used.

To read the data from the sensor the Adafruit_Python_DHT library from Adafruit is used.

If you are using python 2 use this to get the source and install the lib

git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT/
sudo python setup.py install

if you are using python 3 use this:

 

git clone https://github.com/JoBergs/Adafruit_Python_DHT
cd Adafruit_Python_DHT/
sudo python3 setup.py install --force-pi

The option --force-pi is only necessary if you do not run this on the raspberry pi.he

The data collected from the sensor will be transmitted to the REST Service described here

This is the code that sends the data to the Service.

import requests 
import Adafruit_DHT

# Init the DHT11
sensor = Adafruit_DHT.DHT11
# GPIO 4 is used
pin = 4
# now read the values
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
uuid = "Sensor1"

data = "empty"
if humidity is not None and temperature is not None:
    data = 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity)
    
payload = {'data':data, 'uuid':uuid}
requests.put("http://localhost:8080/sensor/", json=payload)

use

sudo python3 DHTDataProvider.py

to run this code.

Links:

Measure temperature and humidity with the sensor DHT22/AM2302

Luftfeuchtigkeit und Temperatur mit DHT11 / DHT22 und dem Raspberry Pi messen

DHT humidity sensing on raspberry pi with gdocs logging