A Little More Temperature, a Little More Humidity

Following on from yesterday’s experiments with an Arduino, a DHT22 temperature and humidity sensor, some Python and Cosm, a few developments.

First, I generalized and cleaned up the Python code and companion Arduino sketch, and you can now find these both, with some documentation, in a Github repository. This code improves error detection, and filters out the occasional out-of-range reading (like a 2300ºC temperature). It also sends the data to both Cosm and to Thingspeak.

Second, I brought my Belkin WeMo Switch into the office with hopes of wiring it up to this system: the Venta humidifier in the office doesn’t have a humidistat in it, only three fan speeds; I’m thinking that I should be able to set thresholds for turning it on, like “if the humidity is below 25% and it’s after 7:00 a.m., then turn on the humidifer.” Stay tuned for that.

Third, in Safari on my Mac I opened the Cosm feed for the temperature and humidity and the selected File | Open in Dashboard… from the menu.

Safari Open in Dashboard

I then selected the orange temperature “badge” from the page, and clicked Add and then did the same thing for the humidity badge:

Open in Dashboard

The result is that on my Mac OS X Dashboard I now have the temperature and humidity in the office displayed:

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()

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


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_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())])

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.