Hello,
Yesterday I wired my AM2302 ( DHT22 in a plastic enclosure ), and to my surprise the humidity measurement is way off (about 10%).
Was I just unlucky, and got a crappy sensor?
Here’s the code I am using to connect to it.
#include "PietteTech_DHT/PietteTech_DHT.h"
#include "math.h"
#define DHTTYPE AM2302 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 3 // Digital pin for communications
//#define NUM_TRACES 2 //There will be 4 data traces in the stream
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
bool odd; // counter
double temp; //temperatura era double
double umid; //umidità era double
double t; //mi serve per approx
void dht_wrapper() {
DHT.isrCallback();
}
double humidity = 0, cTemp = 0;
String code = "";
void setup()
{
Serial.begin(9600);
Serial.println("Device starting...");
//while (!Serial.available()) {
// Serial.println("Press any key to start.");
// delay (1000);
//}
Serial.println("DHT Example program using DHT.acquireAndWait");
Serial.print("LIB version: ");
Serial.println(DHTLIB_VERSION);
Serial.println("---------------");
Particle.variable("Humidity", humidity);
Particle.variable("Temperature", cTemp);
Particle.variable("code",code);
}
void loop()
{
Serial.print(": Retrieving information from sensor: ");
Serial.print("Read sensor: ");
//delay(100);
int result = DHT.acquireAndWait();
switch (result) {
case DHTLIB_OK:
Serial.println("OK");
code = "OK";
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
code = "checksum error";
break;
case DHTLIB_ERROR_ISR_TIMEOUT:
Serial.println("Error\n\r\tISR time out error");
code = "isr error";
break;
case DHTLIB_ERROR_RESPONSE_TIMEOUT:
Serial.println("Error\n\r\tResponse time out error");
code = "response timeout err";
break;
case DHTLIB_ERROR_DATA_TIMEOUT:
Serial.println("Error\n\r\tData time out error");
code = "data timeout err";
break;
case DHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
code = "aquiringt error";
break;
case DHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
code = "dt too small";
break;
case DHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
code = "not started";
break;
default:
Serial.println("Unknown error");
code = "error";
break;
}
Particle.publish("code", code, PRIVATE);
if ( result != DHTLIB_OK )
return;
umid=DHT.getHumidity();
Particle.publish("Humidity", String(umid),PRIVATE);
temp=DHT.getCelsius();
Particle.publish("Temperature", String(temp),PRIVATE);
delay(15000);
}