DHT22 sensor on Raspberry Pi
Small, simple Humidity and Temperature graphs with a Raspberry Pi and the DHT22 sensor
Log time ago I got a Raspberry Pi (Version B1, the first one). I never had a simple enough project or the time to do anything with it beside installing RetroPi and use it for old C64 games. Now I found a simple enough project, which I implemented in a weekend: With a DHT22 Temperature / Humidity sensor I created a small weather station which measures and graphs those two values.
The sensor: DHT22
The DHT22, also named AM2302, from Aosong Electronics is a cheap, pre-calibrated and compensated Temperature and Humidity module.
It sends a simple, single-bus signal with its sensor values. Luckily there are already many libraries for C/Python that can read the sensor data with ease.
The board
The electronic setup of the DHT22 is really simple: Connect 3.3V - 5V, GND, and the data PIN, with a pull up resistor. I soldered the DHT22 and the resistor together with a 5-PIN connector to a laboratory card:
Et voilĂ , the sensor side is done.
The cable
My idea was to have the sensor some meters away from the Raspberry, so that I could measure the outside temperature, while the PI sits in the dry house. So I “borrowed” an old Ethernet cable, which already has twisted cable pairs and a minimal shield. For my experiment, I took a 2m cable, clipped the connectors and soldered two 5-pin terminal connectors to the ends:
I only connected the pins / cables that I really needed. For the Raspberry side, I deceided to use Pins 2 (5V), 6 (GND) and 10 (GPIO15).
The final result looks like this:
The software
As a developer, I wanted to do most of the software by myself. I decided to use a Pyhton library to read the DHT22 sensor:
Then I wrote the rest of the software:
- A python script that reads the sensor data, executed by a 5min-cron job (reader.py)
- The python script stores its data into a Toby Oetliker’s RRD Database, using its Python bindings
- A python script that generates pre-defined graphs from the RRD database (create_graphs.py)
- A nodejs server that servers the frontend, a very simple ReactJS application. (server.js)
The tools itself are nothing sophisticated: They do just the minimum (collecting data, procude some graphs).
All the code can be found on my github repo: https://github.com/bylexus/dht22_reader
The graphs generated using RRDTool looks like those: