Plotting Home Temp Using Arduino and Pachube

20111006-000440.jpg

In this project I’m using DHT11 heat and humidity sensor to plot the temperature of my place, A simple Sketch reads the temperature once every 5 minutes, writes it to the serial port to be received by a java application using RXTXcomm library, and then uploads it to Pachube via JPachube library.

First of all the wiring, red goes to 5v, black to GND and blue to Pin2, using the DHT library I wrote the following simple sketch, the function delay isn’t designed for such tasks however and due to the simplicity of the sketch I was able to use it.

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
 // Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {

    Serial.println(t);

    for(int i=0; i<300; i++)///delay 5 minutes
    {
      delay(1000);
    }
  }
}

I’m using the RXTXcomm test function to handle the serial event and then submit the data to pachube, the active bit being

input.read(chunk, 0, available);
String t=new String(chunk);
System.out.println(t);
new pachube().update(1, Double.parseDouble(t));

jpachube is simple enough to use, I’ll take about it in full details in another entry, mainly you set up a feed (designated by a unique ID) and then within that feed you set up Datastreams, you then call on an update function that includes code similar to

Pachube p=new Pachube(API);
Feed f = p.getFeed(36952);
f.updateDatastream(stream, value);

Worked like a charm and I’m going to update this post with the graph after I accumulate enough data to make it look interesting, however I’m really impressed by arduino’s capabilities.

—-update—-

As it tuns out the java loop as is ended up sending 27.00 as two values (27 and then 00) which threw off the graph, the resolution was placing a Thread.sleep(500); at the beginning of the serial event handler (right after the try), the advantage here is that the java application waits until the serial line is loaded with the full message before reading it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s