How’s the weather in there? Using a DHT22 and an Arduino to find out.

Regular readers will know of my interest in (obsession with?) the temperature and humidity here in the office. Today I took this to a whole new level with this:

DHT22 Wired to Arduino

That’s an Arduino wired up to a DHT22 temperature and humidity sensor ($9.95 from The Robot Shop).

Using the wiring diagram helpfully provided by Adafruit I connected the DHT22 to the Arduino, with data flowing in on pin #7.

Next I installed this DHT library for Arduino and then used this slightly adapted Arduino sketch to actually grab the data:

#include <dht.h>

dht DHT;

#define DHT11_PIN 4
#define DHT22_PIN 7

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  int chk = DHT.read22(DHT22_PIN);
  Serial.print(DHT.humidity, 1);
  Serial.print("\t");
  Serial.println(DHT.temperature, 1);

  delay(2000);
}

It took a little bit of fiddling to get things to work – it turns out that I’d used the wrong resistor to bridge the data pin with the 5V pin – but once it was working, monitoring the virtual serial port on my Macbook displayed the current humidity and temperature every 2 seconds:

34.2     22.7
34.1     22.7

and so on.

Finally, to pipe this through to Cosm and the world, I set up this Python script to run in the background on my Mac (adapted from here; I needed to install python-eeml and pySerial first):

import eeml
import eeml.datastream
import eeml.unit
import serial
import time

# parameters
API_KEY = 'MY API KEY'
API_URL = '/v2/feeds/104026.xml'

arduino = serial.Serial('/dev/tty.usbserial-A6008jtr', 115200)

while 1:
    readings = arduino.readline().strip().split('\t')
    pac = eeml.datastream.Cosm(API_URL, API_KEY)
    pac.update([eeml.Data(0, readings[1], unit=eeml.unit.Celsius()), 
        eeml.Data(1, readings[0], unit=eeml.unit.RH())])
    pac.put()
    time.sleep(60)

The result is this Cosm feed, showing temperature and humidity for the office:

Reinventorium Temperature

Reinventorium Humidity

The old analog gauge in the office agrees with the DHT22 measurement almost exactly; the humidty on the analog gauge reads about 6% higher:

Analog Humidistat

I’d like this setup to be less dependent on the intermediate MacBook (it makes the MacBook far less portable, for one thing), so my next step will be to adapt the setup to either use the Raspberry Pi to gather and forward the data, or to get an Ethernet shield for the Arduino so that it can hand this by itself.

Comments

Steve's picture
Steve on February 12, 2013 - 11:51 Permalink

Yes. That is obsession.

Oliver's picture
Oliver on February 18, 2013 - 18:12 Permalink

Archeology museums and ancient manuscript libraries must need to do the same thing to set off an alarm above a certain dampness.

madis's picture
madis on May 10, 2013 - 21:48 Permalink

hey,
nice project! Im trying to replicate it, however I am having hard time to get python up and running (this is quite new for me).
But my first question: dont you need Ethernet shield for arduino to run this? In Cosm it sort of says you do…

Thanks for reply

John O&#039;Hanley's picture
John O'Hanley on June 19, 2013 - 00:41 Permalink

There’s also a wireless-network shield for the Arduino, but it’s a bit expensive (~90$).