Go Down

Topic: Class for DHT11, DHT21 and DHT22 (temperature & humidity) (Read 62 times) previous topic - next topic

robtillaart


Probably your sample frequency is too high, IIRC the datasheet advices 2000 millis between samples.
Can that be the problem?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Static

A quick update:
I started playing around a bit more and fixed the problem.   It seems that i needed to drop the comma after the sensor request (dht.temperature) instead of (dht.temperature, 1).  I'm not sure why that matters.   I'm getting serial prints using the previuos codes.

Static

Sorry, missed a comment in between.

I'm pretty sure I'm sampling every thirty seconds.  However, I just realized that my code is getting the data several times in each iteration.  I'd think that this would simply get the old values, but perhaps not.  I've done some code revisions.  I also changed the temperature format to the US standard Fahrenheit, to allow my girlfriend to use the system.

Right now the system is working.  I'm still getting checksum errors at about half the rate.

Code: [Select]
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
*  Sensors:  DHT22 dataline connected to pin 7.  Suggested 4.7k Pull-up used
              Analog Pin 0 is a photocell to detect light levels
              Analog Pin 1 is a motion sensor

* Based on code:
* Created 22 April 2011
* By Jeffrey Sun
* http://code.google.com/p/pachubelibrary/
* and
* robtillart
* http://arduino.cc/forum/index.php/topic,58531.0.html
* http://arduino.cc/playground/Main/DHTLib

*/
#include <dht.h>
#include "ERxPachube.h"
#include <Ethernet.h>
#include <SPI.h>

dht DHT;
#define DHT22_PIN 7
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x21, 0xE3 };
byte ip[] = { 192, 168, 1, 20   };                  // no DHCP so we set our own IP address

float TempC = (DHT.temperature, 1);
float Humidity =(DHT.humidity, 1);
float TempF;
float OldTemp = TempF;
float OldHumidity = Humidity;

#define PACHUBE_API_KEY "Your API Key HERE" // fill in your API key PACHUBE_API_KEY
#define PACHUBE_FEED_ID 34665 // fill in your feed id

ERxPachubeDataOut dataout(PACHUBE_API_KEY, PACHUBE_FEED_ID);

void PrintDataStream(const ERxPachube& pachube);

void setup() {

Serial.begin(9600);
Ethernet.begin(mac, ip);

dataout.addData(0);
dataout.addData(1);
dataout.addData(2);
        dataout.addData(3);
}

void loop() {

Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
float fSensorData = 15.23;
        int chk = DHT.read22(DHT22_PIN);
        switch (chk)
        {
          case 0:  Serial.print("OK,\t"); TempC = (DHT.temperature); Humidity = (DHT.humidity); break;
          case -1: Serial.print("Checksum error,\t"); break;
          case -2: Serial.print("Time out error,\t"); break;
          default: Serial.print("Unknown error,\t"); break;
          }
        // DISPLAY DATA
        //Temp = (DHT.temperature, 1);
        //Humidity = (DHT.humidity, 1);
        //int(Temp);
        //int(Humidity);
        //Serial.print(Temp);
        //Serial.print(",\t");
        //Serial.println(Humidity);
        TempF = float((TempC * float(1.8))+32);
        //Serial.print(DHT.humidity, 1);
        //Serial.print(",\t");
        //Serial.println(TempC);
        //Serial.println(TempF);
       
dataout.updateData(0, analogRead(0));
dataout.updateData(1, analogRead(1));
//dataout.updateData(2, (DHT.temperature,1));
        //dataout.updateData(3, (DHT.humidity,1));
        dataout.updateData(2, TempF);
        dataout.updateData(3, Humidity);
int status = dataout.updatePachube();

Serial.print("sync status code <OK == 200> => ");
Serial.println(status);

PrintDataStream(dataout);
        OldTemp = TempF;
        OldHumidity = Humidity;
delay(30000);
}

void PrintDataStream(const ERxPachube& pachube)
{
unsigned int count = pachube.countDatastreams();
Serial.print("data count=> ");
Serial.println(count);

Serial.println("<id>,<value>");
for(unsigned int i = 0; i < count; i++)
{
Serial.print(pachube.getIdByIndex(i));
Serial.print(",");
Serial.print(pachube.getValueByIndex(i));
Serial.println();
}
}

robtillaart


The statement "float TempC = (DHT.temperature, 1); "  makes little sense,
You copied it from :   Serial.println(DHT.temperature, 1);    where it means    print the float DHT.temperature with one decimal  You cannot copy that into an assigment,


Here a patched (& stripped) version of your code, not tried as I don't have the pachube lib and sensor nearby to rebuild your sketch.
Let me know how it works...
Code: [Select]

#include <dht.h>
#include "ERxPachube.h"
#include <Ethernet.h>
#include <SPI.h>

dht DHT;
#define DHT22_PIN 7
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x00, 0x21, 0xE3 };
byte ip[] = {
  192, 168, 1, 20   };                  // no DHCP so we set our own IP address

float TempC = 0;
float Humidity = 0;
float TempF = 0;
float OldTemp = 0;
float OldHumidity = 0;

#define PACHUBE_API_KEY "Your API Key HERE" // fill in your API key PACHUBE_API_KEY
#define PACHUBE_FEED_ID 34665 // fill in your feed id

ERxPachubeDataOut dataout(PACHUBE_API_KEY, PACHUBE_FEED_ID);

void PrintDataStream(const ERxPachube& pachube);

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);

  dataout.addData(0);
  dataout.addData(1);
  dataout.addData(2);
  dataout.addData(3);
}

void loop() {

  Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
  float fSensorData = 15.23;
  int chk = DHT.read22(DHT22_PIN);
  switch (chk)
  {
  case 0: 
    Serial.print("OK,\t");
    TempC = DHT.temperature;
    Humidity = DHT.humidity;
    break;
  case -1:
    Serial.print("Checksum error,\t");
    break;
  case -2:
    Serial.print("Time out error,\t");
    break;
  default:
    Serial.print("Unknown error,\t");
    break;
  }
 
  TempF = float((TempC * float(1.8))+32);

  dataout.updateData(0, analogRead(0));
  dataout.updateData(1, analogRead(1));
  dataout.updateData(2, TempF);
  dataout.updateData(3, Humidity);
 
  int status = dataout.updatePachube();

  Serial.print("sync status code <OK == 200> => ");
  Serial.println(status);

  PrintDataStream(dataout);
  OldTemp = TempF;
  OldHumidity = Humidity;
  delay(30000);
}

void PrintDataStream(const ERxPachube& pachube)
{
  unsigned int count = pachube.countDatastreams();
  Serial.print("data count=> ");
  Serial.println(count);

  Serial.println("<id>,<value>");
  for(unsigned int i = 0; i < count; i++)
  {
    Serial.print(pachube.getIdByIndex(i));
    Serial.print(",");
    Serial.print(pachube.getValueByIndex(i));
    Serial.println();
  }
}


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Static

robtillaart,
Thank you very much!
I'm in the process of trying to reduce the number of erroneous readings with the DHT22 sensor.  It seems that a 1 in 10 error rate is something to actually shoot for.  Some of my test runs are scoring an error rate of closer to 4 in 10 or higher.

Has anyone had any success reducing the error rate with this sensor?  I've placed the filter capacitor across the power and ground pins, but that didn't seem to have much of an effect.

Go Up