Temperature and Humidity Logging with RaspberryPi 4

After a long time with all my RC stuff, I got interested to do something with the RaspberryPI. I am using one already a media center but that was not really a challenge.

Motivation was to solve a problem we have at some of our customer sites. Here in a tropical environment, working air conditions in server rooms are vital. At some customer sites, a not well functioning aircon caused issues with server hardware. To prevent that in the future, a temperature monitoring would be great. We are monitoring our customers already with Nagios. The idea was to purchase a network capable thermometer that we can hook up with Nagios.

There are ready made solutions, but to my surprise really expensive! Another thing that would be nice to have, are remote switchable power plugs. To at least to be able to switch off/on devices using low amps like a PBX or a wireless access point.

I was sure that all this can be done with a RaspberryPi. What we need is a temperature sensor and some relay board, saw some for Arduinow which should do the job. Quickly I discovered the DHT22 sensor, which provides temperature and humidity data!

dht22I found them at eBay for $4.68 a piece with free shipping. I ordered two and got to my surprise two little boxes and in each of them 2 DHT22. So I got four over all. Nice!

For the relay I found two channel boards with opto coupler, selling at eBay for less than $4. I ordered two as well, but this part of the whole project will be covered in a separate blog posting. For now I want to concentrate on the temperature and humidity monitoring.

For the biggest part of the project, connecting and reading the DHT22 sensor, exists already a tutorial with some programs:

http://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview

The wiring is actually quite easy. I can recommend to use a cable from an old USB device, you will need only 3 wires and one of them is for data, hooked up in our example to Raspberry’s GPIO 4.

I am using the Adafruit_DHT driver, which is nothing more than a little C program, to retrieve the data from the sensor. I did a small modification to the code, that it is easier to parse its output. The modified version can be downloaded here. Also I had to change the Makefile, I was not able to compile the code after updating Raspbian on my RaspberyPI.

This is the use of the little shell program to read the data from the sensor.  I changed the output to “temperature|humidity“:

To make these reading web accessible I wrote a little PHP script, which executes “Adafruit_DHT” and parses its output. With a little bit of inline CSS I even formatted it nicely and get an attractive output:

temp

You need to copy Adafruit_DHT into the corresponding path and my check the permission. You can add the S Flag (chmod +s /usr/bin/Adafruit_DHT) to make it run permanently with superuser permissions. Not the most elegant way, but the simplest.

The next part of the project was, to collect the data every 10 minutes and store it in a database.

For that purpose a second PHP script was created, executed every 10 minutes from cron.

 

So the first parts of the project are done. Whats missing is the Nagios script and something to use the data which are stored in the database. This will be an interesting tool to look for trends. My plan is to write a multi plattform application with Lazarus, displaying the data in graphs.

I will update my blog once this project is further progressing.

Adafruit DHT Driver (modified)
Adafruit DHT Driver (modified)
Adafruit_DHT.tar.gz
25.1 KiB
686 Downloads
Details

4 thoughts on “Temperature and Humidity Logging with RaspberryPi

  1. Pingback: The magic of SQL ← Lincade.org

  2. Reply Malfatti Oct 30, 2013 11:42

    Hello guy
    I´m student and want work with Temperature and Humidity with RaspberryPi, but can´t get a temperature and Humidity with this code “Adafruit DHT Driver (modified)”, this code not read a data of sensor, and not work, you have any idea?

    thanks

    • Reply admin Nov 4, 2013 08:36

      Hi, I would guess for a wiring issue to your sensor. You can try the original code from the Adafruit website, link is provided in the article. Holger

  3. Reply Beecher Apr 29, 2014 07:51

    Hi, i am trying to use your code, i can get the T° and Humidity with the modified adafruit code, but can’t get the monitoring, i put the modified adafruit in the same path /usr/bin/Adafruit_DHT, give the permissions, the result is the windows and “echo” everywhere, i miss something….
    Thank’s for your work, and hope you can give me a hand !

Leave a Reply